LED dimming using Binary Code Modulation
This article has given a brief overview of just a couple of methods of dimming LEDs using microcontrollers. In the lists
below, I have summarised points about each method, not to find a 'winner', but to help guide you to which
method might be suitable for your application. If you're just dimming a single LED then hardware PWM is probably
your best bet. If you want to control five or six RGB LEDs from a single AVR, then I'd suggest Binary Code Modulation
as a solution (each colour is a separate channel of output). If you're playing with several hobby servo motors, then Software PWM is
probably for you.
- Very well known method.
- Easy to use.
- Zero processor overhead.
- Limited number of channels.
- Accurate output of a single frequency.
- Suitable for all types of output; speakers, motors, bulbs, LEDs, servo motors (obviously with other driver hardware between the uC and the device)
- Only a 16-bit PWM is really suitable for controlling servo motors, as the duty cycle is restricted to 10% to 20%.
- Can have a very high frequency
- A good solution if you only need to control a few LEDs and the appropriate pins are available for use.
- Well known
- several implementations available
- Multiple channels possible
- Processor overhead is dependent on the number of channels (but not dependent on the resolution)
- Accurate, single-frequency output.
- Also suitable for LEDs, motors, hobby-servos, bulbs etc. (again, obviously with driver hardware, depending on the peripheral in question)
- Can be modified to make it especially useful for hobby servo motors
Binary Code Modulation
- Less well known
- Not many implementations 'in the wild'.
- Can run as many channels as you like
- Processor overhead is dependent on the resolution (but not dependent on the number of channels)
- Suitable for any frequency-instensitive outputs, such as LEDs, bulbs and DC motors.
- Not suitable for servo motors.
- Can easily run at around 500hz, and with some work up to 15khz for 8-bit resolution on an 8Mhz processor.
- Fixed timings and overheads regardless of the duty cycle
- An excellent solution for LEDs.
- Relatively easy to adapt for charlieplexing
It's always difficult/dangerous to simplify and generalise decisions.
The correct choice for a given situation will inevitably involve many more factors than I could possibly cover here,
but in order to offer some guidance (however mistakenly), here is what I'd choose for controlling LEDs with 8-bit resolution;
- 0-4 channels: H/W PWM (if they're available)
- 0-7 channels: S/W PWM
- 6+ channels: BCM
8th January 2009 -
Modified some details about Software PWM as I think I'd been slightly mislead into believing that S/W PWM is more processor intensive than it is.