Learn to code

ARDUINO & CODES

Previous Post
Introduction into visual output with Arduino
Next Post
Adjust the brightness of an LED with PWM for Arduino

Connecting LEDs with Arduino

in Arduino Visual Output

Turning an LED on and off is easy to do with Arduino, and some of the tutorials in previous chapters have included this capability. This tutorial will provide some insights into choosing external LEDs. You will connect three LEDs and use a function to blink the LEDs.

Parts Required

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 7
220 ohm resistor 220 ohm resistor × 3
LED LED × 3

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Circuit Diagram

Three LEDs Arduino connected

Connect 3 LED with the longer lead, the anode, with three wires that go to pin 3,5 and 6. The short lead of the LEDs, the cathodes, are wired to the GND by connecting them with 220-ohmn resistors. 

The Code

LEDs sketch
  Blink three LEDs each connected to a different digital pin
*/
const int firstLedPin = 3; // choose the pin for each of the LEDs
const int secondLedPin = 5;
const int thirdLedPin = 6;
void setup()
{
  pinMode(firstLedPin, OUTPUT); // declare LED pins as output
  pinMode(secondLedPin, OUTPUT); // declare LED pins as output
  pinMode(thirdLedPin, OUTPUT); // declare LED pins as output
}
void loop()
{
  // flash each of the LEDs for 1000 milliseconds (1 second)
  blinkLED(firstLedPin, 1000);
  blinkLED(secondLedPin, 1000);
  blinkLED(thirdLedPin, 1000);
}

// blink the LED on the given pin for the duration in milliseconds
void blinkLED(int pin, int duration)
{
  digitalWrite(pin, HIGH); // turn LED on
  delay(duration);
  digitalWrite(pin, LOW); // turn LED off
  delay(duration);
}

 

Code Explanation

Because anode pins of the LEDs are connected to the Arduino pins and the cathodes are connected to the ground, the LEDs will light when the pin is HIGH and will be off when the pin is LOW. If you connected the anode and cathode the other way around the LEDs will light when the pin is LOW and will turn off if the pin is HIGH.

To be able to control the amount of current that will flow to the LEDs we can use resistors. We need to resist the current since LED’s normally cannot handle 5v of the Arduino.

To calculate the resistor value you need to know the input power supply voltage (Vs, usually 5 volts), the LED voltage (Vf), and the amount of current (I) that you want to flow through the LED. The formula for the resistance in ohms (known as Ohm’s law) is:

R = (Vs – Vf) / I

For example, driving an LED with a voltage of 1.8 volts with 15 mA of current using an input supply voltage of 5 volts would use the following values:

Vs = 5 (for a 5V Arduino board)
Vf = 1.8 (the voltage of the LED)
I = 0.015 (1 milliamp [mA] is one one-thousandth of an amp, so 15 mA is 0.015 amps)

The voltage across the LED when it is on (Vs – Vf) is: 5 – 1.8, which is 3.2 volts. Therefore, the calculation for the series resistor is: 3.2 / 0.015, which is 213 ohms. The value of 213 ohms is not a standard resistor value, so you can round this up to 220 ohms.

Create Global Variables

At the beginning of your program, you will need to declare the variables that hold the pins of the LEDs. These should be set to an output mode because you will be using digitalWrite() in your sketch.

const int firstLedPin = 3; // choose the pin for each of the LEDs 
const int secondLedPin = 5; 
const int thirdLedPin = 6;

 

Set the pinModes

Since you will want to light up an LED

pinMode(firstLedPin, OUTPUT); // declare LED pins as output
  pinMode(secondLedPin, OUTPUT); // declare LED pins as output
  pinMode(thirdLedPin, OUTPUT); // declare LED pins as output
Void loop() part

The void loop() part enables you to control the LEDs by using a funtion that is explained below. The LEDs will blink for 1 second (10000 milliseconds).

 

// flash each of the LEDs for 1000 milliseconds (1 second)
blinkLED(firstLedPin, 1000);
blinkLED(secondLedPin, 1000);
blinkLED(thirdLedPin, 1000);
Void blinkLED

Below is the function that is used to blink the LED. The function consists of two parameters. Int pin and int duration, which are send in the loop part to this function. In other words, int pin and int duration are replaced by the parameters written in the void loop code blinkLED(firstledPin, 1000). The function wil send a HIGH signal to the corresponding pin, wait for 1 second and then sends a LOW signal to the corresponding pin. This wil make the LED blink.

void blinkLED(int pin, int duration)
{
  digitalWrite(pin, HIGH); // turn LED on
  delay(duration);
  digitalWrite(pin, LOW); // turn LED off
  delay(duration);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu