Introduction: Illuminated Presents

At home we have two illuminated presents which are used during the Christmas period. These are simple illuminated presents using a 2 color red-green LED that randomly change color which fade in and fade out. The device is powered by a 3 Volt button cell. The latter was the reason for this project since the battery is depleted very quickly when the presents are powered on for a longer time.

As to prevent the usage of a huge amount of button cell batteries I designed my own version using three rechargeable AAA batteries. This version uses an RGB LED so blue is also possible but that was not part of the original design. My version has the following functions:

  • Control 2 presents at the same time using one PIC12F617 microcontroller. The microcontroller software was written in the JAL programming language.
  • Switch the present on and off using a push button. The original version used a switch for that purpose but a push button was easier in use.
  • Randomly change the color of the presents by fade-in and fade-out of the colors red and green.
  • Switch off the presents when the battery voltage drops below 3.0 Volt. This will prevent the rechargeable batteries from being discharged too much.

After fading-in one color, the LED stays on for a time somewhere between 3 seconds and 20 seconds. Since I still had the unused blue LED I added the feature that both packages will turn blue when the on-time is exactly 10 seconds. This does not happen very often since the random time is generated in timer ticks of 40 milliseconds as described later.

Step 1: Some Theory About Fading-in and Fading Out Using Pulse Width Modulation

The best way to change the brightness of a LED is not by changing the current that flows through the LED but by changing the time the LED is on within a certain time interval. This way of controlling the brightness of a LED is called Pulse Width Modulation (PWM) which has been described several times on the internet, e.g. Wikipedia.

PIC and Arduino have special PWM hardware on board that make it simple to generate this PWM signal but they often have one output for this and so you can only control one LED. For this version I needed to control 5 LEDs (2 red, 2 green and 1 combined blue) so PWM needed to be done in software using a timer which generates both the PWM frequency as well as the PWM duty cycle.

The PIC12F617 has an on-board timer with auto-reload capabilities. This means that once you set the reload value of the timer, it will use that value each time the timeout has passed and so the timer operates stand alone at a specified frequency. Since timing is critical for a stable PWM signal the timer operates on an interrupt basis, not being influenced by the time the main program needs to control and determine the random on-time for the LEDs.

The PWM frequency must be high enough as to prevent seeing any flickering and so I chose a PWM frequency of 100 Hz. For the fade-in and fade-out effect we need to change the duty cycle and so the brightness of the LED. I decided to use a step increment of 5 to increase or decrease the brightness to get the fade-in and fade-out effect and since the timer uses a range of 0 to 255 for the duty cycle, the timer needs to run at 255 / 5 = 51 times the normal frequency or 5100 Hz. This results in a timer interrupt every 196 us.

Step 2: The Mechanical Work

For making the presents I used milk white acrylic plastic and for the rest of the set-up I used MDF. As to prevent that you see the shape of the LED in the package when the LED is on, I put a cover on top of the LEDs that diffuses the light from the LED. This cover came from some old electronic candles that I had but you can also create a cover by using the same acrylic plastic. In the pictures you see what I used as equipment and material.

Step 3: The Electronics

The schematic diagram shows the electronic components you need. As mentioned earlier 5 LEDs are controlled independently where the blue LED is combined. Since the PIC cannot drive two LEDs on one port pin I added a transistor for controlling the combined blue LEDs. The electronics is powered by a 3 AAA rechargeable batteries and can be powered on or off by pressing the reset switch.

You need the following electronic components for this project:

  • 1 PIC microcontroller 12F617 with socket
  • 2 Ceramic capacitors: 2 * 100nF
  • Resistors: 1 * 33k, 1 * 4k7, 2 * 68 Ohm, 4 * 22 Ohm
  • 2 RGB LEDs, high brightness
  • 1 BC557 transistor or equivalent
  • 1 push button switch

You can build the circuit on a breadboard and does not require much space, as can be seen in the picture. You may wonder why the resistor values for controlling the maximum current through the LEDs are so low. This is because of the low supply voltage of 3.6 Volt in combination with the voltage drop that each LED has, which depends on the color per LED, also see Wikepedia. The resistor values result in a maximum current of around 15 mA per LED where the maximum current of the whole system is around 30 mA.

Step 4: The Software

The software performs the following tasks:

When the device is reset by the push button it will turn the device on if it was off or it turns the device off if it was on. Off means putting the PIC12F617 into sleep mode in which it hardly consumes any power.

Generate the PWM signal to control the brightness of the LEDs. This is done using a timer and an interrupt service routine that controls the pins of the PIC12F617 who on their turn the LEDs on and off.

Fade-in and fade-out the LEDs and keep them on for a random time between 3 and 20 seconds. If the random time equals 10 seconds, both LEDs will turn blue for 10 seconds after which the normal red-green fade-in and fade-out pattern is used.

During operation the PIC will measure the supply voltage using its on-board Analog to Digital Converter (ADC). When this voltage drops below 3.0 V, it will switch the LEDs off and will put the PIC into sleep mode again. The PIC could still operate well at 3.0 V but it is not good for the rechargeable batteries to be completely drained.

As mentioned earlier the PWM signal is created using a timer that uses an interrupt service routine as to keep a stable PWM signal. The fading-in and fading-out of the LEDs including the time the LEDs are on, is controlled by the main program. This main program uses a timer tick of 40 milliseconds, derived from the same timer that creates the PWM signal.

Since I did not use any specific JAL libraries for this project this time I had to make a random generator using a linear feedback shift register for generating the random on time and random off time of the LEDs.

Step 5: The Final Result

There are 2 video that show the intermediate result. My wife still needs to change the cubes into actual presents. One video shows a close up of the result where the other video shows it with the original present which lead to this project.

As you may expect when you think you are done, new requirements pop-up. My wife was requesting if the brightness of the LEDs can also vary after they are faded in. That is possible of course since I only used about half of the program memory of the PIC12F617.

The JAL source file and the Intel Hex file for programming the PIC are attached. If you are interested in using the PIC microcontroller with JAL – a Pascal like programming language – visit the JAL website.

Have fun making this Instructable and looking forward to you reactions and results.

Make it Glow Contest

Participated in the
Make it Glow Contest