Author Topic: PWM pins on microcontrollers  (Read 4465 times)

0 Members and 1 Guest are viewing this topic.

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
PWM pins on microcontrollers
« on: December 07, 2011, 02:00:04 AM »
Im not quite sure how to word this, but I guess the question will portray itself eventually.

I know  that different micro controllers have different amounts of PWM pins.  Arduinos have 6 PWM, Teensys (http://www.pjrc.com/teensy/ Finally came in the mail!) have 7... And I know that they have different microcontroller "brains".  The arduino uses an Atmega 328, and I'm not sure what the Teensy has.  So I'm guessing that the amount of PWM pins are based on the main microcontroller that an augmented microcontroller is built around (seems obvious....).  But, even though the Arduino only has 6 PWM, does that mean that the ATMEGA 328 only has 6 available?  Or can you get more PWM out of microcontrollers...

So, if I were to want lots of PWM pins (like thousands) would I have to get hundreds (if not thousands) of controllers to run them?  I'm sure there are controllers that have more then 6, no idea how many could be on one though.

So... To sum it all up.... What makes a PWM pin special and how can I get more out of a chip?

(I know PWM are special because of what they can do in the programming, but what about them hardware wise makes them special?) 

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: PWM pins on microcontrollers
« Reply #1 on: December 07, 2011, 05:14:08 AM »
Hi Xyver  :)

But, even though the Arduino only has 6 PWM, does that mean that the ATMEGA 328 only has 6 available?
According to ATmega328 datasheet there are only 6 PWM pins.

Or can you get more PWM out of microcontrollers...
You can create Software PWM for any I/O pin, however, the more of them You have, the more inaccurate it gets. You can also utilize I2C interface to get PWM if You use some extra hardware (555 timer and digital pot based).

So, if I were to want lots of PWM pins (like thousands) would I have to get hundreds (if not thousands) of controllers to run them?
If You want Hardware PWM to be driven directly by uC, then yes, You will need a lot of them.

I'm sure there are controllers that have more then 6, no idea how many could be on one though.
Yes there are. Here's the list of controllers that have highest amount in their class:

  • ATiny26, ATiny28L - 9 PWM channels

  • ATmega169A, ATmega329A, ATmega6450A - 15 PWM channels

  • ATxmega64A3, ATxmega128A3 - 24 PWM channels

  • AT32UC3A1256, AT32UC3A1128, AT32UC3A0256, AT32UC3A0128, AT32UC3B0256, AT32UC3B1256, AT32UC3B0128, AT32UC3B064 - 35 PWM channels

What makes a PWM pin special and how can I get more out of a chip?
Real PWM is digital signal of set frequency and set duty-cycle (both (not always) can be altered in runtime). You cannot get more PWM channels from uC that it has already (appart from methods mentioned above.

(I know PWM are special because of what they can do in the programming, but what about them hardware wise makes them special?)
Stable hadware timer and efficiency.
« Last Edit: December 07, 2011, 05:40:45 AM by newInRobotics »
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: PWM pins on microcontrollers
« Reply #2 on: December 07, 2011, 05:24:14 AM »

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,166
  • Helpful? 111
Re: PWM pins on microcontrollers
« Reply #3 on: December 07, 2011, 01:32:40 PM »
See also http://www.societyofrobots.com/member_tutorials/node/228

Whats special about the PWM pins? Well its that they generate the waveforms in hardware  ie you just set up the cpu registers and it gets on with the job and any other code you want to run can go ahead at full speed.

Yes - you can simulate PWM in software (as WebbotLib can do) to make it available on other pins. However: this is normally done using a timer/channel. So the next limiting factor is how many timer/channels there are. EG a timer may have 2 channels (normally called A and B) so you could use the timer to generate two more PWM signals. The caveat is that the frequency of the PWM would need to be similar even tho you can set the duty cycles independently. The downside is that you need to write more code to do this and it takes up processing time.

If you are willing to add extra hardware, and you are using the PWM to control servos, then WebbotLib for example allows you to control up to 8 servos from one channel by adding a 74HC238 chip. This scenario only works for things like servos where you dont need 100% duty cycle. The max duty cycle per channel is = 100% / (number of outputs). So for 8 servos its 100/8=12.5%. Assuming that the frequency is 20ms then this means a max output pulse of 12.5% x 20ms = 2.5ms which is fine for a servo.

So it depends on what you are using the PWMs for.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: PWM pins on microcontrollers
« Reply #4 on: December 07, 2011, 08:01:03 PM »
Hi,

So, if I were to want lots of PWM pins (like thousands) [...]
It might help towards a solution, if you reveal the purpose, for which you needs thousands of PWM outputs.
Perhaps there are other methods, to do whatever it is that you want to do, that you have overlooked, but nobody will know if you keep your goal hidden :)
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: PWM pins on microcontrollers
« Reply #5 on: December 08, 2011, 05:25:56 PM »
I've been thinking that the next electronics project I want to make is an LED cube, with RGB leds.  I was going to make a practice one, a 3x3x3, to see how it works, but then I was going to amp it up and go for a 10x10x10 once I know how it works and such.  Then, I was thinking on how to get more colors out of an RGB and PWM seemed like a good solution.

So, 10x10x10= 1000 LEDs, 3 colors per LED means I need 3000 PWM potentially xD

EDIT:: Actually, I may only need 300 PWM.
« Last Edit: December 22, 2011, 03:22:04 PM by Xyver »

Offline rbtying

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: PWM pins on microcontrollers
« Reply #6 on: December 08, 2011, 07:42:43 PM »
Or you multiplex, which is what most people do with LED cubes. There's no reason why you can't just use SPI-based LED drivers, either.

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 32
  • Your success is in your hands.
Re: PWM pins on microcontrollers
« Reply #7 on: December 08, 2011, 10:57:18 PM »
You can use this TLC5940
Quote
The TLC5940 is a 16 channel PWM unit with 12 bit duty cycle control (0 - 4095), 6 bit current limit control (0 - 63), and a daisy chainable serial interface. It is handy for expanding the number of PWM outputs available.

http://www.arduino.cc/playground/Learning/TLC5940
Using multiplex with 3 TLC5940 each for each layer with RGB LED, so 10 layers will need 30 quantity of TLC5940.

Look at the wires if you are not using multiplex.

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: PWM pins on microcontrollers
« Reply #8 on: December 08, 2011, 11:11:20 PM »
I'm pretty much following this: 

http://www.instructables.com/id/LED-Cube-4x4x4/

I believe multiplexing is a  part of that.

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: PWM pins on microcontrollers
« Reply #9 on: December 22, 2011, 03:21:31 PM »
So yes, multiplexing is part of the game plan.  As far as I can tell though, I'll still need 300 PWM pins...

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: PWM pins on microcontrollers
« Reply #10 on: December 23, 2011, 01:37:57 AM »
Hi,

So yes, multiplexing is part of the game plan.  As far as I can tell though, I'll still need 300 PWM pins...
The optimal matrix with common multiplex will always be sqr(number of LEDs)
sqr(1000) is 31.6 and since it's hard to make "decimal lines", you round up to 32.
That means that with 32 rows and 32 columns, you can drive up to 1032 LEDs (with a duty cycle of 1/32).
You only need either the rows or the columns to be PWM, so you'll need a maximum of 32 PWMs.

But... You could just adjust how much of the time slice that a given LED gets current and then you have PWM without using dedicated PWM lines.


You'll probably have to use either very low current LEDs or transistors (or integrated buffers like the ULNxxxx types), possibly both, as the 1/32 time slice would theoretically need a 32 times as high current, so for a regular 20mA LED, the equivalent current would be 640mA, which a regular LED won't like.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: PWM pins on microcontrollers
« Reply #11 on: December 23, 2011, 02:20:00 AM »
O.o .... So you're saying I only need 32 pins to drive 1000 LEDs, each having 3 colors and therefore 3 legs?  Those 32 pins are able to make the color changes on each individual color of each individual LED?

I cannot wrap my head around how that would work... If that is really what you are saying, just say yes.  I'm sure there is a large technical explanation on how it works, but I'm not going to make you explain it to me :P

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: PWM pins on microcontrollers
« Reply #12 on: December 23, 2011, 04:11:15 AM »
Hi,

O.o .... So you're saying I only need 32 pins to drive 1000 LEDs, each having 3 colors and therefore 3 legs?  Those 32 pins are able to make the color changes on each individual color of each individual LED?

I cannot wrap my head around how that would work... If that is really what you are saying, just say yes.  I'm sure there is a large technical explanation on how it works, but I'm not going to make you explain it to me :P
No, I said that you could use a matrix of 32 rows and 32 columns (i.e. 64 pins), but that's for a single color. For 3 color LEDs (4 pin I assume, R, G, B and a common anode/cathode) you would need 3x32=96 rows plus 32 columns (or v.v.) a total of 128 pins.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
Re: PWM pins on microcontrollers
« Reply #13 on: December 23, 2011, 06:14:40 PM »
Ok... That makes a little more sense.  Still curious on how 128 pins would be able to drive 3000 colors, but I'm sure it possible.  I don't need to know how to now, I'll probably forget by the time I start building.  Just doing research now.

Thank you :).  So now I need to find 128 PWM pins....

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: PWM pins on microcontrollers
« Reply #14 on: December 23, 2011, 09:19:38 PM »
Hi,

96 will be enough, you don't need to PWM on both rows and columns - the remaining 32 pins should just be held steady for a time slice.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,166
  • Helpful? 111
Re: PWM pins on microcontrollers
« Reply #15 on: December 24, 2011, 06:33:56 AM »
Something like the TLC5947 chip (Google it) may be useful - provides 24 PWM channels with 12 bit PWM - designed for controlling LEDs.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list