Arduino Projects

Arduino Led Blinking Code Example

Arduino Led Blinking Example:

Arduino Led Blinking Code Example- In this example, we are going to control the Arduino’s onboard LED connected to the digital pin D13. Arduino Nano also has this LED connected to the same pin13. So, for this first example, you don’t need to connect an external LED. So, let’s go ahead and connect the Arduino board to the Laptop.




Amazon Links:

Arduino Nano USB-C Type (Recommended)

Other Tools and Components:

ESP32 WiFi + Bluetooth Module (Recommended)

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Arduino Led Blinking Code:

I have written three programs that have the same purpose of blinking the onboard LED. So, let’s start with the first program.

This is the simplest LED blinking code. No doubt it follows all the rules. Inside the setup() we tell the controller to set the digital pin13 as the output because LED is an output device. And then in the loop() function we turn it ON and OFF. Isn’t so rude? It feels like if you call someone for a certain job and when he arrives and you don’t even bother about asking his name or introducing him to the manager etc. And you just tell him start working.

The same thing is happening over here. You are not supposed to write a program like this. Now, you might be thinking why? Imagine you have used multiple LEDs for creating some cool animations and for some reason, you decide to change the pin numbers. However, those pins are used all over the code.

And what if those LEDs have different colors and you want to control a specific color LED. So, programs like these don’t make any sense. Let’s take a look at another program.

This is the same program, but this time I have properly defined the pin. I call it device. Now, if I want to change the pin number; I can change it over here. But, still this program is not good. You know this program is for blinking the onboard LED, because I told you. What if I ask someone what is the purpose of this code. His first question would be what is this device? Because, this device could be anything, it could be a relay, it could be a buzzer, or it could be a motor or something else.




So, what I am trying to explain is give it a meaningful name and add a little bit of information. Let’s take a look at another program and you will get the idea.

Your program should be like this because it’s easy to read this program. If I send it to someone who knows about Arduino programming; in just 5 seconds he will completely understand the purpose of this program because everything is well commented. So, let me explain this code and the rules.

Let’s start with comments. Comments are optional and it’s totally up to you whether you want to use them or not. But it’s a good programming practice to add comments in your programs because it makes your programs more understandable. We have two types of comments single-line comments and multi-line comments. The one on the top is a multi-line comment. To add a multi-line comment, you simple right the forward slash followed by an asterisk, and then write anything you want. To end a multi-line comment simply write the asterisk and forward slash. So, anything written in between these is completely ignored by the compiler.

To write a single-line comment simply put two forward slashes and start typing anything you want. It has nothing to do with the actual code.

While defining a pin number or any variable you need to follow some rules.

  1. No special characters are allowed in the beginning of a variable name.
  2. No numbers are allowed in the beginning.
  3. No spaces are allowed.
  4. You can use Underscore as the space.
  5. A variable name should be unique. You are not allowed to use a single name for multiple pins and you can’t define a single variable name with different data types. Think about yourself, your name is unique among your brothers and sisters. What if all had the same name? So, a variable name should be unique.
  6. It should be meaningful. As I am controlling an LED so I defined Pin 13 as LED. I could also write it as onboard_led. So, what I am trying to explain is select a name that is unique and meaningful.

So, after defining the pin, next, in the setup() function we will need to tell the controller whether we want to use it as an input for monitoring some kind of a sensor or as an output to control something. We know that LED is an output device so we are going to set it as output and for this we use the pinMode() function. This is the built-in function you can see it has a different color. You are not allowed to use a built-in function as a user-defined function. And you might know it’s a case sensitive language so the M should be capital, you have to use the built-in functions and all the keywords as it is. You are not allowed to change even a single character; otherwise it will generate an error.



The actual code which is supposed to run repeatedly is kept inside the loop() function. To turn ON/OFF any IO pin on the Arduino we use the digitalWrite() function. Its also a built-in function and it takes two arguments as the input the pin and the command HIGH or LOW.

Then there is a delay of 3 seconds. Then I have turned OFF the LED and again there is a delay of 1 second.

Now, to check for any errors click on the verify button, it will compile the code for you, if you see any error, fix it. “Watch video tutorial for step-by-step explanation“ While the Arduino Uno board is connected to the PC, go to the Tools Menu then to board and select your desired Arduino board, In my case I am going to select Arduino Uno. Again go to the Tools Menu and this time select the communication Port. Now, you can click on the Upload button.

Arduino course

The onboard LED will start blinking now you can try different delay values.

Related Articles

Leave a Reply

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

Back to top button