LED Candle (Part 2)

light christmas DIY

Let there be light! Today we are building the electronics for our LED candle.

Finishing up the LED Candle

In the last part of this project, we made a candle stand and a candle wrap. In this second part, we are going to build the electronics. It's time for our LED Candle to light up. For this we are going to use a tiny microcontroller: the ATtiny85.

We will separate the electronics into two components. The LED circuit with the microcontroller and a separate perfboard for the battery holder. Later, both components get connected to form the LED candle. Having the LED circuit on a separate perfboard, makes the build more compact and allows us to power the board via an Arduino Uno while programming the microcontroller.

Required components for the LED candle

For today's build you need:

  • 2x perfboard pieces (single-sided, 11x8 holes, 2.54 mm pitch)
  • 2x female headers (4-pin)
  • 2x male headers (4-pin)
  • 1x ATtiny85
  • 1x switch
  • 1x 68 Ω or 100 Ω resistors
  • 1x 10 kΩ resistors
  • 1x 100 nF ceramic capacitor
  • 1x CR2032 coin cell + clip

Additionally, the following tools will be required:

  • Arduino Uno and six jumper wires (female-male) for programming
  • Soldering Iron, solder, wire cutter
  • Hot glue gun and hot glue
  • A drill matching the size of the battery clip contacts

Let's go!

Building the LED Circuit

We start with the LED circuit. The circuit itself is pretty simple. An LED is connected to the ATtiny85 via a 68 Ω resistor. A switch is used to turn the supply voltage on and off. Additionally, a 100 nF capacitor is added to the supply rails and a 10 kΩ pull up resistor is connected to the reset pin and prevent unintentional resets. These last two components are good practice but not strictly necessary for the circuit to work.

Here is how the final circuit looks like:

Let's go through the build process step by step:

Soldering in the components for the LED circuit

Start by soldering in the individual components as shown in the breadboard view. Start with the flattest component first and then work your way up. Make sure that the components have the correct orientation. Pin 1 of the ATtiny85, which is marked by a small dot, needs to be in the upper-left position. The cathode (–) of the LED, which is marked with a flat spot in the casing, needs to be on the right side.

Front and back of the perfboard with the LED circuit

Once you soldered in all components, it's time to make the connections between the individual components. For that you can use thin wires, make solid solder traces or use the cut-offs from the components. You can see all necessary connections in the breadboard view, the schematic and in the image above. Note, that the board has been flipped horizontally.

Fixating the switch with some hot glue

Depending on the used switch, it can be required to fixate it and prevent it from ripping off when pressing it. For this you can use hot glue to fill out the space behind the switch. This will give it some extra mechanical stability. Be careful to not heat up the switch too much or its plastic case might start to deform. Make sure that the glue does not get into the switch.

Building the Battery Holder

Let's continue by building the battery holder. The image below shows the breadboard view and schematic for it:
Breadboard view and schematic for the battery holder

Prepare the perfboard for the battery clip

Start by preparing the perfboard for the battery clip. Usually the contacts of the battery clip are too large for the standard 0.8 mm holes in the perfboard. In that case you need to drill two holes for the contacts of your battery clip. After that solder in two bare metal wires for making contact with the bottom side of the CR2032 coin cell. You can e.g. use the cut-offs from the LED circuit's components for that.

Front and back of the perfboard with the battery holder

Once you prepared the perfboard, it's time to solder in the battery clip and the header bars. Make sure to make a strong solder connection to the battery clip. It will experience quite some mechanical stress. When making the connections between the battery clip and the header bars, make sure to do them in the correct orientation. Note, that the board has been flipped horizontally in the image above the schematic shows the connections from the top.

Putting everything together

Now that we have built both modules, let's try connecting them together. The image below shows the final construction. Note, that the LED won't turn on until we program the ATtiny85.

Final LED candle construction with battery holder and LED circuit

It is important to not put the modules together in the wrong way around. The battery clip opening should point down, the LED should point up. To ensure this, you can cut off an unnecessary pin, e.g. the third from above on the left side. Then block the corresponding pin on the battery module as shown in the image below. This makes it impossible to plug both modules into each other in the wrong way.

Mechanical reverse polarity protection by cutting of one pin and blocking the corresponding hole on the battery module

Programming the ATtiny85

To light up the LED, we need to program the ATTiny85. For this, I prepared a small program, that controls the LED using PWM. The program changes the brightness randomly. For that it calculates a random value, that is added to the current brightness, to define the new target value. It then enters a loop, which slowly adjusts the brightness to the target value. This produces a nice and slow sparkling effect which can be adjusted by changing the constants for speed, variance, and brightness.

Here is the code for this program:

const int outputPin = 4;

void setup() {
  randomSeed(0);
  pinMode(outputPin, OUTPUT);
}

const int maxValue = 200;
const int minValue = 0;
const int variance = 20;
const int fadeSpeed = 100;

int targetValue = 50;
int value = 50;

void loop() {
  targetValue += random(variance*2)-variance;
  targetValue = max(targetValue, minValue);
  targetValue = min(targetValue, maxValue);

  while(value < targetValue) {
    value++;
    analogWrite(outputPin, value);
    delay(fadeSpeed);
  }

  while(value > targetValue) {
    value--;
    analogWrite(outputPin, value);
    delay(fadeSpeed);
  }
}

There is just a tiny problem: how can we upload this program to the ATtiny85 microcontroller? Well, since the ATtiny85 has no support for USB it is not possible to connect it directly to a computer. We need to program it with a dedicated programmer. Luckily, we can use the Arduino Uno as such a programmer. This process is explained below.

First, we need to add support for the ATtiny85 hardware to the Arduino IDE. This allows us to compile a program for it. For this purpose, we use the ATTinyCore library provided by SpenceKonde. To install it, follow the installation guide linked down below.

The next step is to prepare the Arduino Uno for the use as programmer. For this we need to program it with the Arduino ISP sketch. You can find this sketch under File > Examples > 11. ArduinoISP > ArduinoISP. Open it and flash it to the Arduino Uno. Make sure that you have selected the Arduino Uno as target board.

Select Arduino Uno as target board

We have now prepared the Arduino Uno for the use as programmer. To program the ATtiny85, we need to connect it to the Arduino Uno as shown in the image below.

Connecting the ATtiny85 to an Arduino Uno for programming

When everything is wired up, it is time to upload the LED candle program to the ATtiny85. For this you need to select ATtiny25/25/85 (No bootloader) as target board. Decrease the clock speed to 1 MHz, to allow the ATtiny85 to run safely, even when the battery voltage decreases over time. Finally, select Arduino as ISP as programmer and upload the LED candle code to the ATtiny85.

Selecting the necessary options for the ATtiny85

The Result

It's done! You now have your own custom-made LED candle. The LED should now light up, and you should see a result similar to the one in the video below. Note, that the sparkling effect is barely visible due to its slow speed. However, if you scroll through the video it becomes much more noticeable.

Note, that I also added a small hat out of white transparent paper on top of the LED. This causes the light to be more diffused and light out the candle wrap more evenly.

Improving the diffusion by adding a small hat out of white transparent paper

I wish you a merry Christmas and a happy new year. Be blessed!

Previous Post Next Post