
In this tutorial we will flash two LEDs (red and green) with the Arduino board every two seconds.
A LED (Light-Emitting Diode),is an opto-electronic component capable of emitting light when it is traversed by an electric current.
An LED lets only the electric current pass in one direction, the forward direction (the opposite being the blocking direction): so that the current can pass, the cathode (“-“, short leg, flat side) must be connected on the ground side (GND). the anode (“+”, long leg) must be connected on the side of the digital port. So that the current passing through it is not too high (which would destroy it!), it must always be connected in series with an electrical resistance (typically from 200Ω to 1kΩ).
For the electronic assembly, we will need an Arduino Uno card, two LEDs, two resistors, a breadboard. This assembly will be connected to a computer via a USB cable for the power supply (5 Volts) and the transfer of the program to be installed on the microcontroller of the Arduino Uno board.
To achieve this assembly:
First We Connect the digital pin 0 of the Arduino board to the resistance leg.
Secondly,we connect the second anode resistance leg (terminal +) of the red LED.
Third we connect the red LED cathode (terminal -) to the Arduino GND.
Fourth we connect also the digital pin 1 of the Arduino board to the second resistor leg.
Then we connect the second anode resistance leg (terminal +) of the green LED.
Finally, the green LED cathode (terminal -) is connected to the Arduino GND.
Here is the program that allows to flash 2 LEDs.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void setup(){ pinMode(0,OUTPUT); //sets the number 0 digital pin of the Arduino in output mode pinMode(1,OUTPUT); //sets the number 1 digital pin of the Arduino in output mode } void loop(){ digitalWrite(0,HIGH); //red LED lights up digitalWrite(1,LOW); //green LED goes out delay(2000); digitalWrite(0,LOW); //red LED goes out digitalWrite(1,HIGH); //green LED lights up delay(2000); } |
Every Arduino program has 2 main functions, setup() and loop() that need to be declared.
Let’s pass the setup() function, which is only performed once during the program: when the Arduino starts. Here we will define the role of the pins: input or output. The function is called pinMode(). The pins connected to the LEDs are outputs.
The loop() function loops continuously after the execution of the setup() function.
In the loop() function:
digitalWrite(0,LOW) sets a logic 0 (LOW) level on the red LED pin which will turn off.
digitalWrite(1,HIGH) sets a logic 1 (HIGH) level on the green LED pin which will light up.
digitalWrite(0,HIGH) sets a logic 0 (LOW) level on the red LED pin which will turn up.
digitalWrite(1,LOW) sets a logic 1 (HIGH) level on the green LED pin which will light off.