LED dimming using Binary Code Modulation

Overview

The motive behind this article is to introduce Binary Code Modulation to a wider audience. Binary Code Modulation (BCM) is an alternative to Pulse Width Modulation (PWM) when controlling frequency insensitive devices, such as LEDs. Both of these methods are described.

There are other methods of LED dimming available (for example error diffusion), but these are not covered.

Example code is provided for Binary Code Modulation that demonstrates 8 channels of 8-bit (256 values) dimming.

Introduction

OK, so you're bored with a blinking LED. What about dimming an LED instead?

  1. Dig out your first blinking LED program (yes, really!)
  2. Change the delays to be 1/100th of a second rather than 1 second
  3. run the code.

Voilla, your LED will be dimmer.

Wow, short article!

 

Oh, hang-on, this article is supposed to cover a little more ground than that... I'll witter on a little longer...

The flippant first example demonstrates that despite controlling the LED in an essentially binary fashion (it is only ever on or off), the human eye can be fooled into believing that the LED is being dimmed. All we need to do is repeatedly turn it on and off at high speed (the minimum rate required is covered under the 'Complications' section).

In that first example, where the LED is on for the same amount of time as off, it has a "duty cycle" of 50%. This simply means that the LED is on for 50% of the time. If we changed the delays so that the LED is off for twice as long as it's on, then it would have a duty cycle of 33%. i.e. It would be on for an average of 33% of the time.

Duty cycle is calculated as;

It is often stated as a percentage, rather than as a fraction.

The eye isn't sensitive to the frequency at which an LED is flashing (again, assuming a minimum flicker rate - see 'Complications'). An LED running at 50% duty cycle on a 1khz frequency will look the same brightness as an LED running at 50% duty cycle on a 15khz frequency. The eye averages the "on"s and "off"s regardless of their precise timing. It doesn't even matter if the pattern of 'on's and 'off's are not regular. It is the average on-time that counts.

e.g. The following three patterns, if repeated rapidly enough, are indistinguishable from each other by eye;

a low-frequency 50% duty cycle

50% duty cycle (1)

a high-frequency 50% duty cycle

50% duty cycle (2)

a complex 50% duty cycle

50% duty cycle (3)

On to the different methods of creating these "dimming" patterns...