Arduino External LED Code Example
Table of Contents
Arduino External LEDs, Example:
In this example, we are going to control 2.5V and 10mA Leds. Controlling an external LED is just like controlling the onboard LED. Now, you might be thinking then why am I explaining this? Well, I want to explain how to use other IO pins, how to define and use multiple pins at the same time, and how to calculate the current limiting resistor.
The Arduino Uno and Arduino Nano both are 5V compatible controller boards. Which means 5V are available on its IO pins when turned ON. So, if you will directly connect 2.5V LEDs with the Arduino; they will burnout in a second.
So, how to control a 2.5V LED with a pin that has 5V. Well, We will have to use a current limiting resistor and for this we will have to perform some calculations. So, let’s do it.
The LED Voltage is 2.5V and the LED Current is 10mA.
Voltage on the Arduino Pin is 5V.
So using the V = IR formula, we can find the Resistor value which is 250 ohms.
Don’t select a resistor with a value less than 250 ohms. Go for a slightly bigger value this will increase the LED life span. The LED won’t heat up. I always select 330 ohm resistor it works perfectly.
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Led Interfacing with Arduino:
Connect the Cathode legs of the LEDs together and then connect it to the GND pin on the Arduino board. Connect the Anode legs of the LEDs to the digital Pins 2 and 3 on the Arduino through these 330ohms current limiting resistors. Now, let’s go ahead and take a look at the programming.
Arduino External Led Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* * Example 2: External Leds * https://www.electroniclinic.com/ */ int led1 = 2; int led2 = 3; void setup() { // put your setup code here, to run once: pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(led1, HIGH); delay(1000); digitalWrite(led2, LOW); delay(1000); digitalWrite(led2, HIGH); delay(1000); digitalWrite(led1, LOW); delay(1000); } |
This program is quite similar to the program used in example number1. The only difference is that this time I am using two LEDs connected to digital pins 2 and 3. Since, I am using the same color LEDs so I named them led1 and led2. And if I was using different color LEDs; for example Red and Green, then I could also call these LEDs as R_led and G_led. Anyway, the pins are defined.
In the void setup() function, I have set both the Pins as output, and in the void loop() function I am simply turning ON and turn OFF these LEDs. Let’s verify the code to check for any errors… You can see there are no errors and now we can upload the program.
For this go to the Tools Menu…then to board and select the Arduino Uno. Again go to the Tools menu and this time select the port, and finally, click on the Upload button. The two Leds will start blinking. For the step by step explanation and practical demonstration watch the video tutorial given at the end of this article.
video tutorial