Arduino LED Blink
1. What is a LED?
An LED
(light-emitting diode) is a semiconductor device that emits light when an
electric current is passed through it.
LEDs have
two main components: the anode and the cathode.
- The anode is the positive terminal of the LED and is typically the longer of the two leads.
- The cathode is the negative terminal of the LED and is typically the shorter of the two leads.
LED |
When an electric current flows through the LED in the correct direction (from the anode to the cathode), it causes the semiconductor material inside the LED to emit light.
LED ON |
2. Connecting
an LED to the Arduino UNO
Connecting an LED to the Arduino UNO is a simple process that involves connecting the positive leg of the LED (the anode) to a digital output pin on the Arduino board and the negative leg (the cathode) to a ground pin.
Connecting an LED to the Arduino |
To protect the LED from burning out, we need to use a resistor. The resistor can be placed:
- between the digital output pin and the anode (+).
- between the cathode (-) and GND.
The value of the resistor in series with the LED may be of a different value than 220 Ω; the LED will lit up also with values up to 1K Ω.
LED protected by a resistor |
The final diagram is as below:
Arduino diagram with led and resistor |
3. The
Arduino sketch
Once the LED is connected, it’s time to write some codes using the Arduino IDE(here).
Blink code |
In the Arduino sketch, use the pinMode() function to set the digital output pin as an OUTPUT. For example, if the LED is connected to digital pin 8, you would use the following sentence:
pinMode(8, OUTPUT);
Use the digitalWrite() function to send a HIGH or LOW signal to the digital output pin, in order to turn the LED on or off.
digitalWrite(8,
HIGH) // to turn the LED ON
digitalWrite(8, LOW) // to turn the LED OFF
Use the delay() function to control the blink rate of the LED. For example, to blink the LED every second, you would use the following code:
digitalWrite(8,
HIGH);
delay(1000);
digitalWrite(8,
LOW);
delay(1000);
The final code is as below:
void
setup() {
pinMode(8, OUTPUT);
}
void loop()
{
// turn the led ON
digitalWrite(8,
HIGH);
// wait for 1 second
delay(1000);
// turn the led OFF
digitalWrite(8,
LOW);
// wait for 1 second
delay(1000);
}
It's
important to note that this is just a basic example and that you can adjust the
pin number and delay time to suit your specific application.
Now, upload your code and enjoy 😁 (here).
Comments
Post a Comment