BY: NG YONG HAN
Ever seen one of those little lamps that changes different shades of colour slowly in a gift store? I’m sure you seen a lot of those already when you go shopping. In this tutorial you are going to learn how to make those lamps and you can even modify the colour changing sequence later.
Tools and components:
- SK28A w/ PIC16F1933 microcontroller
- UIC00B w/ UIC-A adapter
- One (1) RGB LED
- 3x 330 ohms resistors
- Some wires
Software required: MPLAB X 1.10, Hitech for PIC16F v9.83, PICKit 2 Programmer (should come with UIC00B)
The RGB LED is a special type of LED which can emit three coloured lights, which are the Red, Green and Blue, hence the RGB acronym. When one of these colours mixed together it will give you another new colour. By using the microcontroller’s PWM channels to drive the LED, you can control the intensity of each colour which can give you many shades of different colours.
According to the datasheet, the longest pin is the cathode of the LED, so be careful when you connect them to the microcontroller. On the other three pins you need to connect at least a 330 ohms resistors in between the microcontroller pins to limit the current.
Here is the schematic of the project and tutorial
On the PIC16F1933 microcontroller that is on your hand in the SK28A, it has three PWM channels. Setting these registers to enable its functionality may take some time and it is not the focus of the tutorial. Therefore, a function prepared in the sample program automatically deals with the PWM setting instantly:
Since it is operating on interrupts, you need to enable all of the interrupts after calling the function:
Why are interrupts used on this tutorial? It is because when the microcontroller is driving the PWM, we do not have to keep checking on the interrupt flags of the respective timers. (The Timers are connected to the PWM modules) The system only clears the flag when it is up. So the system can do other things like controlling the intensities of the RGB LED while waiting for the flag to be up.
A general mood lamp changes colour slowly by turning up/down the intensity of the LED step by step with a short delay in between. If you combine these behaviour you will see the lamp gets very bright, and then fade.
Well, it gets brighter slowly, brightest and then fades. That’s all. Looks a bit boring. How about slowly fades into another colour? Like red, then purple to blue?
You will see that while the intensity of the red light goes down, the intensity of the blue light goes up, causing a mixture of both colours to appear, and then turns back into blue. Now it looks more like a mood lamp to you.
In this program, three PWM channels are used to drive the colours of the RGB LED, which are:
The code example of the red which glows brighter, and then fades into green:
The value of the count1 is stored into the respective duty cycle register (CCPR3L, CCPR4L and CCP5L). When the value increases slowly, the intensity (or the duty cycle) increases too and vice versa. When you want the colour to fade, you just deduct 250 from count1 in the loop, as circled in red. In this example, the (A) code fragment causes the red to gets brighter, while in (B) the red slowly fades, and mixes with the green light, forming other colour. When the other colour diminish to green in (C), then it slowly fades back into no light. The code for the (C) is left out for your exercise.
Due to the PWM’s low frequency (1KHz) and using an internal oscillator (1MHz) for low power operation, the maximum duty cycle value for this program is only 250.
You can modify the delay (*) in the loop too if you want them to fade faster or slower.
There are many combinations of fading into different shades of colour which is totally impossible to cover here in this tutorial – experiment it yourself! The source code project is attached and it is using MPLAB X.