RGB LED with Arduino for beautiful lighting

In this tutorial, we will be using an RGB LED with an Arduino. We have included an RGB LED our DIY kit which also has some awesome sensors.

The RGB LED

An RGB LED is a special type of LED that can emit the colours red, green and blue. The LED has four wires, one being GND and the rest being pins for making the RGB pins lighter or dimmer. Obviously, all of the colour pins will have to be connected to resistors. In the picture below, you can see which pins belong to which colour, the longest one being GND.

RGB LED pinout

Hooking up all of the wires

Now that you understand how a RGB LED works, we will connect it to the Arduino. We will need three 220 ohm resistors, the LED, an Arduino, some wires and a breadboard. We will be connecting each of the RGB pins to a PWM pin, so we can adjust the brightness and change the colour. Connect the resistors between the RGB pins and the Arduino pins and GND to GND.

 RGB LED with arduino

The code

The last step, is actually the easiest. We wil just need a few lines of code which are all pretty easy to understand. First we set the RGB pins as output, and then we use analogwrite to change the brightness of the red, green and blue pins.

void setup() {
// Set the pins as output
   pinMode(redPin,OUTPUT);
   pinMode(bluePin,OUTPUT);
   pinMode(greenPin, OUTPUT);
 }
 void loop() {

   analogWrite(redPin, 125);//Number between 0 and 256 for brightness
   analogWrite(greenPin, 8);
   analogWrite(bluePin, 255);

 delay(10);// Code loops every 10 miliseconds
 }

Just upload the code, and you should now see the RGB LED turn on. Obviously you can choose any number between 0 and 256 to change the colour.

Leave a Reply

Your email address will not be published. Required fields are marked *

[]