### Introductory Course on Arduino

Getting Started
Arduino Basics
Loops in Arduino IDE

# Digital Output | How to control State of a LED?

In the previous topic, you learned about Arduino sketch and its flow. In this topic, we will learn about digital output and how to control a LED using Arduino IDE.

## Introduction

Digital Output: A digital output allows you to control a voltage with evive. If evive instructs the output to be high, the output will produce a voltage (generally about 5 or 3.3 volts). If evive instructs the output to be LOW, it is connected to ground and produces no voltage.

## Light-Emitting Diodes (LEDs)

An LED is a small device that glows when electricity passes through it. They are like tiny light bulbs but use less energy as compared to the conventional filament bulbs. They’re also more energy efficient. so they don’t tend to get hot.  LED bulbs and tube lights have picked up the front stage because they use less energy as compared to the conventional filament bulbs.

### Some concepts to keep in mind

1. Polarity Matters: Polarity is the property of having two poles that have opposite physical properties. In electronics, the two poles are: positive and negative. It decides the direction in which the charge in the wire/circuit will move. It indicates whether a circuit component is symmetric or not. LEDs allow current to flow only in one direction. Connecting them in reverse might blow them up. The positive side of the LED is the anode; it is generally the longer leg of the LED. The other, negative side of the LED is the cathode. The current always flows from the anode to the cathode. A reversed LED can keep an entire circuit from operating properly by blocking current flow.
2. More current equals more light: The brightness of an LED is directly proportional to how much current it draws. That means two things: first, super bright LEDs drain batteries more quickly, because the extra brightness comes from the extra power being used; second, you can control the brightness of an LED by controlling the amount of current through it.
3. …but too much current means ‘Boom!’: If you connect an LED directly to a current source it will try to use up as much power as it’s allowed to draw, and in the process, it might destroy itself. That’s why it’s important to limit the amount of current flowing across the LED. Resistors are used in circuits for this purpose to protect the LED from blowing itself up by drawing too much current.

Both evive and Arduino Uno has an LED (named Pin 13) whose positive terminal is connected internally to digital pin 13 and negative terminal is connected to GND i.e. ground. A resistor is present between the digital pin 13 and the positive terminal of LED to prevent excess current from flowing through the LED.

When digital pin 13 is HIGH, current passes through LED and it glows; when it is LOW, no current passes through LED. Hence, it does not glow.

## Configuring a Digital Pin as an Output Pin

In order to declare a digital pin as an output in evive that pin should be configured as OUTPUT using pinMode function available on Arduino IDE. This can be done using the following statement:

`pinMode(pin, OUTPUT);`

where the pin is the digital pin number you want to initialize as output.

The digital pins are in a low-impedance state, i.e. they can provide a substantial amount of current to other circuits. Atmega pins can source (provide positive current) or sink (provide negative current) up to 40 mA (milliamperes) of current to other devices/circuits. This current is enough to brightly light up an LED.

Alert: As mentioned earlier when the digital pin is used as output it is in low-impedance mode. Hence if any input is given to that pin in-spite of the fact that it is declared as output then due to low impedance there are possibilities that current higher than the capacity of evive may flow through that digital pin and may damage that pin forever.

### digitalWrite()

Using digitalWrite() function in Arduino IDE, you can write a digital pin, to a HIGH or LOW value.

If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V for HIGH, 0V for LOW.

## Example: Making a LED Blink

In this Arduino IDE sketch, we will turn ON the LED for 1 second, then turn it OFF for another 1 seconds and repeat, i.e. make it blink, same as the example code we run in the previous lesson.

As blinking is a repetitive process, the blinking statements will be written in the loop() function. However, the pin mode defining statement needs only once at the start of the program. Hence, it is written in setup() function.

Note: We are using delay(1000) for waiting for 1 second or 1000 milliseconds.

Below is the sketch: