Smart Home Automation Forum
Kc868 Read Pwm input - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A6 (https://www.kincony.com/forum/forumdisplay.php?fid=22)
+--- Thread: Kc868 Read Pwm input (/showthread.php?tid=6811)



Kc868 Read Pwm input - huntleight - 09-30-2024

How to read pwm input with pcf8574 ?


RE: Kc868 Read Pwm input - admin - 10-01-2024

PCF8574 not use for PWM.


RE: Kc868 Read Pwm input - huntleight - 10-01-2024

(10-01-2024, 02:42 AM)admin Wrote: PCF8574 not use for PWM.

so how can i read pwm ?


RE: Kc868 Read Pwm input - admin - 10-01-2024

you can use free GPIOs on board, define with PWM , try to use it.


RE: Kc868 Read Pwm input - huntleight - 10-01-2024

(10-01-2024, 05:40 AM)admin Wrote: you can use free GPIOs on board, define with PWM , try to use it.

I use PCF8574 to access digital input/output ports. Because pinmode(2,OUTPUT); and in loop function digitalWrite(2,LOW or HIGH) does not work . Same goes for input codes.

So
Q1) Can i use digital input ports for PWM? If yes how can i define those pins ?
Q2) If you said no Q1 so which pins is free GPIO and how can i use it? I use serial port pins so i cannot use that pins.


RE: Kc868 Read Pwm input - admin - 10-01-2024

Q1) Can i use digital input ports for PWM? If yes how can i define those pins ? ---> can't
Q2) If you said no Q1 so which pins is free GPIO and how can i use it? I use serial port pins so i cannot use that pins. --> you need check with ESP32 datasheet, which pins can use for PWM output.


RE: Kc868 Read Pwm input - huntleight - 10-01-2024

(10-01-2024, 08:32 AM)admin Wrote: Q1) Can i use digital input ports for PWM? If yes how can i define those pins ?  ---> can't
Q2) If you said no Q1 so which pins is free GPIO and how can i use it? I use serial port pins so i cannot use that pins.  --> you need check with ESP32 datasheet, which pins can use for PWM output.

I explained it wrong. I want to read pwm input. Which GPIO pin is free i dont know but i know i use all input and output ports and serial port for hmi. Which pin is free and how can i access it ? Can you explain with schemes if needed  ?

   


RE: Kc868 Read Pwm input - admin - 10-01-2024

on the TOP-RIGHT photo, there label IO-1 and IO-2 you can use. they are GPIO32, GPIO33.

To read a PWM signal on a specific pin of the ESP32, you can't directly capture the PWM signal as an input in hardware. However, you can use the pulseIn() function in code to measure the high and low durations of the PWM signal, and from there, calculate the frequency and duty cycle.

Method: Use pulseIn() to Capture the PWM Signal
ESP32 offers the pulseIn() function, which allows you to measure the duration of a high or low pulse on a pin. By measuring both the high and low durations, you can calculate the PWM duty cycle and frequency.

Example Code
Here's a simple code example using pulseIn() to measure the PWM signal:
Code:
int pwmPin = 32; // Assume PWM input is connected to GPIO 32

void setup() {
  Serial.begin(115200);
  pinMode(pwmPin, INPUT);
}

void loop() {
  // Measure the high time (in microseconds)
  unsigned long highDuration = pulseIn(pwmPin, HIGH);
  // Measure the low time (in microseconds)
  unsigned long lowDuration = pulseIn(pwmPin, LOW);
 
  // Calculate the total period
  unsigned long period = highDuration + lowDuration;

  // Calculate the duty cycle (percentage)
  float dutyCycle = (float)highDuration / period * 100;

  // Calculate the frequency (Hz)
  float frequency = 1000000.0 / period;

  // Print the results
  Serial.print("Frequency: ");
  Serial.print(frequency);
  Serial.print(" Hz, Duty Cycle: ");
  Serial.print(dutyCycle);
  Serial.println(" %");

  delay(1000); // Print every second
}
Explanation of the Code
pulseIn(pwmPin, HIGH): Measures the duration the pin stays HIGH in microseconds.
pulseIn(pwmPin, LOW): Measures the duration the pin stays LOW.
Adding both high and low durations gives the total period, from which you can calculate the frequency.
The duty cycle is computed by dividing the high duration by the total period and multiplying by 100 to get a percentage.