Page 1 of 1

Reading PWM signal

Posted: Thu Oct 10, 2019 4:10 pm
by Leag2824
Hi
I'm a nooby in programing and I was wondering if it was possible to read PWM signal using a pyBoard ?
I have found timerchannel.pulse_width_percent but i dont know if it's useful.

thx in advance for your respond

ps. sorry for my english :?

Re: Reading PWM signal

Posted: Fri Oct 11, 2019 3:49 am
by jimmo
If you know the frequency and just want to read the duty cycle, then https://docs.micropython.org/en/latest/ ... e_pulse_us will do the trick.

This should be enough to (for example) read a servo signal.

Re: Reading PWM signal

Posted: Fri Oct 11, 2019 5:48 am
by dhylands
You can also use the IC (input capture) mode of the hardware timers to do this. Here's an example that generates servo style pulses and measures them:
https://github.com/dhylands/upy-example ... ic_test.py

Re: Reading PWM signal

Posted: Fri Oct 11, 2019 4:40 pm
by Leag2824
Thanks a lot for your fast responds !
I will try later when I have more time

Re: Reading PWM signal

Posted: Sat Oct 12, 2019 3:25 pm
by Leag2824
So I tried both methods, but I only was successful with dhylands method. I think I made something wrong with machine.time_pulse_us, so if someone have an example of code with it, it would be super cool !

I'm trying to read PWM signal from an airplane receiver, so the frequency should be 50Hz

Re: Reading PWM signal

Posted: Sun Oct 13, 2019 11:17 pm
by jimmo
Leag2824 wrote:
Sat Oct 12, 2019 3:25 pm
So I tried both methods, but I only was successful with dhylands method. I think I made something wrong with machine.time_pulse_us, so if someone have an example of code with it, it would be super cool !
Using the timer capture mode is definitely a better approach, so stick with that. :)

I'm curious as to why time_pulse_us didn't work, perhaps the likelyhood of starting a measurement mid-pulse is too high. I'll investigate myself sometime.

Re: Reading PWM signal

Posted: Sun Apr 05, 2020 7:44 pm
by Hybricks
Just wanted to thank you for pointing me to time_pulse_us() in order to read the PWM from a RC Receiver.
Works great on the Microbit, but had to find out first to set the input port to digital: pinX.read_digital().