Arduino LED blink Code

As we know that we can program an Arduino to do anything. Today we will see an example of Arduino code that turns an LED ‘on’ and ‘off’ and it is called Arduino LED blink code.  This is simplest program that we can do in Arduino. We can make any digital pin of Arduino to do this. Arduino board has an LED built in on board which is connected to pin 13. We can blink that LED without using any external LED. Then we will see how we can connect an external LED and blink that. But before let’s see the coding format of Arduino.

Arduino buying links

Amazon link for India Amazon link for other countries

Basics of Arduino programming

Arduino programming has a very simple structure. It has minimum two blocks which are following.

void setup()

{

————————

—Lines of code—-                                                                     

————————
}

void loop()

{

————————

—Lines of code—-

————————
}

These two blocks void setup and void loop are as you can see, has some lines of code enclosed in curly braces. These two blocks are compulsory in Arduino code even if one of them or both have no lines of code.

Code available in void setup block will run once and code available in void loop block will run indefinitely. We can say the void setup block as preparation and void loop block as execution. Because, here variables, pin mode, serial communication and many more configuration is initiated and it executes first.

Code available in void loop block is the main program that we want to run again and again. Let’s see what we generally put in the void setup function. We can define our own function outside both blocks and can execute it either in void setup or in void loop function.

Defining a pin as input or output in Arduino

Let’s see how we can define any digital I/O pin as input or output.

void setup()

{

pinMode(pin_number1,INPUT);

pinMode(pin_number2,OUTPUT);
}

Here we have declared the pin pin_number1 as input that means we will give it some input and pin_number2 as output that means we will take output from here. Pin_number1 and pin_number2 can be any digital I/O pin of Arduino. “;” is called terminator in programming language. It is used to separate the statements.

 Data types in Arduino

As other programming language Arduino also some data types which are listed below

Data type Size (in bits) (8bit = 1 byte) Range in decimal
void 0 Null
boolean (bool) 8 True or False
byte 8 0 to 255
char 8 -128 to +127
unsigned char 8 0 to 255
int 16 -32,768 to 32767
unsigned int 16 0 to 65,535
word 16 0 to 65,535
long 32 -2,147,483,648 to +2,147,483,648
Unsigned long 32 0 to 4,294,967,295
float 32 -3.4028235E+38 to 3.4028235E+38
double 32 -3.4028235E+38 to 3.4028235E+38
string Character array

Boolean (defined as bool)  is a variable type which can store one bit data (as 0 or 1) but takes 8 bit space in memory. Byte is a data type which can store 8 bit unsigned number which means it can store decimal number from 0 to 255. Char is a data type which store ASCII value of characters or indirectly we can say that it stores characters in it.

Defining Data types

Now let’s see how we can define a data type.

int Led_pin = 4;

void setup()

{

pinMode(Led_pin,OUTPUT);
}

Here we declared Led_pin as integer and assigned it 4. Now a 2 byte space is reversed from memory and it has label Led_pin. It has value ‘4’ in decimal and ‘0000000000000100’ in binary stored in it. In the void setup, we have declared Led_pin as output. That means we have declared pin 4 as output.  We can use ‘4’ instead of using Led_pin.

void setup()

{

pinMode(4,OUTPUT);
}

But in this code we have to change the pin number from all places wherever we have used this pin number. Suppose we have used a pin 10 times in code, so we have to change it 10 times. But in that code we have to change only one time. So it is always preferred to soft code instead of hard coding.

One other thing if you noticed that we have used an integer type of data to store the pin number. Since we have limited amount of I/O pins available in any Arduino board, which is not greater than even 100. So, we are just wasting the memory by declaring the pin as integer. This is ok for small codes, but in advance code where we need more memory, we can utilize memory by using right data types. You can use a particular data types for declaring pin number or number greater than that data type range.

Turning a pin high or low in Arduino

Now we will see how we can turn a pin high or low. We can do it in both blocks or in our custom functions.

int Led_pin = 4;  // assigning the Led_pin
void setup()
{
pinMode(Led_pin,OUTPUT);  // declaring Led_pin as output
}
void loop()
{
digitalWrite(Led_pin,HIGH);  // turning pin 4 high 
delay(100);   // one second delay
digitalWrite(Led_pin,LOW);  // turning pin 4 low
delay(100); // one second delay
}

Here, we have added some statements in the void loop block which are digitalWrite() and delay(). These function are responsible for LED to blink. digitalWrite() function pulls a digital pin of Arduino high or low. For this it takes two parameters: first is pin number and second is logic state. Delay(), as name suggest it is used to add some delay and then execute next line. For this delay takes a parameter which is time in milliseconds. Texts after “//” are called comments. These texts are not a part of code. These are used in documentation and compiler will ignore these.

Arduino LED blink code

To make an LED blink project we need following components.

  1. Arduino UNO R3 -1
  2. Breadboard – 1
  3. Male to male connector wire – 2
  4. LED -1
  5. 220Ω resistor -1

Place the LED on the breadboard then connect its anode of LED to Arduino’s pin number 4 through the 220Ω resistor. Connect cathode of LED to GND pin of Arduino using male to male connector wire. Connect Arduino board to computer using USB cable and upload the code given just above. Your LED will start blinking.

Now let’s quickly recap what we have done in code. We have first declares Led_pin as integer and assigned it with 4. Then in void setup block we declared Led_pin as OUTPUT. This will basically set the pin 4 as output. This code will run once. Then we have pulled the Led_pin high and wait for 1 second and then we have pulled the Led_pin to low and wait one second. What this will do is, it will turn pin 4, high and low at the interval of one second.

Arduino built-in LED blink code

As mentioned earlier almost all Arduino board has some LEDs on board, which are RX, TX and L in the case of Arduino UNO as you can see in the figure above. From which RX and TX indicates the serial transmission and reception. LED labeled ‘L’ is connected with pin 13 of Arduino UNO. We can turn it ‘on’ and ‘off’ by turning the pin number 13 ‘on’ and ‘off’. Or there exists a predefined function to controlling it called “LED_BUILTIN”. For this we don’t need any external LED. Let’s see how we can implement it on code.

void setup() {
// the setup function runs once when you press reset or power the board 
  pinMode(LED_BUILTIN, OUTPUT);   // initialize digital pin LED_BUILTIN as an output.
}
void loop() {
// the loop function runs over and over again forever
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Leave a Comment

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