Home | Store | Showcase | Forums | Examples | Guides | Reviews | Support | Download | About Us

 

 

 

RGB Night Light


LEDs have always fascinated me.  I love working & playing with them but unfortunately, I didn't have much of a real-world reason to do a project that focused on LEDs.  That changed when my wife brought home a interesting little lamp that is made up of 5 sunflower plants sprouting out of a round base. Each flower had a small 110v twinkle bulb in it.  As soon as I saw it, I knew I had to pull out those twinkle bulbs and install LEDs.  At first I was going to put in standard white LEDs to approximate the original light, but I read an article on RGB LEDs and I had the idea of making the lamp multi-color.

Most of my projects have a short life; usually a victim of cannibalism for the next project. But this light has turned out be very functional and fun to play with. My wife and I will change the color intermittently to indicate we're having a "green" day or a "blue" day.  

The Construction

I picked up 5 bright 5mm RGB LEDs from SuperBrightLEDs.com. These are common anode with a rated amperage of 20mA per LED color.  

Each color has a different luminosity:

Red:    1200 mcd
Green: 3700 mcd
Blue:      700 mcd

By looking at the LED, you would think it varied that much. At a full 20mA, all of the colors seem really bright in a darkened room. I found that 15mA per LED was plenty to create a functional night light. These have a clear lense so I frosted them with steel wool to diffuse the light.

Each flower has a hollow stem that held the tiny wires feeding the original lamp.  I was just able to fit 4 26-gauge stranded wires into the stems to connect to the RGB LEDs.

I used a small amount of 1/8" heatshrink tubing to protect the LED leads from shorting out and I staggered the solder joints so they wouldn't create a width too large to fit into the tube. It just barely fit in.

 

 

The base is just large enough to allow for a small circuit board.  To make the light unique, I decided to add some intelligence to the mix, instead of a simple on/off switch and a power supply.  I designed the following circuit to give me a myriad of choices on how the lamp could operate.

The heart of this circuit is one of our DevBoard-M32 controllers.  It has plenty if I/O to drive the LEDs as well as the switch and the light sensor.  But trying to drive the LEDs directly would be too much current for the M32.  So 3 darlington transistor arrays were used to control each LED independently and 3 PWM channels were used to drive the brightness of the colors independently.  To drive each LED and it's brightness separately would have required 15 PWM channels, which couldn't be done easily.

The circuit is actually simpler than it seems. An I/O port on the M32 controls the inputs on the transistor array. By turning on one of the transistors, one of the LED elements will light. To control how bright the LED will be, a PWM channel controls the common ground for that chip. This ensures that all of the LED elements of a single color will have the same brightness.  This is repeated once for each color.

The switch is interrupt-driven and will allow for powering on/off the device as well as selecting the operational mode.

The photo cell indicates the ambient light levels and can be used to adjust the brightness of the LEDs.  This hasn't been incorporated into the software yet.

The night light can change colors by push of a button or controlled by the MCU.

Bascom Code for Night Light

I use Bascom basic compiler for AVR microcontrollers. Below is the code I use to control the night light

Click here to download the Bascom file

Note: This code is larger than 2k when compiled. You will need the registered version of Bascom to load this code as-is (but it is only 11% of the capacity of the chip!)

'Varying the output of a tri-color brightness using PWM
'Wright Hobbies, LLC 2004
'http://www.wrighthobbies.net

'Dim Variables
Dim I As Byte , Tmp As Long
Dim Timeslice As Long , Timebase As Long
Dim Random1 As Word , Random2 As Word
Dim Flashmode As Byte

Dim Reddelay(5) As Long
Dim Greendelay(5) As Long
Dim Bluedelay(5) As Long

Dim Redflag(5) As Byte
Dim Greenflag(5) As Byte
Dim Blueflag(5) As Byte

'Define Alias's
Greenbrightness
Alias Pwm1a
Bluebrightness
Alias Pwm1b
Redbrightness
Alias Ocr2

'LED Aliases
Red
Alias Portc
Red1
Alias Portc.0
Red2
Alias Portc.1
Red3
Alias Portc.2
Red4
Alias Portc.3
Red5
Alias Portc.4

Green
Alias Porta
Green1
Alias Porta.0
Green2
Alias Porta.1
Green3
Alias Porta.2
Green4
Alias Porta.3
Green5
Alias Porta.4

Blue
Alias Portb
Blue1
Alias Portb.0
Blue2
Alias Portb.1
Blue3
Alias Portb.2
Blue4
Alias Portb.3
Blue5
Alias Portb.4


'Define Constants
Const True = 1
Const False = 0

'Constants for Dazzle Mode
Const Flash = 1
Const Dark = 1000

'Config statements
Config Timer0 = Timer , Prescale = 64
Config Timer1 = Pwm , Pwm = 8 , Prescale = 64 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down
Config Timer2 = Pwm , Prescale = 64 , Compare Pwm = Clear Up , Pwm = On

'Configure ports for Output or Input
Config Portb = Output 'Set the Port B pins to output
Config Porta = Output
Config Portc = Output
Config Portd = Output
Config Pind.2 = Input
Portd.2 = 1

On Timer0 T0_int

Bluebrightness
= 0
Greenbrightness
= 0
Redbrightness
= 0
Flashmode
= 1

'Enable external interrupt
Enable Int0 'Used for mode switch

Enable Timer0 'Used as a general timer
Enable Timer1 'Used for 2 PWM channels
Enable Timer2 'Used for 3rd PWM channel
Enable Interrupts
On Int0 Buttonpushed 'Interrup routine triggered by the switch

'Start the timers
Start Timer0
Start Timer1
Start Timer2


'Turn off all LEDs
Red
= 0
Green
= 0
Blue
= 0

'Set default brightness
Redbrightness
= 200
Bluebrightness
= 200
Greenbrightness
= 200

'Set startup mode to be off
'Start the light by pushing the button
Flashmode
= 0

'Main loop
'The flashmode variable is set in the Buttonpushed routine.
'Since that is interrupt driven, we don't need to poll the switch.

Do
Select Case Flashmode

Case 0
'Power off
'Powerdown mode will reduce power consumption
'but can still be awakened by an external interrupt
Blue
= 0
Red
= 0
Green
= 0
Powerdown
'return from powerdown
'Disable the external interrupt while we do some housework
Disable Int0
Red
= 1
Blue
= 1
Green
= 1
Redbrightness
= 200
Greenbrightness
= 200
Bluebrightness
= 200

'Debounce the switch
Do
If Pind.2 = 1 Then Exit Do
Loop

Enable Int0
'Default to mode 1 after powerdown
Flashmode
= 1

Case 1
'Color Cycling
Random1
= Rnd(10)
Select Case Random1
Case 1
Do
'cascade up and down
Red
= 0
Blue
= 0
Green
= 0

'red first
Red
= 1
For I = 1 To 5
If Flashmode <> 1 Then Exit For
Waitms 50
Red
= Red * 2
Next I
Red
= 0
'Blue
Blue
= 1
For I = 1 To 5
If Flashmode <> 1 Then Exit For
Waitms 50
Blue
= Blue * 2
Next I

Blue
= 0
'green
Green
= 1
For I = 1 To 5
If Flashmode <> 1 Then Exit For
Waitms 50
Green
= Green * 2
Next I
Green
= 0
Loop Until Flashmode <> 1

End Select


Case 2
'All blue
Bluebrightness
= 0
Blue
= 31
Green
= 0
Red
= 0
'ramp up blue
For I = 1 To 200
Bluebrightness
= I
If Flashmode <> 2 Then Exit For
Waitms 10
Next I
Do
Loop Until Flashmode <> 2
Blue
= 0
Bluebrightness
= 200


Case 3
'All Red
Redbrightness
= 0
Red
= 31
Blue
= 0
Green
= 0
For I = 1 To 200
Redbrightness
= I
If Flashmode <> 3 Then Exit For
Waitms 10
Next I
Do
Loop Until Flashmode <> 3
Red
= 0
Redbrightness
= 200


Case 4
'All Green
Greenbrightness
= 0
Green
= 31
Blue
= 0
Red
= 0
For I = 1 To 200
Greenbrightness
= I
If Flashmode <> 4 Then Exit For
Waitms 10
Next I
Do
Loop Until Flashmode <> 4
Green
= 0
Greenbrightness
= 200

Case 5
'All Colors
Red
= 31
Green
= 31
Blue
= 31
Greenbrightness
= 0
Bluebrightness
= 0
Redbrightness
= 0
For I = 1 To 200
Bluebrightness
= I
Redbrightness
= I
Greenbrightness
= I
If Flashmode <> 5 Then Exit For
Waitms 10
Next I
Do
Loop Until Flashmode <> 5


Case 6
'Twinkle mode
'Each LED/Color will randomly blink momentarily

For I = 1 To 5
'Check Red
If Timeslice > Reddelay(i) Then
If Redflag(i) = False Then
Redflag
(i) = True
Reddelay
(i) = Reddelay(i) + Flash
Select Case I
Case 1
Red1
= True
Case 2
Red2
= True
Case 3
Red3
= True
Case 4
Red4
= True
Case 5
Red5
= True
End Select
Else
If Timeslice > Reddelay(i) Then
Redflag
(i) = False
Reddelay
(i) = Rnd(dark)
Reddelay
(i) = Reddelay(i) + Timeslice
Select Case I
Case 1
Red1
= False
Case 2
Red2
= False
Case 3
Red3
= False
Case 4
Red4
= False
Case 5
Red5
= False
End Select
End If
End If
End If

Next I

For I = 1 To 5
'Check blue
If Timeslice > Bluedelay(i) Then
If Blueflag(i) = False Then
Blueflag
(i) = True
Bluedelay
(i) = Bluedelay(i) + Flash
Select Case I
Case 1
Blue1
= True
Case 2
Blue2
= True
Case 3
Blue3
= True
Case 4
Blue4
= True
Case 5
Blue5
= True
End Select
Else
If Timeslice > Bluedelay(i) Then
Blueflag
(i) = False
Bluedelay
(i) = Rnd(dark)
Bluedelay
(i) = Bluedelay(i) + Timeslice
Select Case I
Case 1
Blue1
= False
Case 2
Blue2
= False
Case 3
Blue3
= False
Case 4
Blue4
= False
Case 5
Blue5
= False
End Select
End If
End If
End If

Next I
For I = 1 To 5
'Check Red
If Timeslice > Greendelay(i) Then
If Greenflag(i) = False Then
Greenflag
(i) = True
Greendelay
(i) = Greendelay(i) + Flash
Select Case I
Case 1
Green1
= True
Case 2
Green2
= True
Case 3
Green3
= True
Case 4
Green4
= True
Case 5
Green5
= True
End Select
Else
If Timeslice > Greendelay(i) Then
Greenflag
(i) = False
Greendelay
(i) = Rnd(dark)
Greendelay
(i) = Greendelay(i) + Timeslice
Select Case I
Case 1
Green1
= False
Case 2
Green2
= False
Case 3
Green3
= False
Case 4
Green4
= False
Case 5
Green5
= False
End Select
End If
End If
End If

Next I
End Select

Loop


End

Buttonpushed
:
'Interrupt triggered
Waitms 100 'Debounce the switch
Do
Waitms 100
If Pind.2 = 1 Then Exit Do 'Wait for person to release the switch

Loop

Incr Flashmode 'Change the flash mode variable
If Flashmode > 6 Then Flashmode = 0
Return



T0_int
:
'Interrupt Routine for timer used in twinkle mode
Incr Timeslice
Timer0 = 100

Return