You Are Here: Home » AVR ATmega Projects » A sunrise and sunset lamp with LEDs

A sunrise and sunset lamp with LEDs

You know it, in the winter time it is hard to get up, because it is dark outside and your body just won’t wake up in the middle of the night. So you can buy an alarm-clock that wakes you up with light. These devices are not as expensive as few years ago, but most of them look really ugly.
On the other hand, most of the time it is also dark when you come home from work. So the great sunset is also gone. Wintertime seems sad, isn’t it?

But not for the readers of this instructable. It explains you how to build a combined sunrise and sunset-lamp from a picaxe microcontroller, some LEDs and a few other parts.

The LEDs might cost you 5-10 Euros depending on the quality and the other parts should not make more than 20 Euros. So with less than 30 Euros you can build something really helpful and nice.
And this instructable will not only explain you how to rebuild this, but also show you how to modify it to your individual preferences.

Step 1Things we need

You need these things:

o       12V or 24V power-supply
o       1 Picaxe 18M (or any other microcontroller) from
o       A socket for a 3.5mm phone-jack, or any other connection from the serial-port to the microcontroller to program the picaxe
o       1 pushbutton and 1 toggle-switch, or 2 pushbuttons
o       1 IC7805 with capacitors, this converts us the 12V or 24V to the 5V we need to operate the microcontroller
o       1 IC ULN2803A, This is a Darlington Transistor Array for the direct use on TTL-Level outputs. Alternatively use 8 single Darlington-Transistors with suitable resistors but it also works with the standard BC547-transistors.
o       1 High-Power FET like the IRF520, or some other Power-Darlington-transistor like the BD649
o       A whole bunch of LEDs, different colors like red, yellow, white, warmwhite, blue and ultraviolet. Read step 4 for further informations.
o       1 10k&-potentiometer, preferable with a long knob
o       1 300&- potentiometer for testing purposes
o       Some resistors, some cables, a board to build the circuit and of course a soldering-iron
o       A measuring tool for currents would also be handy, but is not absolutely necessary

Depending on the power-source you use you might need additional connectors and a housing for the LEDs. I used an acrylic board which I fixed to the housing of the Power-supply.

In older computer-mouses with D-Sub-connectors you might find a good substitute for the phone-jack cable used to program the picaxe.

Picaxes and a lot of other useful stuff might be bought here:

For the rest just check out your local dealer.

Step 2The circuit-layout


The ULN2803A is a darlington-array, consisting of 8 individual darlington-drivers with suitable resistors on the input-side so that you could directly connect the output from the microcontroller to the input of the UNL2803A. If the input gets a high level (5V) from the microcontroller, then the output will be connected to GND. This means that a high on the input will light up the respective LED-strip. Each channel might be used with a current up to 500mA.

Standard ultrabright 5mm LEDs normally use 25-30mA per strip and even eight of them will stress the FET only with 200-250mA, so your far away from any critical points. You might even think about using high power 5W LEDs for the wakeup light. They usually use 350mA at 12V and might also be driven by this array.

The pushbutton “S1” is the reset-button for the microcontroller.

The switch “S2” is the selector of sunset or dawn. You could also replace it by a pushbutton and activate sunset by an interrupt in the software.

The potentiometer R11 acts as a selector for speed. We use the picaxes ADC ability to read out the position of the potentiometer and use this value as the timescale.

The picture shows the first board that I built with 7 individual transistors (BC547C) and the resistors to drive them. I didn’t have the ULN2803 at the time I build the circuit, and now I’m missing some other parts. So I decided to show you the original layout, but also provide the layout with the new driver-array.

What is sunset looking like?

When you observe a real sunset you might recognize that the colour of the light is changing over time. From a bright white when sun is still over the horizon it changes to a bright yellow then to a medium orange then to a dark red and after that a low blueish white glow, then there is darkness.

The sunset will be the most difficult part of the device because you watch it with full consciousness and little mistakes are quite annoying. Sunrise is principally the same program reversed but as you are still asleep when the sunrise starts, we don’t have to worry too much about colours. And starting your sunset when laying down, you might not want to start out with bright sunshine but in the morning it is important to get the most out of the LEDs. So it is convenient to have different sequences for sunrise and sunset, but you are free to test anything you like of course!

But these differences in the programs, might lead us to a different selection of LEDs for both programms.

Selecting the LEDs and calculating the resistors

Selecting the LEDs is the creative part of this instructable. So the following text is just a suggestions from me to you. Feel free to vary and change them, I will tell you how to do this.

It is difficult to smoothly switch a strip on or off with LEDs of a complete new colour. So my recommendation is that each strip contains LEDs of all colours but in changing quantities.
If we imagine the sunset reversed the first strip would contain a lot of red LEDs and maybe one white, a blue and a UV one. So let’s say 5 red ones, 2 yellow, 1 warm white and 1 UV.  If you like you might replace one of the red or yellow LEDs by an orange one (Strip 2 in the schematic)
The next brighter strip would then have a few red ones substituted by yellow ones. Let’s say 2 red, 5 yellow and 2 warm white (strip 3 in schematic)
In the next strips a few more red ones will be substituted by yellow ones or even white ones. Let’s say 1 red, 1 yellow, 4 warm white and 1 blue. (strip 4 in schematic)
The next strip might consist of 3 cold white, 2 warm white and 1 blue LED. (strip 5)

This would be four strips for sunset so far. For Sunrise we could use the leftover three strips with mainly cold white and blue LEDs. If you connect the 7th and the 8th input together you could also use 4 strips for sunrise, or give sunset a fifth strip, just as you like.

You might have noticed that the strips containing red LEDs have more LEDs per strip than the pure white ones. This is caused by the difference in minimum voltage for red and white LEDs.

As the LEDs are really bright and even dimming them down to 1% is quite a lot, I calculated strip 1 with 3 reds, 2 yellow and a warmwhite LED to have only 5mA of current. This makes this strip not as bright as the other ones and therefore suitable for the last hint of sunset. But I should have given this strip an UV-LED too, for the last glance.

How to calculate the LEDs and the resistors:

The LEDs need a certain voltage to operate and even the darlington-array uses 0.7V per channel for its own purpose, so to calculate the resistor is very simple. The FET practically doesn’t cause any voltage loss for our purposes. Let’s say we operate at 24V from the power supply. From this voltage we subtract all the nominal voltages for the LEDs and 0.7V for the array. What is left must be used by the resistor at the given current.

Lets look at an example:
first strip: 5 red, 2 yellow, 1 warm white and 1 uv LED.
One red LED takes 2.1V, so five of them take 10.5V.
One yellow LED also takes 2.1V, so two of them take 4.2V.
The white LED takes 3.6V, the UV LED takes 3.3V and the array 0.7V.
This makes 24V -10.5V – 4.2V – 3.6V – 3.3V – 0.7V = 1.7V which must be used by some resistor.
You surely know Ohm’s law: R = U/I. So a resistor that uses 1.7V at 25mA has a value of 1.7V/0.025A = 68 Ohm which is available at electronic stores.
To calculate the power used by the resistor just calculate P = U*I, this means P = 1.7V * 0.025A = 0.0425 W. So a small 0.25W resistor is enough for this purpose. If you use higher currents or want to burn more volt in the resistor you might have to use a bigger one!

That’s the reason why you could only operate 6 high voltage consuming white LEDs on 24V.
But not all LEDs are really the same, there might be big differences in the voltage loss from LED to LED. So we use the second potentiometer (300 ?) and a current-meter to adjust the current of each strip to the desired level (25mA) in the final circuit. Then we measure the value of the resistor and this should give us something around the calculated value.
If the result is something in between two types then choose the next higher value if you want the strip to be a little darker or the next lower value for the strip to be a bit brighter.

I installed the LEDs in an acrylic glass board which I fixed to the power-source-housing. Acrylic glass can easily be drilled and bend if heated to around 100°C in the oven. As you can see on the pictures I also added the sunrise – sunset selection switch to this display. The potentiometer and the reset-button are on the circuit-board.

Step 5Adjusting the software


The picaxes are very easy programmable by some basic dialect from the vendor. The Editor and the software are free of charge. Of course one might also program this in assembler for blank PICs or for the Atmel AVRs, but this was one of my first projects after I tested the picaxes. In the meantime I work on a better version with several PWMs on an AVR.

The picaxes are very good for beginners because the requirements to the hardware are very simple and the basic-language is easy to learn. With less than 30€ you can start to explore the wonderful world of microcontrollers. The disadvantage of this cheap chip (18M) is the limited RAM.
If you chose other features or connect the picaxe different you might have to adjust the program. But surely you will have to make adjustments to the transitions between the individuals strips.

As you can see in the listing the variable w6 (a word-variable) acts as a counter – variable and as the parameter for the PWM. With the chosen PWM-frequency of 4kHz the values for 1% to 99% duty-time are 10 to 990 respectively. With the calculations in the loop we get a nearly exponential decrease or increase of LED-brightness. This is the optimal when you control LEDs with PWM. When switching on or off one strip, this is compensated by the software by changing the value of the PWM.

For example let’s look at the sunset. Initially the outputs 0,  4 and 5 are switched high, that means the respective strips are switched on via the ULN2803A. Then the loop reduced brightness until the variable in w6 is smaller than 700. At this point pin0 is switched low and pin2 is switched high. The new value of w6 is set to 900.
This means that the lamp with strips 0, 4 and 5 at PWM-level 700 is nearly as bright as the lamp with strips 2, 4 and 5 at PWM-level 800. To find out these values you have to test around and try some different values. Try to stay somewhere in the middle, because when you dim down the lamp in the first loop too much, you can not make much in the second loop. This will reduce the colour-change-effect.

To adjust the PWM-settings I used a subroutine that also uses the value of w5 to pause the program. At this point the speed comes in the game. Only during start-up the potentiometer is checked and the value is stored in w5. The number of steps in each loop of the program are fixed, but by changing the value of w5 from 750 to around 5100, the pause in each step changes from 0.75s to 5s.

The number of steps in each loop might also be adjusted by changing the fraction for the exponential de- or increase. But make sure not to use to small fractions, because the variable w6 is always a whole number! If you would use 99/100 as a fraction and apply that to a value of 10, that would give you 9.99 in decimals but again 10 in integers. Also keep in mind that w6 might not exceed 65325!

For more Detail: A sunrise and sunset lamp with LEDs

Leave a Comment

You must be logged in to post a comment.

Read previous post:
Ghetto Programming: Getting started with AVR microprocessors on the cheap.

Microprocessors are so cheap these days. If only there were a way to program them up just as cheaply... *wavy...

Scroll to top