6. Pulsing an LED with an ATTiny45 Atmel Microcontroller

(continued from previous page)

Discrete blinking LEDs are convenient, but the blink rate and blink duration are not configurable. I tried goofing around with using transistors, resistors, and capacitors to alter the blinking LED. In the end, I had so many extra electronic components that I figured it would be easier to simply program the lighting effect with software.

As soon as a circuit includes a microcontroller (MCU), it cuts down on the number of hobbyists that are capable of implementing it. Nevertheless, I decided to include this more complex circuit in this article to demonstrate the possibilities.

A number of fancy electronics (computers, MP3 players, etc) now include a pulsing LED that fades in and out. For example, a pulsing LED may indicate that the device is sleeping (in a low power mode).

I decided to recreate this pulsing effect for a pumpkin:

Click to see a movie of a pumpkin with a blue pulsing LED.

Click to see a movie of a pumpkin with a blue pulsing LED.

Pretty cool, huh?

The color can be changed by installing different LEDs. The speed is adjustable through a potentiometer (small versions are called “trimpots”).

Schematic to control LED lighting with an Atmel ATTiny45 microcontroller.

Schematic to control LED lighting with an Atmel ATTiny45 microcontroller.

The heart of the original LED circuit still exists on the upper left side of the schematic. See the 9V battery and the parallel strands of LEDs?

The circuit has become much more complicated because the microcontroller can’t run on 9 volts. A Microchip MCP1702 LDO (low-dropout) voltage regulator (VR1) steps down the battery voltage to 5 volts for the microcontroller section. Several capacitors (C1 and C2) steady the voltage supply to avoid electrical glitches and noise.

A diode (D1) protects all of the electronics components against a reversed battery. This wasn’t necessary in previous circuits because the strands of LEDs wouldn’t be harmed by a reversed battery. But now, a microcontroller and other sophisticated semiconductor parts are installed, and they can be harmed by a flipped battery.

The Atmel ATTiny45 microcontroller has firmware (software inside a chip) that is written in C on a personal computer. The compiled software is then downloaded to the chip by an Atmel programmer. I use an Atmel STK500 board and the ImageCraft C compiler.

In the circuit, the Atmel microcontroller reads the trimpot (R2) using the built-in Atmel ADC (analog-to-digital converter). Depending on the voltage on the trimpot wiper, the microcontroller delays each increment or decrement of the LED brightness. Thus, by adjusting the trimpot, the rate of pulsing can be slowed down or sped up.

All of the power going to the LEDs is controlled through a single bipolar NPN transistor (Q1). A resistor prevents too much electricity from entering the transistor base pin, which the microcontroller controls.

The LEDs can be turned on completely by the microcontroller outputting 5V on the OC1B pin. That turns on the transistor (Q1) which completes the path back to the battery for the LEDs.

The LEDs can be turned off completely by the microcontroller outputting 0V on the OC1B pin. That turns off the transistor (Q1) which disconnects the path back to the battery for the LEDs. It’s as though the battery wire were disconnected from LEDs.

To vary the brightness, the Atmel microcontroller switches the transistor on and off very rapidly. Because the microcontroller switches it so rapidly (four-thousand times a second), you don’t notice the blinking. The longer it is turned on versus turned off, the brighter the LEDs. This is called pulse-width modulation (PWM).

Solderless breadboard layout with pulsing blue LED lighting controlled by an Atmel ATTiny45 microcontroller.

Solderless breadboard layout with pulsing blue LED lighting controlled by an Atmel ATTiny45 microcontroller.

The left half of the breadboard looks similar to earlier layouts. The right half of the breadboard is now crammed with other circuitry. The power from the battery now connects the upper right half of the breadboard, because the microcontroller needs to control the power going to the LEDs.

“GND” stands for “ground”. GND is the lowest voltage level for the IC chips in a circuit. In this case, GND is the negative terminal of the 9V battery.

Candle Shutoff

As an added bonus feature, the Atmel AVR microcontroller timer automatically shuts off the LEDs after a certain period of time (presently 5 hours). So, you don’t have to worry about draining the battery by accidentally leaving it on all night.

If the battery voltage drops too far (dead battery), the microcontroller shuts off the circuit. This is just like a candle going out.

At shutdown, the total circuit measures less than 22 uA (or 0.022 mA) of current consumption. That means a fresh 9V volt alkaline battery would last:

540 mAh / 0.022 mA = 24545 hours = 1022 days.

Of course, the battery won’t be fresh when the LED candle turns off. But, the point is that the shutoff mode uses very little power. This may be particularly useful to avoid deeply draining a rechargeable battery.

Source Code

If you’re interested, I am willing to send you a copy of the source code for the above pulsing effect for your own personal use, for $5 through PayPal. (See legal details.)

It is about 350 lines total of C, including white space and documentation/comments. It is written for an Atmel ATTiny45 microcontroller using ImageCraft ICCAVR 7.13A standard edition.

Lastly, let’s see what a nice LED PCB would look like...