How to drive an rgb led using three microcontroller pins. using pic microcontoller

The RGB LED contains three LEDs encased in one shell: Red, Green and Blue (some contain an extra blue led – as blue LEDs generate less output intensity (candela) per mA). It looks like a single white led except that it has four leads – one for the common ground connection and one for each led.

This project shows you how to produce PWM output without using the internal PWM hardware modules i.e. a softare PWM method.

How to drive an rgb led using three microcontroller pins.

Basic operation

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

RGB LED driver project details.

RGB LED Project
Compiler Mikroelectronika MikroC Compiler Free!
Target 16F877A (retargetable to other PICs that have an 3 output pins!).
Software level Medium.
Software notes Interrupt driven 3 output PWM.
Hardware level Easy.
Hardware notes No special notes
Project version 1.01
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 will get All the C source code and hex file.

Note: Check your email for the project code download link.

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 semi-transparent plastic.

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).

How it works.

This project uses puls 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 resoelution 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.

How to drive an rgb led using three microcontroller pins.

PWM Software

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.

Heart beat timer

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.


For more detail: How to drive an rgb led using three microcontroller pins.

Leave a Comment

= 5 + 3

Read previous post:
Wisp628 an in-circuit flash PICmicro Programmer

Features in-circuit programmer programs flash PICmicro's supports a rapid cycle of code/program/test prototype programmer takes power from the target circuit...

Scroll to top