LED Example

We are going to go through a short example. In which we will turn on an RGB LED. For this example we will use the Arduino Mega.

RGBLED
The LED we will use is called an RGB led. It has four pins. Ground, Red, Green and Blue. We can turn on each of the individual colours seperatly or together. As we can turn them on at the same time and we can vary the brightness, we are then able to produce a whole spectrum of different coulours. This is the principle behind LED TV’s. This can be seen in the image above.

We are going to use Digital pins 8,9,10 for the RGB LED. The ground leg is simply wrapped around a 1K resistor which goes to the GND on the Arduino Mega.
rgbled-1
Thats all we need for the Hardware. Its very simple. The Ground Lead on the RGB LED is the longest one. Lets take a look at the circuit diagram.
rgbledsyma

The diagram above shows us what we have created. This is called a schematic. It looks more complicated than what we actually did. First of all we have the leads from pins 8,9 and 10. These go to three LEDS’s. These are the triangles in the image. This triangle with a line at one end, tells us which way the current is flowing. You will notice two little arrows above each LED. These tell us that they are emitting light. LED stands for Light Emitting Diode. There are lots of other types of diode, with variations on the symbol. The symbol reminds us of an arrow. There is a good reason for this. A Diode will only conduct in one direction. If we, was to turn it around. The LED would not light. This is a very important attribute in electronics called semiconducting.

The LED and Diodes in general have no internal resistance. This forces us to add some in the form of a 1 KOhm resistor. If we didn’t, the LED would conduct more and more until it reached a point of failure.

Now lets take a look at the Software :-

Here is the Arduino file.
I have added lots of comments, so it should be self explanatory. If not I will cover some bits here. lines in italics are code.

byte red = 8; // give our LED pins a familiar name
byte green = 9; // so we can use red,green,blue instead of the pin numbers.
byte blue = 10;

As we know, we are using pins 8.9 and 10 to control the LED. In the section of code above, we are defining some new names for the pins. We call these variables. We don’t need to do this but this way we can use red, green and blue instead of the numbers. This makes it easier to read and follow.

void setup() {
}

The above section is the setup function. We are not using it here but its important to make sure we have it. In further programs this will become very important. We use it to tell the Arduino what we want its various parts to do. So why don’t we use it ? The Arduino has default settings. It just so happens this sketch uses those settings. So we don’t have to change them. Most other Sketch’s will require something in setup.You will see curly braces like { } This tells the Arduino where the the setup function starts and finishs. They are also used for other blocks of code. A common mistake is to forget to add both.

void loop() {
 

This is the main loop of our program. Everything we put in here will be repeated forever.

for ( int loop = 0; loop <5; loop ++) // flash the three colours 5 times
{
analogWrite(red,127); // turn on Red LED
delay(500); // wait for 500 mili seconds ( half a second )
analogWrite(red,0); // turn off Red LED
analogWrite(green,127); // turn on Green LED
delay(500);
analogWrite(green,0); // turn off Green LED
analogWrite(blue,127); // turn on Blue LED
delay(500);
analogWrite(blue,0); // turn off Blue LED
}

This section of code is called a for loop. The first line tells the Arduino what to do. between the brackets are the details of the loop. The first int loop = 0 defines a new variable called loop and sets its value to 0. The int tells the Arduino that the new variable is an integer. We have to define variable types so the Arduino knows what will be stored in them. An int can only hold numbers between 0 and 65535. If your interested this is a 16 bit number. The second part is loop < 5 This tells the Arduino to keep going while the loop variable contains a number less than 5. The last section is loop ++ This tells the Arduino that every time it does a loop add one on to the loop variable. The curly braces { and } contains the code that the for loop should repeat. We have two commands here, the first is analogWrite(pin,value). We use this to control one of the analog pins. We can pass it a pin number and a value. The value must be between 0 and 255. 0 is off and 255 is fully on. analogWrite doesn’t actualy produce an analog value on the pin as you might expect. It produces a PWM ( pulse width modulated ) waveform. The next command is delay(length in miliseconds) I think this is understandable, It waits for a certain number of miliseconds before continuing.

// slowly turn the Red LED up to full brightness
for ( int loop=0;loop <250; loop++)
{
analogWrite(red,loop); // Red LED Value
delay(20);
}
analogWrite(red,0); // Shut the Red LED off
 
// slowly turn the Blue LED up to full brightness
for ( int loop=0;loop <250; loop++)
{
analogWrite(green,loop); // Green LED Value
delay(20);
}
analogWrite(green,0); // shut the Green LED off
// slowly turn the Blue LED up to full brightness
for ( int loop=0;loop <250; loop++) // colour mix/
{
analogWrite(blue,loop); // Blue LED Value
delay(20);
}
analogWrite(blue,0); // shut the blue LED off.

The above code contains three for loops. I wont explain these again. This time we are using it slightly differently. we are using it to slowly turn the brightness of each colour up. They start off and slowly grow brighter and brighter. At the end of the for loops we have an additional analogwrite command. This is there to turn the indicidual colour off again, Or it would affect the next one.

}

Finaly we have one last closing curly brace. This marks the end of the main loop. Its important ! it is also a common mistake to forget it. Remember every opening curly brace needs a closing one.

You will notice through this sketch there are descriptions of what it is doing. They all start with // This is special and tells the Arduino to ignore everything after them. It is just there for us. You might be thinking why do I need them ? Well they can be life savers. If the sketch doesn’t work the way we want. They can show us whats gone wrong. If we give our sketch to someone else, like I have done here. It will tell them what I am trying to do. If we come back to it in a couple of years, when we have forgotten even writing it. It will remind us. For simple sketch’s we may be able to work it out. As things get more complicated, a simple error can mean hours of trouble. I suggest you use them.

One last comment here, you will see above that the sketch code all appears in line. In the file you can download, it appears differently. The preferred way of doing it, is called indenting. This is the way its done in the download file. It makes the sketch much simpler to read and find errors. I wasn’t able to show it on this page they way I liked. This page is designed to work on computers, tablets and phones, pretty much any device that can get online. So I had to accept some limitations.

If you cant get hold of an RGB LED, you can use seperate Red, Green and blue or any colour you have. Just don’t forget they all need resistors ! When using standard LED’s you will need to be carefull which way around you put them. They have a flat edge near one of the pins. This is the pin that goes to ground.

I hope this has been of some help.

 

Comments are closed