Basic Arduino Programming – LED Blinking

Back to Blog
LED-Blinking

Basic Arduino Programming – LED Blinking

Arduino can be programmed using C and C++. Most of its library is written in C++. We will be using Arduino IDE to program it. There are few other IDE also available, but Arduino IDE is built by its creators. In this tutorial, we will see how to get started with Arduino Programming and what are the terms used in it.

Sketches:

sketch is a name that Arduino community uses for a program. It’s the unit of code that is uploaded to and runs on an Arduino board.

A basic Arduino sketch has the following two necessary parts.

void setup() {

}

void loop(){

}

void setup()

The setup function runs only once when the program is executed or reset. We will be assigning the pins which are connected to the external Hardware. In simple, you are informing the Arduino in which pins you have connected the components.

Example:

pinMode (13, OUTPUT); //Assigning pin 13 as Output

void loop()

In loop function, we will be writing the main code. This code will run repeatedly until the Arduino resets or power went off.

Note: Arduino Uno and other boards have power on reset function which will reset the Arduino while powering up and the code runs from the beginning.

Example:

void loop() {

digitalWrite(13, HIGH);  

delay(1000);             

digitalWrite(13, LOW);  

delay(1000);       

}

So combining setup and loop we will have the complete code like this.

Code 1: Single LED Blinking

void setup() 
{
pinMode (13, OUTPUT); //Assigning pin 13 as Output
}

void loop() 
{
digitalWrite(13, HIGH);  //Turn ON LED at pin 13
delay(1000);             //Delay for 1 second

digitalWrite(13, LOW);   //Turn OFF LED at pin 13
delay(1000);             //Delay for 1 second
}

Circuit diagram 1: Single LED Blinking:

1 LED Blinking_bb

Choose the correct Board and COM Port

Choose Board & COM V2

Upload the Code

Upload V2

BEST PRACTICE: Assigning Variables for setting up pins

There is another way to set up the pin using variables. This is the most convenient way for changing the code. Because if you want to change the pin, you just need to change the pin number at the declaration part (the variables) instead of changing it in entire code. All changes take place wherever you used those variables.

In this example, We connect RED LED to 9, Green LED to 10 and Blue LED to 11. This is the actual code and corresponding hardware connected.

Code2: 3 LED using Variables:

int redLED = 9;     //Using Variable redLED for Pin 9

int greenLED = 10;  //Using Variable greenLED for Pin 10

int blueLED = 11;   //Using Variable blueLED for Pin 11


void setup()            //Runs when program starts executing

{

  pinMode(redLED, OUTPUT);

  pinMode(greenLED, OUTPUT);

  pinMode(blueLED, OUTPUT); 

}

void loop()      //Runs repeatedly

{

digitalWrite(redLED,HIGH);

digitalWrite(greenLED,HIGH);

digitalWrite(blueLED,HIGH);

}

Circuit diagram: 3 LED using Variables:

3 LED_bbBut Now think of it we have connected the redLED at Pin 8 instead of 10. If you didn’t use a variable we might have to change it in two places. What will happen if you needed to change it in 30 Places? That’s where Variables comes into play.

Example:

int greenLED = 8; // Changing pin number from 10 to 8

Now wherever the variable greenLED is used it is routed to Pin 8.

Share this post

Leave a Reply

Back to Blog