Blink the LED
This is similar to "Hello World" that you write whenever you learn a new programming language.
In this post we shall see a few type of LED blinking projects. One is a single LED blink, then bicolor LED and lastly RGB LED.
Single LED blink
A LED is connected to port 11 using a current limiting resistor 220 Ω. The suitable resistor is between 220 ohm to 1 kΩ. This resistor is used to limit the current to the LED so that it does not get damaged by overcurrent.
Port 0 to 13 are digital ports of Arduino. Normally port 0 and 1 are used for serial communication so they are not used for LED. The digital ports of Arduino are bidirectional meaning that they can be configured as input or output. By default they are inputs. If we need to use it as output, then it need to be configured in the software. Lets look at the program that will blink the LED repeatedly.
//program to blink led repeatedly at interval of 1 sec.
int led = 11;
void setup()
{
pinMode ( led, OUTPUT); // port 11 as output
}
void loop()
{
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
delay(1000);
}
If the above task is modified to include printing out a statement in the serial monitor that the led is ON, the following changes to the program are needed.
int led = 11; // declare which port led is connected to
void setup()
{
pinMode ( led, OUTPUT); // port 11 as output
Serial.begin(9600); // set the baud rate for communication
}
void loop()
{
digitalWrite(led,HIGH);
Serial.println("LED is ON");
delay(1000);
digitalWrite(led,LOW);
Serial.println("LED is OFF");
delay(1000);
}
RGB Led
This type has three colored led built inside, red, green and blue. Hence the name. It can be of common cathode type or common anode type. If common anode as shown below, the common pin shall be connected to 5V. If we use common cathode type, the common pin shall be connected to ground (0V) and the code shall be altered accordingly.
Task: Blink the RGB led is the following sequence: R -> G and -> B at 500 ms interval and repeat.
int R =11;
int G =10;
int B=9;
void setup()
{
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
}
void loop()
{
// on the red led
digitalWrite(R,HIGH);
digitalWrite(G,LOW);
digitalWrite(B,LOW);
delay(500);
// on green led
digitalWrite(R,LOW;
digitalWrite(G,HIGH);
digitalWrite(B,LOW);
delay(500);
// on Blue led
digitalWrite(R,LOW;
digitalWrite(G,LOW);
digitalWrite(B,HIGH);
delay(500);
}
Bicolor LED
A bicolor LED component has 2 leds build into a component. Since the structure is two led in opposite direction, each leg should not be connected to any fixed voltages such as 5V or 0V because if we did that, then we cannot turn ON or OFF one of the LED. Hence both legs must be connected to a Arduino port.
For example if pin 1 is connected to 5V, red led can be turned on by setting pin 2 low or turned off by setting pin 2 to high. How about the green led. It is already in the reverse bias (off) mode. If pin 2 is set to low, the green led will be off and even if set to high, it will be off because both sides at 5V. No matter what logic level at pin 2, the green led will be off. So pin 1 and 2 shall be connected to two of the digital ports of Arduino.
Task : Turn on red led for 1 sec and then followed by green led for 1 sec and repeat. Serial monitor shall display which led is turned on.
int anodeR =11;
int anodeG =8;
void setup()
{
pinMode(anodeR, OUTPUT);
pinMode(anodeG, OUTPUT);
Serial.begin(9600); //task requires serial monitor
}
void loop()
{
// on the red led
digitalWrite(anodeR,HIGH);
digitalWrite(anodeG,LOW);
Serial.println("Red is ON");
Serial.println("Green is OFF");
delay(1000);
// on green led
digitalWrite(anodeR,LOW);
digitalWrite(anodeG,HIGH);
Serial.println("Red is OFF");
Serial.println("Green is ON");
delay(1000);
}
No comments:
Post a Comment