Arduino programming is based on C/C++. Before creating more advanced Arduino and IoT projects, it is important to understand the basic programming concepts.
In this tutorial, we will learn:
- Variables
- Data Types
- Operators
- if Statement
- else Statement
- else if Statement
- for Loop
- while Loop
- Functions
- Arrays
- Constants
- Pointers
- Structures
1. Variables
A variable is a named location in memory used to store a value.
We use variables to store information such as:
- Sensor readings
- Temperature
- Humidity
- Pin numbers
- Counters
- User input
- Motor speed
- Moisture values
Example
Here:
int → data type
moisture → variable name
500 → value stored in the variable
We can change the value later:
Now the variable contains 300.
Example with Arduino
Here, sensorValue stores the value read from the analog sensor.
2. Data Types
A data type tells the program what kind of value a variable will store.
Different types use different amounts of memory and can store different ranges of values.
Common Data Types in Arduino
int
Used for whole numbers.
Examples:
float
Used for numbers that contain decimal values.
Examples:
char
Used to store a single character.
A character is written using single quotation marks.
String
Used to store text.
boolean
Used to store either true or false.
byte
Used to store a small positive integer.
Common Data Types
Data Type | Example | Purpose |
|---|---|---|
int | 500 | Whole numbers |
float | 25.5 | Decimal numbers |
char | 'A' | Single character |
String | "Hello" | Text |
bool | true | True or false |
byte | 100 | Small positive number |
For Arduino programming, you will use int, float, char, String, and bool very frequently.
3. Operators
Operators are symbols used to perform calculations, comparisons, and logical operations.
There are several types of operators.
Arithmetic Operators
These are used for mathematical calculations.
Example
Assignment Operator
The = operator assigns a value to a variable.
This means:
Store 10 inside x.
We can also use:
Now x contains 20.
Comparison Operators
Comparison operators are used to compare two values.
Example:
Logical Operators
Logical operators are used to combine conditions.
Example:
This condition is true only when both conditions are true.
4. if Statement
The if statement is used to make a decision.
It executes code only when a condition is true.
Syntax
Example
If the condition is true, the message will be printed.
In IoT, if statements are extremely important because they allow the microcontroller to make decisions based on sensor values.
5. else Statement
The else statement is used when the if condition is false.
Example
If moisture is less than 300, the first message is printed.
Otherwise, the second message is printed.
6. else if Statement
The else if statement allows us to check multiple conditions.
Example
The program checks the conditions from top to bottom.
If the first condition is false, it checks the next condition.
If all conditions are false, the else block runs.
Example for Temperature
7. for Loop
A for loop is used when we want to repeat a block of code a specific number of times.
Syntax
Example
Output:
The loop runs five times.
Understanding the Loop
Start with 0.
Continue while i is less than 5.
Increase i by 1 after every iteration.
Arduino Example
Suppose we want to blink an LED five times:
8. while Loop
A while loop repeats code as long as a condition remains true.
Syntax
Example
Output:
The loop continues as long as:
Important Point
Make sure the condition eventually becomes false.
Otherwise, the loop may continue forever.
9. Functions
A function is a reusable block of code designed to perform a specific task.
Functions help us organize programs and avoid repeating the same code.
Example
We can call the function:
Function with Parameters
A function can accept values called parameters.
We can call it like this:
Function with Return Value
A function can also return a value.
We can use it:
Now:
Functions in Arduino
Arduino programs already use two important functions:
and:
setup() runs once when the Arduino starts.
loop() runs repeatedly as long as the Arduino is running.
10. Arrays
An array is used to store multiple values of the same data type under one variable name.
Example
This array contains five values.
The positions are called indexes.
Important:
Array indexing starts from 0.
Therefore:
Accessing an Array
This prints:
Using a Loop with an Array
Arrays are useful when working with:
- Multiple sensor readings
- Multiple LED pins
- Multiple relay channels
- Lists of values
- Stored configuration data
11. Constants
A constant is a value that should not be changed during the program.
Constants are useful for values such as:
- Pin numbers
- Threshold values
- Fixed configuration values
- Device settings
Using const
The value of LED_PIN should not be changed later.
For example:
We can use it:
Why Use Constants?
Constants make programs easier to understand and maintain.
Instead of writing:
we can write:
The second version makes the purpose of the number much clearer.
12. Pointers
A pointer is a variable that stores the memory address of another variable.
Pointers are an advanced C/C++ concept.
They are not required for basic Arduino programs, but understanding them becomes useful when working with more advanced C++ programming, libraries, memory management, and embedded systems.
Example
Here:
number stores the value:
&number means:
Address of number
ptr stores the address of number.
The * operator can be used to access the value stored at that address.
This prints:
Understanding the Symbols
means:
Address of number
means:
Value stored at the address contained in ptr
Pointers are particularly important when learning advanced C/C++ and embedded programming.
13. Structure
A structure, commonly called a struct, allows us to group different types of related data together.
For example, suppose we want to store information about a sensor.
A sensor might have:
- Name
- Pin number
- Current value
- Status
Instead of storing these separately, we can create a structure.
Example
Now we can create a sensor object:
We can assign values:
We can access the values:
Why Are Structures Useful?
Structures become very useful when an IoT project contains multiple sensors or devices.
For example:
We could use the same structure for:
- Temperature sensors
- Humidity sensors
- Soil moisture sensors
- Light sensors
- Pressure sensors