Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Blink a LED with Arduino

in Arduino Basics

One of the first projects made with Arduino is “how is to blink a LED”.  Many examples exist for blinking a Led with Arduino. Unfortunately, these tutorials do not have details about the coding and are very basic in their explanation. In this tutorial, I hope to help new users go into more detail and understand the code used for controlling a LED.

In this tutorial, we are using a resistor. It is not a problem if you are not familiar with resistors. As the name suggests, resistors resist the flow of electricity. The more it resists the less electric current will flow through it. 

So, let us look at our first real example. In the circuit that we are building, we will have a voltage supply connected to the Arduino Uno Board which is wired to a LED with a resistor.  There is also an example without the resistor in this tutorial.

A breadboard and wires are necessary for connecting the Arduino with the external components (LED).

BreadBoard Layout

In the diagram, you can see in the diagram that you need to connect the voltage supply to one leg of the resistor. The other leg of the resistor connects to the LED. Note that the LED is directional, meaning it has to connect to a certain orientation. The Cathode side is wired to the positive voltage. The Cathode is typically the longest of the two leads on the LED. If you put the LED upside down, it will not light up. The other leg of the LED needs to be connected to the negative terminal of the voltage supply. For this tutorial, we will supply the voltage from the Arduino microcontroller. That way, we can turn the LED on and off from a program.

As you can see, the LED is connected to both GND and pin 10. Between pin 10 and the LED is a 220-ohm resistor. This is necessary since the voltage signal of pin10 is higher than the LED can handle.

A lot of tutorials out there suggest using a 220-ohm resistor. This is not necessary, but 220 ohm is a common resistor that is included in starter kits. You can always use higher resistors with LEDs than the minimum, but that will have a declining effect on the LED’s brightness. The LED we are using has an average forward voltage of 2.2 and a current of 20mA. If we apply Ohm’s law of resistance, the following minimum resister value can be calculated.

5V (power source) – 2.2V (LED forward voltage) / 0.02A = 140 ohm

If you would like to learn more about ohm law, then look up the tutorial https://www.arduinoplatform.com/arduino-getting-started/understanding-basic-electronics-and-resistance-for-arduino/

The Code

/*
    Arduino Sketch that blinks an LED every second
*/


int pinLed = 10; // LED is attached to pin 10

void setup()
{
  // set pin 10 as an output pin
  pinMode(pinLed, OUTPUT);

  // turn the LED off at beginning
  digitalWrite(pinLed, LOW);

  //start serial connection
  Serial.begin(9600);
}

void loop()
{
  // turn the LED on
  digitalWrite(pinLed, HIGH);
  // wait for 1 second
  delay(1000);
  // turn the LED off
  digitalWrite(pinLed, LOW);
  // wait for 1 second
  delay(1000);
}

Code Explanation

Declare the variables: Firstly, we need to do is to declare a variable that holds the pin connected to the LED. We are using an integer variable for this. Int stands for integer and is discussed in the data type tutorial. It is wise to provide the variable with a simple and descriptive title. Therefore, we will call it pinLed.  

int pinLed = 10; // LED is attached to pin 10

Set the pinmode: The second step is to tell the Arduino Board that we want to use PIN 10 as an output pinWe want to define the pin as an output pin since we send the pin’s signal to turn the LED on / off.  If you wired everything according to the schematics, write the code correctly but forget to declare that the program will not work.

 // set pin 10 as an output pin
  pinMode(pinLed, OUTPUT);

Turn the LED off in the beginning: When the program starts, we want the LED to be off. Therefore, in the setup() part, we will use digitalWrite()to turn the LED off at the beginning. If we send a HIGH signal, the LED will turn on. If we send a LOW signal, the LED will turn off.

Remember, this is done in void setup() , which results in a one-time result.

  // turn the LED off at beginning
  digitalWrite(pinLed, LOW);

Begin Serial Communication: to send information from the Arduino Board the computer and vice versa. You will need to write code that makes that connection. Serial.begin() is the command for that.

//start serial connection
  Serial.begin(9600);

Turning the LED on: Finally, we arrived at our program’s void loop() section. As mentioned in the previous tutorial, we will use the Serial Monitor a lot. If the LED is turned on or off, we would like to see it at our Serial Monitor. This is done by the using Serial.printIn() function. In this tutorial, we will not use this function.

The next thing to do is turn the LED on by sending a HIGH signal to pin 10. This is done in the void loop()  part of the sketch. Keep in mind that the loop part will repeat itself until you shut down the power supply.

// turn the LED on
  digitalWrite(pinLed, HIGH);

Add a delay for the blinking effect: We will add a little delay() to see the blink effect that we would like to achieve. A delay is in milliseconds. 1000 milliseconds is 1 second.

// wait for 1 second
  delay(1000);

Turning the LED off: At last, the sketch consists of repeating the statements described above for turning the LED off. We will send a LOW signal to our output pin.

// turn the LED off
  digitalWrite(pinLed, LOW);
  // wait for 1 second
  delay(1000);

Congratulations, you have created your first sketch. Now it is time to move on to the next tutorial.

Previous Post
Understanding the Arduino Sketch / Syntax
Next Post
Understanding global and local variables for Arduino
You must be logged in to post a comment.
Menu