Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 9194

Python • Re: Read PWM on a Raspberry pi 5

$
0
0
You need to measure the length of a pulse:

Wait for pulse to be low
Wait for rising edge
Start timer
Wait for falling edge
Stop timer

Assuming you know the PWM frequency then you can calculate the PWM percentage. If not you could just measure that too.

Obviously there are edge cases in the above algorithm, such as when the PWM signal is at 0% or 100%, or if the pin is disconnected, or the sender is malfunctioning. The biggest problem you will have is latency, because the Pi OS is not a real-time system, and you can not guarantee that your time measurement will not be interrupted by another OS task.

You could do it with an external micro, which is deterministic. You could do it with an external ADC. Or you could modify the code in the ESP32 so that it sends the value you are looking for from a serial port, and read it that way. Alternatively you could use a Pi 4 or lower, which will let you run the pigpio library and measure PWM that way:
https://abyz.me.uk/rpi/pigpio/examples.html

Statistics: Posted by ame — Thu Jan 01, 2026 8:54 pm



Viewing all articles
Browse latest Browse all 9194

Trending Articles