# Arduino Led interfacing | Arduino Built in Led Interfacing

Arduino UNO has built in led available at pin no. 13. This built in led is very useful when you want to show some signal without connecting external led.
If you want to learn how to connect led to arduino. We can connect led externally by using the resistor in between the digital pin of arduino to the led. While connecting the led with the arduino make sure you know the resistor value required. We will also discuss how to calculate the led resistance. After finding the correct value resistor we can interface the led with arduino.

For a single led, we need one digital output from the arduino. We use the digital pin 13 for interfacing the led with arduino. We will connect the led to forward bias. Means LED’s anode to the pin no. 13 and cathode to the GND of the arduino.

When we are using external Led to the arduino board, we should connect the proper value resistor to the led to limit the current coming from the arduino uno pin. A led requires around 10-20mA of current but arduino pin gives around 40mA, so due to excess current led may damage. For protecting the led we need a current limiting resistor.

Hardware Required

Arduino Uno

• Led
• Resistor 1K ohm

## How to Calculate the Led Resistance

By using this formula we find the resistance value used across the led.

Led resistor value can be calculated by Ohm’s law:
R=(V s-V led)/I led
where:

• sis the source voltage, measured in volts (V),
• ledis the voltage drop across the LED, measured in volts (V),
• ledis the current through the LED, measured in Amperes (Amps/A), and
• R is the resistance, measured in Ohms (Ω).

Example:
s= 5V
led= 1.8V
led= 20 mA = 0.02A
R = 160 ohm
From the calculation, we get 160 ohms.
but roughly we use around 100 to 1 K ohm resistor. Only LED’s intensity will vary.

## Arduino LED Connection

In the circuit, external led is connected to the digital pin 13. It means two led is connected to the same pin. So both led will glow at the same time.

We can use some other pins also.

We can connect the led to any pin of arduino uno by simply changing led connection at desired pin and change in programming also.

## Arduino LED Code

In embedded world, if you can drive the led you can drive anything. Led interfacing sometime looks very simple but it involves some concepts.

Here we need to understand the function pin pinMode( ). Here led is an output device. Arduino will send an output signal to the led. But how to tell the arduino how to signal output or input.

To send the output, we make arduino pin 13 as output by using the pinMode(13,OUTPUT). This pinmode function has two arguments.

The first argument tells about the pin number where the led connected. So here digital pin 13 connected to the led.

The second argument tells how this pin will be used as an input or output. Here it is a led and we want a output at this pin, so make it as output.

By using the function pinMode(13, OUTPUT) doesn’t give any output or ON the led. It simply set the pin 13 as in output mode.

The actual output will appear at the digital pin 13 by the function digitalWrite( ). This function can give out HIGH or LOW depending on the parameter used in the function.

Here a high signal from the digital pin makes the led ON. So the function would be digitalWrite(13, HIGH). And if you want to OFF the led then give a low signal by digitalWrite(13, LOW).

The first argument tells about the pin number where the led connected. So here digital pin 13 connected to the led.

The second argument tells how this pin will be used as an input or output. Here it is a led and we want a output at this pin, so make it as output.

By using the function pinMode(13, OUTPUT) doesn’t give any output or ON the led. It simply set the pin 13 as in output mode.

The actual output will appear at the digital pin 13 by the function digitalWrite( ). This function can give out HIGH or LOW depending on the parameter used in the function.

Here a high signal from the digital pin makes the led ON. So the function would be digitalWrite(13, HIGH). And if you want to OFF the led then give a low signal by digitalWrite(13, LOW).

```void setup() {             // the setup function runs once when you press reset or power the board
pinMode(13, OUTPUT);     // initialize digital pin 13 as an output.
digitalWrite(13, HIGH);  // turn the LED on (HIGH is the voltage level)
}
void loop(){

}
```

For a beginner, it will be very interesting if they could blink the led.

Led blinking needs a loop in that we ON a led and OFF the led with a delay in between. The blink rate depends on the amount of the delay used.

We can change the led blinking delay using the arduino delay function.

```void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);   // turn the LED on
delay(1000);              // wait for a second
digitalWrite(13, LOW);    // turn the LED off
delay(1000);              // wait for a second
}
```

You should try this programs to see what happen with the led blinking.

Program 1:

```void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);   // turn the LED on
digitalWrite(13, LOW);    // turn the LED off
}
```

Program 2:

```void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);   // turn the LED on
digitalWrite(13, LOW);    // turn the LED off
delay(1000);              // wait for a second
}
```

Program 3:

```void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);   // turn the LED on
delay(1000);              // wait for a second
digitalWrite(13, LOW);    // turn the LED off
}​​​​
```