Arduino Programming for Beginners | The Ultimate Guide

Last updated on March 23rd, 2024 at 11:25 am

If you are new to Arduino and have no prior knowledge of programming, then this guide on “ Arduino Programming for Beginners ” is for you only. In this post, you will learn everything you need to know about Arduino programming in order to build those mind-boggling projects.

This detailed guide is filled with examples so that you can grab the concepts more clearly. And three beginner-level Arduino projects are also given at the end of the article.

Before moving on to the Arduino programming section, let’s learn what Arduino is and why it is so popular.

What is Arduino?

Arduino is an Italy-based company that manufactures development boards called Arduino boards. These Arduino boards are used in many electronics projects and day-to-day applications around the world because programming an Arduino board is very easy. 

Arduino is one of the most popular Microcontroller-based Development boards on Earth. Since many Electronics hobbyists and students from all over the world use Arduino boards in their day-to-day projects. One of the key reasons for Arduino being so popular is that it is Open Source Software and Open Source Hardware. Also, programming an Arduino board is a piece of cake!

One of the factors that separate Arduino from all the other Microcontroller boards out there is its community of thousands of users. Queries, Projects, and questions are posted daily on the Internet by its community members. Checkout: Arduino forum

Why go for Arduino?

The main reasons for Arduino being so popular are:

A) Arduino is Open Source Software: You can alter the software or code according to your need or can design an entirely new environment for Arduino without any software copyright issue. 

B) Arduino is Open Source Hardware: You can change the Hardware circuitry of Arduino boards according to your need or can design an entirely new  Hardware system on Arduino without any Arduino Hardware copyright issue.

C) Large Community of Thousands of users.

This is answered best in Quora:  Why is Arduino so popular?

Types of Arduino Boards:

Since there are so many applications and areas where microcontroller boards like Arduino can be used, it is impossible to fit all features in one type of Arduino Board. Hence there are many types of Arduino boards available in the market according to application and user’s need.

But all of these boards can be programmed using just one software i.e, Arduino IDE. In other words, this guide on Arduino programming for beginners is for every Arduino board listed below. How cool is that!

Some of the popular Arduino Boards are listed below:

Arduino UNO

The Arduino Uno is an open-source microcontroller board that is based on the Microchip ATmega328P (for Arduino UNO R3) or Microchip ATmega4809 (for Arduino UNO WIFI R2) micro-controller by Atmel and was the first USB powered board developed by Arduino.

Arduino UNO
Arduino UNO

Buy now on Amazon:

India: https://amzn.to/2miGBMm

USA: https://amzn.to/2L8mY7M

Arduino MEGA

The Arduino Mega is an open-source development board that is developed by the Arduino company. It is based on the Microchip ATmega2560P by Atmel. The Atmega2560P is an 8-bit microcontroller that comes with a built-in bootloader, which makes it very convenient to flash the board with our code.

Arduino Mega
Arduino Mega

Buy now on Amazon:

India: https://amzn.to/2NdS9Mc

USA: https://amzn.to/2NQkLwg

Arduino Nano

The Arduino Nano development board was first released in 2008 by Arduino and is one of the most popular Arduino boards. Arduino Nano has the same functionality but is smaller in size than Arduino Uno. The other difference is that there is no DC power jack on Nano and is powered using a Mini-B USB cable instead of a standard one.

Arduino Nano
Arduino Nano

Buy now on Amazon:

India: https://amzn.to/2LfB0ER

USA: https://amzn.to/2meTA1v

Arduino Mini

The Arduino Pro Mini is a microcontroller board based on the microchip ATmega328. It comes in a very small form factor and is suitable for projects where space is an issue.

Arduino Mini
Arduino Mini

Buy now on Amazon:

India: https://amzn.to/2LjrHAe

USA: https://amzn.to/2umd1Ko

Arduino Lilypad

Arduino lilypad is the only board that has a circular shape. It is also more appealing than the other Arduino boards.

Arduino Lilypad
Arduino Lilypad

Buy now on Amazon:

India: https://amzn.to/2N9DHEU

USA: https://amzn.to/2NeQRRn

The simplest Arduino board for beginners: Arduino UNO

Among all types of Arduino Boards, Arduino UNO is the most popular one. It is the best option for Arduino beginners who are just starting out their journey in the field of Microcontrollers based Projects and Automation. Anyone can start making projects using Arduino UNO just after a few minutes of reading and practicing. This guide on Arduino programming for beginners is made just for that.

Arduino kits for Beginners:

There are many Arduino kits available for beginners in the Market. You can check out them later after reading this Guide.

As we have covered the basics of Arduino, let’s move on to the Arduino programming guide.

Arduino Programming for Beginners: Basics

What is Arduino IDE( Integrated Development Environment)?

As given in techtarget.com, the definition of IDE is:

 “An integrated development environment (IDE) is a software suite that consolidates the basic tools developers need to write and test software”

So in simple words, Arduino IDE or Arduino programming environment is a software/tool that helps us code and program Arduino Boards. Arduino IDE  is the basic interface of Arduino software that you see on your Laptop screen after installing it.

It contains all the tools like compiling and debugging required to upload and run programs on Arduino boards. So getting familiar with Arduino IDE is important if you are just starting your journey with Arduino

The interface of Arduino IDE or Arduino Programming environment:

Arduino IDE or Arduino Programming Environment

Basic overview of tools of the Arduino IDE:

Sketch: Individual programs that are uploaded to Arduino boards are called sketches

Tools of Arduino IDE
Verify 
Compile the code before uploading it. Use this to check for errors in your program
Upload 
As the name itself suggests, it uploads the code to Arduino. The program is automatically compiled before uploading it to the board.
New 
To create a new sketch or program
Open 
To see and open all the created sketches or Arduino Programs.
Save 
Save your Arduino programs or sketches using this.
Serial Monitor 
Use this to open the Serial Monitor. Serial Monitor is a kind of Monitor where you can print characters, numbers and can read and print the Sensor’s input by using a set of commands called Serial. Commands.

For additional commands check these Menus: FileEditSketchToolsHelp.

Note: To know Arduino IDE in detail you can check out this post on Arduino.cc itself:  Arduino Software (IDE)

Arduino Programming Language:

The structure of the programming language of Arduino IDE is based on C++. 

What are VOID SETUP and VOID LOOP?

Void Setup and Void Loop are the building blocks of any program. Every Arduino Program necessarily needs these two functions to work. In this post on Arduino programming for beginners, we are going to focus on these functions only.

Void Setup() :

1. Void means not returning any value. Void functions are called so because these functions do not take any input parameters whenever they are called.

2. The setup means setting the proper conditions for the initialization of a program.

3. Whenever the program is run, the compiler always looks for void Setup first of all.

4. Structure of Void setup function is : Void Setup() {   }

5. The initialization codes and variables( for ex: Initializing pin 13 as output pin ) are written inside { } parenthesis of Void Setup which runs only once.

6. Example:

void setup()
{
pinMode(13,OUTPUT);
// put your setup code here, to run once:
}

Void Loop() :

1. Void means not returning any value. Void functions are called so because these functions do not take any input parameters whenever they are called.

2. Loop means to run again and again.

3. The code inside the Loop function is executed again and again

4. Structure of Void Loop function is : Void Loop() {   }

5. The main code or execution part of the code( for ex: Switching LED connected to pin 13 On and Off with time difference of 1 Second ) is written inside { } parenthesis of Void Loop which runs again and again in loops.

6. Example:

void loop()
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
// put your main code here, to run repeatedly:
}

The Simplest Arduino Program:

void setup()
{
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
}
The simplest Arduino Program

Though this Arduino program performs nothing, it can be called a no-error Arduino program with no code inside void setup and void loop.

Comments in Arduino Programming :

A comment is added after a piece of code with the purpose of easy understanding of source code to Humans. OR So that it would be easier for you to understand and make changes after the program is written. It helps not only you but other persons also in knowing how the code is working. So if you are a beginner in Arduino programming then we recommend using comments in every program.

Comments are not read by the compiler i.e, the Compiler does not consider any comment a part of the code.

What are Single Line comments in Arduino Code?

Single-Line Comments: As the name itself suggests, this comment can be extended to only one line.

How to use Single Line Comments in Arduino Programming?

Before writing a single line comment:  use  ” // “

To end a Single Line comment: You don’t have to do anything but be careful, never continue comment in the next line before using ” // ” again in the next line

Examples of a single line comment:

Example A:

int x;
void setup() {
// put your setup code here, to run once( Yes this is a comment! )
}
void loop() {
x = 5; // This is a single line comment. Anything after the slashes is a comment
// to the end of the line
}

What are Multi-Line comments in Arduino Code?

Multi-Line Comment or Block Comment: As the name itself suggests, this comment can be extended to more than one line without using ” // ” again and again in every line.

How to use Multi-Line Comments in Arduino Programming?

Before starting a Multi-line comment:  use  ” /* “

To end a Single Line comment: Just end the Multi-line comment by typing ” */ “

Example of a Multi-line comment:

void setup()
{
pinMode(13,OUTPUT);
// put your setup code here, to run once(this is a single line comment)
}
void loop()
{
digitalWrite(13,HIGH); /* This is a Multi-line comment ending 
                                        here in this line,
                                        no in this line !  */
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}

Know more about comments and how to use them in Arduino programs in this blog post by Adafruit

Rule of using a semicolon(;) in Arduino Programming.

Since Arduino programming is C++ based, you need to add a semicolon after every command. If a semicolon is not added, the compiler will show an error. As stated in Arduino.cc  “If an impenetrable or seemingly illogical compiler error comes up, one of the first things to check is a missing semicolon, in the immediate vicinity, preceding the line at which the compiler complained

Variables and Datatypes in Arduino Programming:

Variable is like an empty box that may contain any number, character, or string. Datatypes are the types of data (for ex an integer and a string are datatypes) that you can store inside the variable.

Common Datatypes used and their representation in an Arduino Code:

1) Integer: int

Example: ” int x” // here int is an integer datatype and x is a variable

2) Float: Float

Example: ” float x” // here float is a float datatype and x is a variable

3) Long Datatype: long

Example: ” long value” // here long is a long datatype and value is a variable

The Most Commonly Used Commands or syntax in Arduino Programming:

1)  pinMode():

This function is used to define a digital or analog pin either as an input pin or output pin

Example: ” pinMode(13,OUTPUT) ” // sets the digital pin no. 13 as an output pin

” pinMode(5,INPUT) ” // sets the digital pin no. 5 as an input pin

2) delay() :

This is one of the most commonly used functions in Arduino programming as delays are required in almost every automation project.

delay means to pause the program from running for a specific time(which is specified by us). This time is set in milliseconds.

Example: ” delay(1000) ”  // pause the program for 1s or 1000ms

” delay(5000) ”  // pause the program for 5s or 5000ms

3)  digitalRead() :

This function is used to read digital inputs (i.e, 0 or 1) from the digital pins of Arduino.

Example: ” digitalRead(13) ” // Read digital input from pin 13

4) digitalWrite() :

This function is used to write digital output (i.e, 0 or 1) on the digital pins of Arduino.

Example A : ” digitalWrite(13,HIGH) ” //  Give output 1 or High on Digital pin 13

Example B : ” digitalWrite(13,LOW) ” //  Give output 1 or High on Digital pin 13

NOTE: HIGH means Logic 1 or 5 volts whereas LOW means Logic 0 or 0 volts

5)  analogRead() :

This function is used to read analog inputs (i.e, value between 0 and 1023 or 0 volts and 5 volts) from the analog pins of Arduino.

Example: ” analogRead(A0) ” // Read analog input from analog pin A0

NOTE A: analogRead function can be used only on Analog pins.

NOTE B: analog value 0 means 0 volts and analog value 1023 means 5 volts

6)  analogWrite() :

This function is used to write analog voltage value (i.e, any value between 0 and 255 or 0 volts and 5 volts) on the digital PWM pins of Arduino i.e, to give PWM output on PWM pins.

The analogWrite function is only applicable to particular digital pins of Arduino Boards called PWM pins. For example, PWM pins of Arduino UNO are 3, 5, 6, 9, 10, and 11.

NOTE A:  PWM value 0 means 0 volts and PWM value 255 means 5 volts. Similarly, PWM value 127 gives approximately 2.5 volts on PWM pins.

Example A: ” analogWrite(3,0) ” //  Gives output 0 volts to digital pin 3

Example B: ” analogWrite(3,255) ” //  Gives output 5 volts to digital pin 3

Example C: ” analogWrite(3,127) ” //  Gives output 2.5 volts to digital pin 3

NOTE B:  analogWrite() function is not related to analog pins or analogRead function in any way.

Still confused? Check out the official Arduino page on analogWrite function.

Now we will see some basic Arduino codes to brush up whatever we have learned so far!

Serial Commands in Arduino Programming:

Serial communication takes place between the Arduino board and Arduino IDE installed on a laptop. So to print any input data(sensor, keyboard) on Arduino IDE, serial commands are required.

What is Serial Monitor in Arduino IDE?

Serial Monitor is a type of display hub on which you can display and print analog inputs to Arduino from sensors or other devices.

1. “Serial.begin(9600)“: this command is used to initialize serial communication between Arduino and Arduino IDE. This command must be typed inside void setup() before using any other serial commands.

2. “Serial.print(variable whose value is to be printed)”: this command simply prints the required value onto the serial monitor. But remains on the same line. Let’s see an example to understand this more clearly:

Example:

void setup()
{
Serial.begin(9600); // begin serial communication
}
void loop() {
Serial.print("Hey");// print Hey on serial monitor
}

** This program prints “Hey” on the serial monitor

void setup()
{
Serial.begin(9600); // begin serial communication
}
void loop() {
Serial.print("Hey");// prints Hey on serial monitor
Serial.print("There");// prints There on serial monitor
}

** This program prints “HeyThere” on Serial Monitor

As you see even though the values to be printed uses the different print commands, both are printed one by one or side by side. To solve this error Serial.println() is used.

3. “Serial.println(variable whose value is to be printed)”: this command simply prints the required values on the next line onto the serial monitor. Let’s see an example to understand this more clearly:

void setup()
{
Serial.begin(9600); // begin serial communication
}
void loop()
{
Serial.println("Hey");// prints Hey on serial monitor
Serial.println("There");// prints There on serial monitor
}

**This program outputs: Hey

There

on Serial Monitor

Arduino Programming for beginners : Program 1

This one is the Arduino Blink Program to Blink internal LED connected to pin no. 13

You don’t need to make any connections as LED is internally connected through a resistor to Pin no. 13.

Program:

void setup()
{
pinMode(13,OUTPUT);  //set digital pin 13 as OUTPUT pin
}
void loop()
{
// this loop runs continuously i.e, LED Blinks with a time period of 2 seconds
digitalWrite(13,HIGH); // give output 1 or 5v on digital pin 13
delay(1000); // pause or delay this HIGH logic for 1 sec or LED is on for 1 sec
digitalWrite(13,LOW);// give output 0 or 0v on digital pin 13
delay(1000); // pause or delay this LOW logic for 1 sec or LED is off for 1 sec
}
DOWNLOAD THIS CODE

**Explanation of the code is given in the comments.

 Arduino Blink Program to Blink LED connected externally to pin no. 13:

Arduino Programming Example 1 Circuit
Arduino Programming Example 1 Circuit

1. Connect LED in series with 100-ohm resistor from pin 13 to GND.

2. The positive terminal of the LED is connected to pin 13 through the resistor and the negative terminal to GND pin.

PROGRAM: Since there are no changes in the output, the Arduino program is identical to previous one.

Arduino Programming from beginners : Program 2

Reading Analog Voltage Values using a potentiometer:

In this project we are going to take analog input from one of the analog pins using a potentiometer and print these analog values as well as the corresponding voltage value on the Serial monitor :

Circuit Diagram for Arduino Analog Input program :

Potentiometer connected to Arduino for Analog input
Potentiometer connected to Arduino for Analog input

Connect end terminals of the potentiometer to 5v and Gnd respectively whereas middle terminal to Analog pin A0.

Program:

int x; //define variable x as an integer
float y; //define variable y as a float
void setup()
{
pinMode(A0,INPUT); // Set pin A0 as Input pin
Serial.begin(9600);
}
void loop()
{
x = analogRead(A0); // analog value from the potentiometer
y = (5*x)/1023 ; // Converting this analog value into corressponding analog voltage using the given formula because 5v means 1023
Serial.print("PotValue= "); // giving a name to the value to be printed
Serial.print(x); // printing potentiometer analog input
Serial.print("\t"); // Next variable will be printed in next column
Serial.print(" AnalogVoltage= "); // giving a name to the value to be printed
Serial.print(y); // printing analog voltage
Serial.println(); // variable's value will be printed on next line or row
delay(500); // time difference bw successive print value
}
DOWNLOAD THIS CODE

**Explanation of the code is given in the comments.

 NOTE: The formula used here to convert the analog value into corresponding analog voltage is =  (5/1023) *x Analog value

1023(Max) analog corresponds to 5 volts(Max) which gives 1 analog value corresponds to (5/1023) volts.

Hence any random analog input value x corresponds to = (5/1023)*x volts

Working:

1. Voltage across end terminals of the potentiometer is 5 volts and between middle terminal i.e, analog output and Gnd terminal depends on knob’s position.

Arduino Programming basics: Printing the analog values on serial monitor
Printing the analog values on serial monitor

2. If knob’s position is at Gnd terminal then analog voltage will be 0 or analog value 0 i.e, Minimum

3. If knob’s position is at 5-volt terminal then analog voltage will be 5 volts or analog value 1023 i.e, Minimum

Arduino Programming for Beginners: Program 3

Controlling Brightness of an LED using potentiometer:

In this project we are going to take analog input from one of the analog pins using a potentiometer and print these analog values as well as corresponding voltage values on the Serial monitor :

Circuit Diagram for Arduino Analog Input program :

Circuit diagram of LED brightness control using potentiometer with Arduino
Circuit diagram of LED brightness control using potentiometer with Arduino

A) Connect end terminals of the potentiometer to 5v and Gnd respectively whereas middle terminal to Analog pin A0.

B)  Connect LED in series with 220-ohm resistor from PWM pin 9 to GND.

C)  Positive terminal of LED to PWM pin 9 through the resistor and negative terminal to GND pin.

NOTE: Since the brightness of the LED is to be controlled through PWM, the LED must be connected to a PWM pin only.

Program:

int x; //define variable x as an integer
int y; //define variable y as an integer
void setup() 
{ 
pinMode(A0,INPUT); // Set pin A0 as Input pin
pinMode(9,OUTPUT); //Set digital pin 9 as output pin
Serial.begin(9600);
}
void loop()
{
x = analogRead(A0); // analog value from the potentiometer
y = map(x,0,1023,0,255); // converting the input analog value x(between 0 to 1023) into a PWM value between 0 to 255. This is called Mapping
analogWrite(9,y); // Giving this mapped value as PWM value to PWM pin 9. Remember that maximum PWM value is 255 i.e, 5 volts
Serial.print("PotValue= "); // giving a name to the value to be printed
Serial.print(x); // printing potentiometer analog input
Serial.print("\t"); // Next variable will be printed in next column
Serial.print(" PWM Value= "); // giving a name to the value to be printed
Serial.print(y); // printing PWM value that corresponds to an input analog value from the potentiometer
Serial.println(); // variable's value will be printed on next line or row
delay(500); // time difference bw successive print value
}
DOWNLOAD THIS CODE

**Explanation of the code is given in the comments.

Working:

1. Voltage across end terminals of the potentiometer is 5 volts and between the middle terminal i.e, analog output and Gnd terminal depends on knob’s position.

2. If the knob’s position is at Gnd terminal then analog voltage will be 0 or analog value 0 i.e, Minimum. This is equals to 0 PWM value(using Map function). This PWM value is given to pin 9 and hence the brightness of LED is minimum.

3. If the knob’s position is at 5 volt terminal then analog voltage will be 5 volts or analog value 1023 i.e, Minimum. This is equal to 255 PWM value(using Map function). This PWM value is given to pin 9 and hence the brightness of LED is maximum. 

Photo of author

Ankit Negi

I am an electrical engineer by profession who loves to tinker with electronic devices and gadgets and have been doing so for over six years now. During this period, I have made many projects and helped thousands of students through my blog and YouTube videos. I am active on Linkedin.

2 thoughts on “Arduino Programming for Beginners | The Ultimate Guide”

Leave a Comment