Learn to code

ARDUINO & CODES

Previous Post
Understanding the Arduino Sketch / Syntax
Next Post
Understanding global and local variables for Arduino

Blink a LED with Arduino

in Arduino Basics

One of the first projects that you can make with an Arduino is to blink a LED. Many examples exist for blinking a Led with Arduino. Unfortunately, these tutorials do not go in detail about the coding and are very basic in their explanation. In this tutorial, I hope to help new users to go in more detail and to understand the code that is used for controlling a LED. We will make use of some syntaxes explained in the previous tutorial: Understanding the Arduino Sketch / Syntax

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 Arduino which is connected to a LED through a resistor. A breadboard and wiring are necessary for connecting the Arduino with the external components (LED).

BreadBoard Layout

Circuit Diagram – 

You can see in the diagram below that you need to connect the voltage supply to one leg of the resistor. The other leg of the resistor is connected to the LED. Note that the LED is directional, meaning it has to be connected to a certain orientation. You must connect the Cathode to the positive voltage. The Cathode is typically the longest of the two leads on the LED. If you put the LED in 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 our 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.

The Code

int pinLed = 10; // LED is attached to pin 10
 
void setup() 
{
    
    pinMode(pinLed, OUTPUT);  // set pin 10 as an output pin
    digitalWrite(pinLed,LOW); // turn the LED off
}
 
void loop()
{
    
    digitalWrite(pinLed, HIGH); // turn the LED on
    delay(1000); // wait for 1 second
    digitalWrite(pinLed, LOW); // turn the LED off
    delay(1000); // wait for 1 second
}

 

Code Explanation

Declare a variable

Firstly, we need to do is to declare a variable that holds the pin connected to the LED. This is done by the int command. We will also give the variable a name. It is wise to give the variable a simple and descriptive name. Therefore, we will call it pinLed.  In the tutorial, Arduino Tutorial: 2.2 Understanding global and local variables, we will go in further detail about Global and Local Variables.

int pinLed = 10;

Set the ledPin as output pin

The second step is to tell the Arduino Board that we want to use PIN 10 as an output pin. We want to define the pin to be an output pin since we are sending a signal to the pin to turn the LED on / off.  If you wired everything according to the schematics, written the code correctly but forget to declare the pin the program will not work.

pinMode(pinLed, OUTPUT);

Turn LED off at the beginning

When the program is started 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.

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

Void loop() turning on the LED

Finally, we arrived at the void loop() section of our program. 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. Tutorial, Arduino Tutorial: 3.1 Sending information to the serial monitor using serial print(), will cover this function.

The next thing to do is to 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.

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

Add a delay

We are going to add a little delay() to see the blink effect that we would like to achieve. A delay is measured in milliseconds. 1000 milliseconds is 1 second.

delay(1000);

Turning the LED off for blinking effect

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

digitalWrite(pin_LED, LOW); 
delay(1000);

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

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