IOT & Robotics
September 5, 2026 7 min read

C/C++ Programming Basics for Arduino

Hs
Hemant singh
Technical Writer & Educator

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:

  1. Variables
  2. Data Types
  3. Operators
  4. if Statement
  5. else Statement
  6. else if Statement
  7. for Loop
  8. while Loop
  9. Functions
  10. Arrays
  11. Constants
  12. Pointers
  13. Structures

1. Variables

A variable is a named location in memory used to store a value.

We use variables to store information such as:

  1. Sensor readings
  2. Temperature
  3. Humidity
  4. Pin numbers
  5. Counters
  6. User input
  7. Motor speed
  8. Moisture values

Example


int moisture = 500;

Here:

int → data type

moisture → variable name

500 → value stored in the variable

We can change the value later:


moisture = 300;

Now the variable contains 300.

Example with Arduino


int sensorValue;

void setup() {
Serial.begin(9600);
}

void loop() {
sensorValue = analogRead(A0);

Serial.println(sensorValue);

delay(1000);
}

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.


int temperature = 25;

Examples:

0
10
25
100
500

float

Used for numbers that contain decimal values.


float temperature = 25.5;

Examples:

25.5
10.75
3.14

char

Used to store a single character.


char grade = 'A';

A character is written using single quotation marks.

String

Used to store text.


String message = "Hello Arduino";

boolean

Used to store either true or false.


bool pumpRunning = true;

byte

Used to store a small positive integer.


byte speed = 100;

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.

+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder

Example


int a = 10;
int b = 3;

int sum = a + b;
int difference = a - b;
int product = a * b;
int division = a / b;
int remainder = a % b;

Assignment Operator

The = operator assigns a value to a variable.


int x = 10;

This means:

Store 10 inside x.

We can also use:


x = 20;

Now x contains 20.

Comparison Operators

Comparison operators are used to compare two values.

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example:


if (temperature > 30) {
Serial.println("Hot");
}

Logical Operators

Logical operators are used to combine conditions.

&& AND
|| OR
! NOT

Example:


if (temperature > 30 && humidity < 50) {
Serial.println("Condition matched");
}

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


if (condition) {
// code
}

Example


int moisture = 250;

if (moisture < 300) {
Serial.println("Soil is wet");
}

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


int moisture = 500;

if (moisture < 300) {
Serial.println("Soil is wet");
}
else {
Serial.println("Soil is dry");
}

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


int moisture = 400;

if (moisture < 300) {
Serial.println("Very Wet");
}
else if (moisture < 500) {
Serial.println("Moist");
}
else {
Serial.println("Dry");
}

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


int temperature = 35;

if (temperature < 20) {
Serial.println("Cold");
}
else if (temperature < 30) {
Serial.println("Normal");
}
else {
Serial.println("Hot");
}

7. for Loop

A for loop is used when we want to repeat a block of code a specific number of times.

Syntax


for (initialization; condition; update) {
// code
}

Example


for (int i = 0; i < 5; i++) {
Serial.println(i);
}

Output:

0
1
2
3
4

The loop runs five times.

Understanding the Loop


int i = 0;

Start with 0.


i < 5;

Continue while i is less than 5.


i++;

Increase i by 1 after every iteration.

Arduino Example

Suppose we want to blink an LED five times:


for (int i = 0; i < 5; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);

digitalWrite(LED_BUILTIN, LOW);
delay(500);
}

8. while Loop

A while loop repeats code as long as a condition remains true.

Syntax


while (condition) {
// code
}

Example


int count = 0;

while (count < 5) {
Serial.println(count);
count++;
}

Output:

0
1
2
3
4

The loop continues as long as:

count < 5

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


void sayHello() {
Serial.println("Hello Arduino");
}

We can call the function:


sayHello();

Function with Parameters

A function can accept values called parameters.


void printNumber(int number) {
Serial.println(number);
}

We can call it like this:


printNumber(100);
printNumber(500);

Function with Return Value

A function can also return a value.


int addNumbers(int a, int b) {
return a + b;
}

We can use it:


int result = addNumbers(10, 20);

Now:

result = 30

Functions in Arduino

Arduino programs already use two important functions:


void setup()

and:


void loop()

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


int temperatures[5] = {20, 22, 25, 28, 30};

This array contains five values.

The positions are called indexes.

Important:

Array indexing starts from 0.

Therefore:

temperatures[0] = 20
temperatures[1] = 22
temperatures[2] = 25
temperatures[3] = 28
temperatures[4] = 30

Accessing an Array


Serial.println(temperatures[0]);

This prints:

20

Using a Loop with an Array


int values[5] = {10, 20, 30, 40, 50};

for (int i = 0; i < 5; i++) {
Serial.println(values[i]);
}

Arrays are useful when working with:

  1. Multiple sensor readings
  2. Multiple LED pins
  3. Multiple relay channels
  4. Lists of values
  5. 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:

  1. Pin numbers
  2. Threshold values
  3. Fixed configuration values
  4. Device settings

Using const


const int LED_PIN = 13;

The value of LED_PIN should not be changed later.

For example:


const int DRY_VALUE = 500;

We can use it:


if (moisture > DRY_VALUE) {
Serial.println("Soil is dry");
}

Why Use Constants?

Constants make programs easier to understand and maintain.

Instead of writing:


if (moisture > 500)

we can write:


if (moisture > DRY_VALUE)

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


int number = 100;

int *ptr = &number;

Here:

number stores the value:

100

&number means:

Address of number

ptr stores the address of number.

The * operator can be used to access the value stored at that address.


Serial.println(*ptr);

This prints:

100

Understanding the Symbols

&number

means:

Address of number

*ptr

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:

  1. Name
  2. Pin number
  3. Current value
  4. Status

Instead of storing these separately, we can create a structure.

Example


struct Sensor {
String name;
int pin;
int value;
bool active;
};

Now we can create a sensor object:


Sensor soilSensor;

We can assign values:


soilSensor.name = "Soil Sensor";
soilSensor.pin = 34;
soilSensor.value = 500;
soilSensor.active = true;

We can access the values:


Serial.println(soilSensor.name);
Serial.println(soilSensor.pin);
Serial.println(soilSensor.value);

Why Are Structures Useful?

Structures become very useful when an IoT project contains multiple sensors or devices.

For example:


struct Sensor {
String name;
int pin;
float value;
bool active;
};

We could use the same structure for:

  1. Temperature sensors
  2. Humidity sensors
  3. Soil moisture sensors
  4. Light sensors
  5. Pressure sensors