Maximum number of PWM or how to get more

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.
clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Maximum number of PWM or how to get more

Post by clack » Thu Jan 15, 2015 11:33 am

I need 24 PWM channels, I can see that I can easily get 20 but is there any way to break out more? or some kind of trick I can use?

I do know I can get SPI controlled PWM controllers but I need to be able to choose the PWM frequency.

Does anyone have any ideas?

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

Re: Maximum number of PWM or how to get more

Post by dhylands » Thu Jan 15, 2015 3:18 pm

It really depends on the PWM frequencies and duty cycles that you need.

clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Re: Maximum number of PWM or how to get more

Post by clack » Thu Jan 15, 2015 3:29 pm

dhylands wrote:It really depends on the PWM frequencies and duty cycles that you need.
Well it doesn't need to be accurate frequency just somewhere in the audio range and 8 bit 0-255 is fine.

It a peculiar application, it involves being able to hear the pwm that is used to control 8 RGB LED's without multiplexing and the pyboard only other task would to be read a text file and send the values to the LED's.

I guess I could use something like this? http://www.adafruit.com/datasheets/tlc59711.pdf and change the frequency through EXT_CLK?

But it would be great if you know if I can somehow break out 4 pins?

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Maximum number of PWM or how to get more

Post by Damien » Thu Jan 15, 2015 11:39 pm

To do hardware PWM (using a timer), you need the pin to have a timer capability / alternate function.

As you saw, there are 20 X/Y pins that can do this.

Extra IO pins that have a timer on them are:
  • PB4 = LED_BLUE has TIM3_CH1
  • PA15 = LED_YELLOW has TIM2_CH1
  • PB3 = X17 = SW has TIM2_CH2
They are easily accessible from the bottom 2 rows.

So you only need 1 more... the options are PC8, PC9, PA8 (sd card) and PA9, PA10 (usb):
  • PC8, PC9 have TIM3_CH3, TIM3_CH4 and TIM8_CH3, TIM8_CH4. They are the D0 and D1 data pins of the sd card. You would need to de-solder the SD card to get at them.
  • PA8 has TIM1_CH1 and is the SD card switch. You might be able to get at this with a fine wire connected to the switch bit of the sd card holder, on the top left next to pin Y3. Plug in an SD card to see the 2 pieces of metal come together.
  • PA9 is USB VBUS. If you're not using USB then this would be the easiest to get to, since it's broken out on the bottom of the board, on 1 of the the OTG pads. If you are using USB, you can still use this if you desolder R10.
  • PA10 is USB ID which is also not needed if you don't use OTG (which isn't supported in software yet...). You'd need to desolder R16.
They are your options. See http://docs.micropython.org/en/latest/h ... index.html for schematics. You'd also need to check that, of the 24 PWM pins, they don't have clashing TIM/CH pairs. Ie you need 24 independent TIMx_CHy's.

clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Re: Maximum number of PWM or how to get more

Post by clack » Fri Jan 16, 2015 10:36 am

Thanks so much for your reply! that might work, I did try to use the blue led one but am I right in assuming it is not supported in the pyb.timer library? https://micropython.org/doc/module/pyb/Timer
how would I go about assigning and controlling these timers in micropython, do I have to use a bit of C?

I did experiment in REPL with this, but I cant get it to work.

Code: Select all

>>> timer = pyb.Timer(3, freq=1000)             
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Timer 3 is for internal use only
I would like to keep the USB functionality and the SD card for data but could I get away with stealing the switch from the SD card whilst it still works?
I guess PA10 is an option.

but even so how would I go about re-assigning them, is it quite low level?

thanks for your help!

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Maximum number of PWM or how to get more

Post by Damien » Sat Jan 17, 2015 8:08 am

Actually, you can use the DAC on X5 to make a repeating square wave of any frequency, thus synthesising a PWM (using an analog output!). Or you could use the analog output directly to fade the LED (but that's harder than using PWM to do it due to nature of how LEDs work).

So you don't need to salvage one from the SD card or USB.

To use the TIM3_CH1 PWM of LED_BLUE you can just call pyb.LED(4).intensity(value) and that will set the PWM for you. If you end up needing low-level access then you can use the stm module (no need for any C code!).

Also, since X17 and LED_YELLOW need TIM2, you can't use TIM2_CH1 for X6. Instead you would use the DAC on X6.

All in all I think it's *just* possible to get 24 PWMs. And you can do it all in Python without modifying the hardware!

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Maximum number of PWM or how to get more

Post by Damien » Sat Jan 17, 2015 6:54 pm

Here is what you'll need to use to get 24 PWM:

X1 TIM5 CH1
X2 TIM5 CH2
X3 TIM5 CH3 (or TIM9 CH1)
X4 TIM5 CH4 (or TIM9 CH2)
X5 DAC(1)
X6 DAC(2)
X7 TIM13 CH1
X8 TIM14 CH1
X9 TIM4 CH1
X10 TIM4 CH2

Y1 TIM8 CH1
Y2 TIM8 CH2
Y3 TIM4 CH3 (or TIM10 CH1)
Y4 TIM4 CH4 (or TIM11 CH1)
Y6 TIM1 CH1 negated
Y7 TIM12 CH1
Y8 TIM12 CH2
Y9 TIM2 CH3
Y10 TIM2 CH4
Y11 TIM1 CH2 negated
Y12 TIM1 CH3 negated (or TIM8 CH3 negated)

X17 TIM2 CH2
LED_BLUE TIM3 CH1
LED_YELLOW TIM2 CH1

Timer(3) is used only by LED_BLUE, and so you can use pyb.LED(4).intensity(value) to set that one (and it's already configured).

X5/X6 on the DACs can be configured using a custom square wave.

The rest you can use normal PWM settings. Note that for the negated channels the PWM duty cycle is inverted.

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

Re: Maximum number of PWM or how to get more

Post by dhylands » Sat Jan 17, 2015 8:32 pm

Damien wrote:Here is what you'll need to use to get 24 PWM:
The rest you can use normal PWM settings. Note that for the negated channels the PWM duty cycle is inverted.
For the negated channels, you can also use PWM_INVERTED instead of PWM and then the duty cycle remains the same.

clack
Posts: 20
Joined: Sat May 03, 2014 11:09 am

Re: Maximum number of PWM or how to get more

Post by clack » Tue Jan 20, 2015 5:23 pm

Thanks for your help, will try and work out the code and perhaps report back here when I get stuck!

How would I go about setting a square wave in the DAC? what would make the pulse width change?

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

Re: Maximum number of PWM or how to get more

Post by dhylands » Tue Jan 20, 2015 5:39 pm

clack wrote:Thanks for your help, will try and work out the code and perhaps report back here when I get stuck!

How would I go about setting a square wave in the DAC? what would make the pulse width change?
There is an example of sending a sine wave here: http://docs.micropython.org/en/latest/l ... ml#pyb-dac

A square wave would only use 2 values: 255 and 0

The pulse width would be determined by the number of 255's in a row you have.

Post Reply