Reading PWM signal

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Leag2824
Posts: 3
Joined: Thu Oct 10, 2019 3:55 pm

Reading PWM signal

Post by Leag2824 » Thu Oct 10, 2019 4:10 pm

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 :?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Reading PWM signal

Post by jimmo » Fri Oct 11, 2019 3:49 am

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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Reading PWM signal

Post by dhylands » Fri Oct 11, 2019 5:48 am

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

Leag2824
Posts: 3
Joined: Thu Oct 10, 2019 3:55 pm

Re: Reading PWM signal

Post by Leag2824 » Fri Oct 11, 2019 4:40 pm

Thanks a lot for your fast responds !
I will try later when I have more time

Leag2824
Posts: 3
Joined: Thu Oct 10, 2019 3:55 pm

Re: Reading PWM signal

Post by Leag2824 » 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 !

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Reading PWM signal

Post by jimmo » Sun Oct 13, 2019 11:17 pm

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.

Hybricks
Posts: 1
Joined: Sun Apr 05, 2020 7:37 pm

Re: Reading PWM signal

Post by Hybricks » Sun Apr 05, 2020 7:44 pm

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().

Post Reply