Update: Arduino Shift Register PWM Gets Speed Boost

Community collaboration is a great thing. Take the Arduino PWM library for shift registers. Some folks at the Arduino forum pitched in and helped [Elco] trim off a bunch of clock cycles by using the Rotate Over Carry instruction. Now he’s reduced the overhead per shift-register from 108 down to just 43. So far this doesn’t mean more possible outputs – 768 is still quite a lot – but does it means better precision when max outputs are used. This effectively doubles the brightness levels for 768 LEDs from 16 up to 32.

We’re at a loss for what to link to here. [Elco] has a new page for the library. There’s the original forums thread but we didn’t see much of interest there. We found some stuff in the comments of this Reddit post. And of course, if you have no idea what we’re talking about go back and read the original feature.

Using Binary Code Modulation To Control LED Brightness

Pulse Width Modulation is definitely the preferred method of dimming an LED with a microcontroller, but we were interested in hearing about a different method called Binary Code Modulation. BCM does the same thing as PWM, it turns the LED on and off very rapidly so that your eye cannot detect a flicker. The brightness level is a result of the average amount of time the LED is on versus when it is off. This is called duty cycle and although it can be the same percentage for both PWM and BCD, there is a fundamental difference.

While PWM usually uses a cyclical on/off cycle (30% on, 70% off, repeat) BCD uses a cumulative cycle. As you can see above, each successive bit of binary code carries double significance compared to the previous bit. Now just assign a duty cycle based on your precision, and have an interrupt fire for each bit of the counter. The graph above shows some highs and some lows combining to reach the target duty cycle. An interrupt is used for each bit, and pin changes are made in the service routine.

The benefit of this system is that it is scaleable without adding overhead. You’re already running the interrupts so servicing 8 or 128 LEDs doesn’t have vastly different needs as it would with PWM. The big downside is that the more bits of precision you use, the faster your processor must run so that the eye doesn’t detect the lengthy on or off cycles of the higher bits as visible flickering.

Thank you [Yetihehe] for tipping us off about a link that [Tomas Martinsen] left when commenting about an Arduino library for up to 768 PWM outputs.

Output Up To 768 PWM Signals From One Arduino

Here’s an Arduino library that will let you drive a very large number of LEDs. [Elco Jacobs], an electrical engineering student, is the author of the library. He has a work-study job that has him helping out others with their electrical projects and he was constantly being solicited for methods to control droves of light emitting diodes. This was the motivation that led him to produce the dazzling 16 RGB LED example seen in the video after the break.

His setup doesn’t use expensive LED drivers, but instead utilizes 595 shift registers which are both common and cheap. He calculates that it is possible to control up to 96 of these shift registers, each driving 8 LEDs, with reasonably satisfying results. This is thanks to his well-optimized code that manages to drive the clock pin of the registers at 1.33 MHz. This optimization is done by writing each command in assembly, which allows him to precisely count the cycles. Each individual pin takes 12-13 cycles to address, totally 9984 cycles at worst when addressing the maximum number of outputs.

[Elco] thinks this is as fast as he can make the routine run, but he is asking for help with testing. If you think you know how to squeeze out a few more cycles, make sure you join in on his forum thread.

Continue reading “Output Up To 768 PWM Signals From One Arduino”

Pulse Width Modulation With Microcontrollers

Those following the ProtoStack tutorials will be happy to hear that there is a new installment which explains Pulse Width Modulation. If you’ve never heard of PWM before, it’s a method of generating a signal that is logic 1 for a portion of the time and logic 0 for the remainder of the time. It is the most commonly used method for dimming an LED, and that’s [Daniel’s] example in this tutorial. But you’ll also find it used in many other applications such as servo motor control and piezo speaker control.

[Daniel] starts off with a brief explanation of duty cycle, then moves on to some examples of hardware and software PWM. Many of the AVR microcontrollers have a hardware PWM feature that allows you to configure a pin that toggles based on a target timer value. This is demonstrated using an ATmega168, but a method of using interrupts and your own code is also covered in case you don’t have a hardware PWM pin available.

Awesome Custom Disco Basement

led_shelves

[Evan] is one of those neighbors you only wish you had.

His neighbors were renovating their basement for use as home theater, and he stopped by to check out how things were coming along. While there, he suggested they add some LED lighting to their shelving unit to make them pop. His neighbors were game, so he sourced some cheap RGB LEDs online and began working on the circuits and firmware needed to control the lights. His neighbors wrote some custom software that interfaces with iTunes to create a neat visualization in the shelving unit whenever music is played.

Once everything was complete, his neighbors informed him that they wanted an additional 20 overhead can lights and a set of 4 wall sconces wired up as well. Needless to say he was pretty excited, so he got busy wiring up the remainder of the basement.

He pushed the installation’s PIC microcontroller about as far as he possibly could, resulting in the awesome show seen in the video below.

Needless to say, it’s pretty impressive, though we wish we could have a peek at the code used to run everything. Wink, wink, nudge, nudge.

Continue reading “Awesome Custom Disco Basement”

Dimming Control For An Ikea Solar Desk Lamp

[Frank] decided to augment his desk lamp’s features by adding dimming controls (translated). Since the light source is a triad of LEDs the best method of dimming their intensity is to use Pulse Width Modulation. That’s the method that he went with, and luckily the SUNNAN lamp from Ikea which he’s using as the donor for the project has just enough room to squeeze in the parts necessary for this hack.

You need two main bits to use PWM with a lamp like this; a microcontroller (or possibly a timer chip like the 555) and a transistor to protect that chip from the current necessary to run the LEDs at full brightness. [Frank] went with an ATtiny13 and a 2N2222 transistor, both quite common and very inexpensive (you can even pull the microcontroller from a light bulb if you know where to look). Two buttons were added to the top of the lamp base which allow for up and down controls. There’s even an SOS function which is triggered by pressing both buttons at the same time. [Frank’s] happy to show off the completed project in the clip after the break.

Continue reading “Dimming Control For An Ikea Solar Desk Lamp”

Optimizing Code For PWM Efficiency

For some projects, it’s okay to have a microcontroller twiddling it’s thumbs most of the time. When a project requires the cpu to do just one thing over and over, there’s no loss with inefficient code – it either works or it doesn’t. However, if a project requires a microcontroller to do several things at once, like reading sensors, dimming LEDs, and writing serial data out, cpu utilization can become an issue. [Robert] wasn’t happy with the code he used to control a string of LEDs, so he rewrote his code. With the old implementation, [Robert]’s code used 60% of the cpu time. With the new and improved code, the cpu was only busy 8% of the time.

The code works by using a hardware timer to trigger an interrupt. After calculating the next time it should run again, and changing the state of the data line, the code just sits quietly until it’s needed again.

It’s not a pretty hack, or even one you can hold in your hands, but [Robert]’s determination in getting a μC to do what he wants is admirable.