Embedded System Chapter 04: RGB LED PDF
Document Details
Uploaded by Deleted User
Dr. Abdulkadir Jeilani Mohamud
Tags
Summary
This document explains how to control an RGB LED using an embedded system based on an Arduino. The document covers the introduction, required components, component introduction, RGB LEDs, color theory, PWM, wiring diagrams, and the code examples for controlling the RGB LED. The document uses simple language to explain the technical concepts.
Full Transcript
Embedded System Chapter 04: RGB LED Dr. Abdulkadir Jeilani Mohamud Email: [email protected] / [email protected] Introduction RGB LEDs are a fun and easy way to add some color to your projects. Since they...
Embedded System Chapter 04: RGB LED Dr. Abdulkadir Jeilani Mohamud Email: [email protected] / [email protected] Introduction RGB LEDs are a fun and easy way to add some color to your projects. Since they are 3 regular LEDs in one, how to use and connect them is not much different. They come mostly in 2 versions: Common Anode or Common Cathode. Common Anode uses 5V on the common pin, while common Cathode connects to ground. As with any LED, we need to connect some resistors inline(3 total) so we can limit the current being drawn. In our sketch, we will start with the LED in the Red color state, then fade to Green, then fade to Blue and finally back to the Red color. Component required (1) X Elegoo Uno R3 (1) X 830 Tie Points BreadBoard (4) X M – M wires (Male to Male jumper wires) (1)X RGB LED (3) X 220 ohm resistors Component Introduction RGB: At the first glance, RGB(Red, Green and Blue) LEDs look just like regular LEDs. However, inside the usual LED package, there are actually three LEDs, one red, one green and yes, one blue. By controlling the brightness of each of the individual LEDs you can mix pretty much any color you want. We mix colors the same way you would mix paint on a palette – by adjusting the brightness of each of the three LEDs. RGB LEDs The RGB LED has four leads. There is one lead going to the positive connection of each of the single LEDs within the package and a single lead that is connected to all three negative sides of the LEDs. Color The reason that you can mix any color you like by varying the quantities of red, green and blue light is that your eye has three types of light receptor in it(red, green and blue). You eye and brain process the amounts of red, green and blue and convert It into a color of the spectrum. In a way, by using three LEDs, we are playing a trick on the eye. This same idea is used in TVs, where the LCD has red, green and blue color dots next to each other making up each pixel. Theory (PWM) Pulse Width Modulation(PWM) is a technique for controlling power. We also use it here to control the brightness of each of the LEDs. The length of this pulse is controlled by the ‘analogWrite’ function. So the ‘analogWrite(0)’ will not produce any pulse at all and ‘analogWrite(255)’ will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time. Wiring diagram Wiring diagram The code Our code will use FOR loops to cycle through the colors. The first FOR loop will go from RED to GREEN. The second FOR loop will go from GREEN to BLUE. The last FOR loop will go from BLUE to RED. Try the sketch out and then we will dissect it in some detail...... The sketch starts by specifying which pins are going to be used for each of the colors: [/code]// Define Pins #define RED 3 #define GREEN 5 #define BLUE 6 void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); digitalWrite(RED, HIGH); digitalWrite(GREEN, HIGH); digitalWrite(BLUE, HIGH); } void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); digitalWrite(RED, HIGH); digitalWrite(GREEN, LOW); digitalWrite(BLUE, LOW; } Before void loop function Int redValue; Int greenValue; Int blueValue; Void loop( ) { #define delayTime 10 // fading time between colors redValue = 255 // choose a value between 1 and 255 to change the color greenValue=0; blueValue=0; For( int i=0; i