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.


  1. monkeyslayer56 says:

    now that may be usefull. but the music on the video….

  2. svofski says:

    hi daqq

  3. medix says:

    Excellent post! There is code that runs on a PIC18F4550 that I used about a year ago to perform a similar task. Suppose I should get the writeup finished.. ;)

  4. napalm says:

    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.

  5. octel says:

    Pinky and the Brain was an excellent show

    Pour a Bawls on the curb for fallen cartoon comrades

  6. andrew says:

    this is a hack.

  7. Thomascpp says:

    The binary calculator is just as much if not more of a hack than this.

  8. abbott says:


    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.

  9. Japala says:

    Remove this if not within the rules but here is a good starting point with the AVRs. This is the first part in the series:

  10. Taylor says:

    lololololololol pinky and the brain.

  11. DarkFader says:

    Perhaps I should refer to this article whenever ppl bug me about my 2-year-old PWM demo video. Although mine used a PIC. Here it is again:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 96,650 other followers