Software Pulse Width Modulation


Pulse Width Modulation is a topic that tends to give a lot of beginners trouble. [Daqq], whose nixie plasma ball we covered a few days ago, has a simple but effective PWM project that you should take a look at. The circuit used 9 LEDs clustered together into 3 sets of RGB modules and connected them to an AVR ATtiny2313 through some current limiting resistors. Most of the time the PWM function of the AVR’s timers would be used to generate the signal but this application calls for 9 signals which is more than can be produced by this chip. The workaround is to generate the signals using software PWM.

Make sure you read up on PWM. Once you’ve got a good idea of what you’re trying to do, check out the code that [Daqq] included with his YALBlinkie project. When studying main.c from the project package, notice that a timer is running that periodically adjusts the duty cycle of each signal. Meanwhile the infinite loop within main constantly scans the output pins with the duty cycle values the timer is setting. This results in the amount of time the LEDs are illuminated being slowly changed, making them fade in and out.

Because ATtiny2313’s are very inexpensive, this is a great way to jump into using the AVR line of microprocessors. You will need a programmer but there are ways of using an Arduino to program these chips. To learn more about timer interrupts and how these chips work, check out the tutorial section over at AVR freaks. Also take a look at the avr-libc documentation for an explanation of how to fix the deprecated sbi and cbi commands in [Daqq’s] code.


11 thoughts on “Software Pulse Width Modulation

  1. hooray! hack a day is taking the “this isnt a hack” comments seriously!!! hardly any crap today!

    also i think the dude put pinky and the brain on in the background himself, on purpose. it made me lol.

  2. @Thomascpp

    I agree with you and I dont. the 8 bit binary calculator is fairly simple logic. it is a hack, to be sure, but this is actually much more useful, for the exact reasons stated… most chips only have a few PWM outputs, so it becomes necessary to utilize software.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.