pwm

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

pwm

Post by PinkInk » Sat Jul 05, 2014 4:32 pm

pyb module has a pyb.pwm function, and many of the pyboard pins are circled i.e. indicating support for pwm ... but there's no documentation on micropython.org.

pyb.pwm takes two int arguments, and anecdotal evidence points to it being used by pyb.Servo and pyb.LED on LED4, but otherwise it's a bit of a mystery ... has anyone worked out how to use pyb.pwm? If so, could you share your findings?

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: pwm

Post by yllumi » Wed Sep 24, 2014 7:19 am

Is it possible if we create arduino like function such as digitalWrite() and analogWrite()?

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

Re: pwm

Post by dhylands » Wed Sep 24, 2014 7:39 am


fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: pwm

Post by fma » Wed Sep 24, 2014 7:43 am

Great! This is really a usefull feature! Thanks.
Frédéric

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

Re: pwm

Post by dhylands » Wed Sep 24, 2014 7:45 am

yllumi wrote:Is it possible if we create arduino like function such as digitalWrite() and analogWrite()?
pyb.Pin('X1').value(0/1) is the equivalent to digitalWrite

AnalogWrite from arduino is too simplistic. There are 14 timers on the pyboard.

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: pwm

Post by yllumi » Fri Sep 26, 2014 2:24 pm

dhylands wrote:Just landed Timer.channel(Chan, pyb.Timer.PWM, ...)

See: https://github.com/micropython/micropyt ... mer.c#L510
And http://wiki.micropython.org/platforms/b ... r-Examples:
It looks great! But I rather don't understand. Can you refer me any knowledge base to understading all of it (OC, IC, etc)?

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

Re: pwm

Post by dhylands » Fri Sep 26, 2014 3:52 pm

OK - there are 3 basic modes: PWM, OC, and IC.

Each timer has a period register which can set either using freq= or prescaler= and period=
Each timer can have multiple channels.
A channel may be exposed on a pin (sometimes more than one channel (usually from different timers) are exposed on the same pin, but the pin can only choose one).

With PWM, the counter runs from 0..period and the pulse_width detemines how long the pulse stays active. So if you look at the first example:
http://wiki.micropython.org/platforms/b ... r-Examples

The top channel is set to generate a 25% duty cycle and the bottom is set to generate a 50% duty cycle. This example is also showing center-aligned PWM, where two channels from the same timer have their PWM pulses aligned in the center. You can also have them edge aligned, although center aligned is better in some cases; it reduces power spikes if you're using the PWM signal to drive a motor, then having both edges go active at the same time produces a larger power spike than having them staggered.

PWM performs 2 actions for each "cycle". When the counter reaches zero, it sets the output active (except when the pulse-width is zero), and when the counter reaches the pulse-width value, it sets the output inactive. This is what forms the output pulse.

With Output Compare mode, you can only perform one action per cycle. These actions are things like: set-active, set-inactive, toggle. The action occurs when the compare value matches the counter. The example was setup to toggle the output 2000 times per second, which should generate a 1 kHz square wave, which is what the logic analyzer capture shows.

With Input Capture mode, you're configuring the channel to record the value of the counter when some event occurs. You can look for rising edges, falling edges, or both (so 3 different types of events). The third example uses PWM to generate a pulse suitable for driving an RC servo. This is done using timer 5, channel 1, which is sent out on pin X1. It assumes you have a jumper wire connecting X1 and X4 and sets up an input capture on timer 2, channel 4 (which is on pin X4).

The input capture interrupt looks fir a rising edge, and when it finds it it saves away the captured value in ic_start. When a falling edge is detected, it subtracts ic_start from the captured value and this then determines the width of the pulse.

5 times per second, the main loop prints out the value of the servo pulse width being generated (in microseonds) and the value of the last captured pulse (also in microseconds). If everything is working properly, they should be the same.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: pwm

Post by nikhiledutech » Tue May 08, 2018 9:35 am

All the links in page are showing up Bad gateway :?:

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: pwm

Post by pythoncoder » Wed May 09, 2018 10:10 am

I've reported the wiki outage to @Damien so hopefully it will be fixed shortly.
Peter Hinch
Index to my micropython libraries.

Post Reply