Text with LEDs. | https://beaconlamps.com/ |
Although some sites on the Internet show you that it is possible to connect an LED directly to the digital or analog outputs of Arduino this is a bad practice and later we will address this issue, so we recommend you read the contents of this post completely.
This post can be used on other platforms than Arduino, even with a microncontroller without any type of board.
Connecting an LED to Arduino is relatively simple thanks to the development and advances that the community has achieved over time. Thanks to the Arduino forum we can find a lot of useful codes for this purpose, however here we seek to help you understand how everything works from scratch and this post is considered by many to be the Hello World of microcontrollers.
In general, the LEDs should always be connected to a resistor even when the maximum current of the source is 20 mA. To understand the importance of resistance and to calculate its value, we recommend you read our post on this topic first, here.
The resistance is necessary because although our source supplies a maximum of 20 mA and our LED supports 20 mA it still has a given working range in voltage and if this is exceeded, the useful life of our LED will be drastically reduced. Basically the correct operation of a led and of any other electronic component depends on a correct combination of Intensity and Voltage (Balance).
Electric connection.
Once the resistance is calculated we can connect the LED without it being damaged or degrading over time. In the case of an Arduino UNO card which supplies us with 5V output and a white LED, the resistance value should be between 100 and 220 Ohms. Below we show you the electrical diagram to connect an LED to Arduino correctly.
Connection scheme. | Fritzing Software |
Code:
Although the Arduino IDE contains sample codes, these are a bit more elaborate in the case of an LED which uses at least one pushbutton which we have not explained at this time. So we will show you a suitable and proper example to be able to turn on the LED.
const int led = 3; // We defined where the led is.
void setup() {
pinMode(led , OUTPUT); // Pin 3 as output.
}
void loop(){
digitalWrite(led , HIGH); // We turn on the led.
delay(1000);
digitalWrite(led , LOW); // We turn off the led.
delay(1000);
}
As you can see the code is very simple because it only consists of 10 lines, its function is to turn on and off the LED every second, (1000 ms).
Initially we need to indicate in which pin our led will be connected in this case we use pin 3, later to be able to use the previous pin as an output we need to indicate it within the configuration of the program (void setup), as shown in the orange text (OUTPUT).
Finally to turn on or off the LED we need to indicate in the program in what state the pin (3) should be, and due we work with a digital output this should be indicated as LOW or HIGH, (0 and 1 in binary).
All the schemes used in this post were created on the Fritzing software. | http://fritzing.org/