This project shows you how to produce PWM output without using the internal PWM hardware modules i.e. a software PWM method.
The average current through each of the LEDs determines it's light output i.e. its contribution to the total output color. So by controlling the average current through each LED you can create almost any other color.
|RGB LED Output colors||Red, Green, Blue|
|Compiler||Mikroelectronika MikroC Compiler Free!|
|Target||16F877A (retargetable to other PICs that have an 3 output pins!).|
|Software notes||Interrupt driven 3 output PWM.|
|Hardware notes||No special notes|
|Project files||Enter your details to get the Download Link
and get the microcontroller newsletter:
(Your email is safe it will never
be sold or rented).
You can program the PIC in circuit through the ICSP connector.
By varying the current through each led you can create almost any other color but at close range you only see the individual colors of each LED.
To see the 'merged' color view it from a distance or put a
diffuser over it. I used a small piece of baking paper - which is transparent
enough to let the light through and opaque enough to diffuse the light from the
three LEDs. In a proper design you would use a diffused LED (roughed up!).
It all depends on what you want to do as the light output is reduced when using a diffused LED so you may want to put the RGB LED in a housing that does the diffusing e.g. a semi-transparent plastic housing, in which case you would use a non-diffused LED.
Note: Other projects in the web show the output without a diffuser I think the camera settings merge the light acting as a diffuser (they do - using a digital camera gives the same results i.e. It's not what your eye sees on the bench).
If you make a sign board using these LEDs you won't need a diffuser as your eye will not be able to distinguish the individual LEDs (if it's far enough away - it's exactly the same as a TV pixel).
This project uses pulse width modulation to drive each of the leds in the RGB led. By changing the duty cycle of each PWM signal you can control the average current flowing through each led creating any color you want. The limit is set by the resolution of the PWM (set at 256 steps per channel).
The project relies on persistence of vision to make it appear that the led is continuously driven (the PWM signals must be repeated quickly enough so that you do not see any flicker) at a rate greater than 50Hz (approx). Too slow and you begin to see the led flickering.
PIC micros only have one built in PWM module so you need to create three pulse width modulators in software.
This is fairly easy to do using one of the timers in the PIC micro and all that's needed is an 8 bit timer for an output resolution of 1/256.
Note: It's fairly difficult to make it work at 4MHz (it probably can be done in assembler or with more effort in 'C'). So I chose to use a 20MHz crystal just to make it easier.
This project uses timer 0 to create a heart beat timer that generates a regular interrupt. The pwm timer tick routine called here controls the PWM operation.
Note: You can use any timer to generate the heartbeat.
The other way to use a tri-color LED is without using PWM and treat each led line as fully on or off.
Using it in this way you get six color outputs: Red, Green, Blue, Magenta, Cyan and Yellow - without complex PWM control.
The hardware is simple with only three wires needed to drive the rgb led.
The source code consists of the following files:
pwm.h defines the prototypes for use in other files.
pwm.c defines a simple pulse width modulator - simplified in that each output uses the same time base. Two user access functions let you initialize the port and set the duty cycle:
Note that U8 is defined in types.h as a typedef so and is a short hand for an unsigned short (usually of 8 bits) hence Unsigned 8bit.
Channels are selected in _pwm_Duty from 0 to 2.
The main control for the PWM is _pwm_timer_tick(void) which must be called regularly from a timer interrupt. This calls the local file scope function action which then calls chan_action that does the PWM output. Note that a shadow register is used here to ensure the outputs are set correctly.
You would need to integrate the shadow register with other code if other code accesses the same port.
The resolution of the PWM is 256 bits and at each call to _pwm_timer_tick(void) each pwm channel is updated.
This function contains main which initializes the PWM on port C and enters an infinite loop.
The random number generator qrn seems more random than the built in one and it uses less memory. It is used in random_start to set a random duty cycle value for each channel and a random sweep direction.
In the infinite loop each channel is swept up and down using the current values from the arrays duty and dir. This produces a random color pulsating light output.
So that a new random sequence is started after a count of 300 cycles a new random sequence is started so you don't see the same thing over and over.
Jump from RGB led page
to Best Microcontroller Projects Home Page.
Arduino EEPROM: How to use it and How to presrve the life of EEPROM. Two examples sketches to save multiple values to EEPROM.
A tutorial on using the ADS1115 precision 16 bit ADC for low power use.
Learn how to use the TP4056 properly. There's a right, and a wrong way, to use it to safely charge Lithium Ion batteries.
The DW01A chip is a Lithium Ion battery protector commonly used on TP4056 boards. Find out Exactly how it works and how to use it the correct way.
For Arduino string operations you can use Object Class Strings or C style strings but which should you use? Also find out how to decode commands and control variables in your programs using strings.
Real Time Clock Design (FREE): A Free and Complete RTC design using the DS1307 and a PIC micro (16F88) also re-targetable. This PIC project uses an I2C Clock chip and 7-segment display to create a fou…