📘 Lesson P4 – Operators in Arduino Programming
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what operators are
✅ Understand why operators are important
✅ Perform mathematical calculations
✅ Compare values using comparison operators
✅ Use logical operators for decision making
✅ Use assignment operators correctly
✅ Build logic for real-world Arduino projects
1. Introduction
Imagine you want Arduino to:
- Add two numbers
- Compare temperatures
- Check whether a button is pressed
- Control a motor based on distance
Can Arduino do this without calculations?
No.
Arduino needs special symbols to perform operations.
These symbols are called:
Operators
Operators help Arduino perform calculations and make decisions.
2. What is an Operator?
An operator is a special symbol that tells Arduino to perform a specific operation on data.
Example:
5 + 3
Here:
+
is an operator.
Result:
8
Real-Life Example
Suppose you buy:
- 2 notebooks
- 3 pens
To calculate total items:
2 + 3 = 5
The plus sign is performing an operation.
Similarly, Arduino uses operators to process data.
3. Types of Operators in Arduino
The most important operators are:
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Increment and Decrement Operators
4. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Meaning |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
Addition Operator (+)
Example:
int a = 10;
int b = 5;
int result = a + b;
Result:
15
Subtraction Operator (-)
Example:
int a = 10;
int b = 3;
int result = a - b;
Result:
7
Multiplication Operator (*)
Example:
int result = 5 * 4;
Result:
20
Division Operator (/)
Example:
int result = 20 / 4;
Result:
5
Important Note
5 / 2
Result:
2
because integers do not store decimal values.
For decimals use:
float result = 5.0 / 2.0;
Result:
2.5
Modulus Operator (%)
Returns the remainder.
Example:
10 % 3
Result:
1
Because:
10 ÷ 3 = 3 remainder 1
Practical Use of Modulus
Checking whether a number is even or odd.
Example:
8 % 2 = 0
Even number.
7 % 2 = 1
Odd number.
5. Assignment Operators
Assignment operators store values in variables.
Basic Assignment (=)
Example:
int age = 22;
Here:
=
assigns value 22 to age.
Addition Assignment (+=)
Example:
int score = 10;
score += 5;
Equivalent to:
score = score + 5;
Result:
15
Subtraction Assignment (-=)
Example:
score -= 3;
Equivalent to:
score = score - 3;
Multiplication Assignment (*=)
Example:
score *= 2;
Equivalent to:
score = score * 2;
Division Assignment (/=)
Example:
score /= 2;
Equivalent to:
score = score / 2;
6. Comparison Operators
Comparison operators compare two values.
Result is always:
true
or
false
| Operator | Meaning |
|---|---|
| == | Equal To |
| != | Not Equal To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
Equal To (==)
Example:
10 == 10
Result:
true
Not Equal To (!=)
Example:
10 != 5
Result:
true
Greater Than (>)
Example:
20 > 10
Result:
true
Less Than (<)
Example:
5 < 10
Result:
true
Greater Than or Equal To (>=)
Example:
10 >= 10
Result:
true
Less Than or Equal To (<=)
Example:
5 <= 10
Result:
true
Real Arduino Example
Temperature Monitoring System
if(temperature > 40)
{
// Turn ON Fan
}
Arduino checks:
Is temperature greater than 40?
If yes, fan turns ON.
7. Logical Operators
Logical operators combine multiple conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |
AND Operator (&&)
Both conditions must be true.
Example:
(age > 18 && age < 60)
Both conditions must be satisfied.
OR Operator (||)
At least one condition must be true.
Example:
(score > 90 || attendance > 95)
Only one condition is enough.
NOT Operator (!)
Reverses the result.
Example:
!true
Result:
false
Real Arduino Example
Automatic Door Lock
if(passwordCorrect && cardDetected)
{
// Open Door
}
Door opens only if both conditions are true.
8. Increment Operator (++)
Used to increase value by 1.
Example:
int count = 5;
count++;
Result:
6
Equivalent to:
count = count + 1;
Practical Uses
- Counting button presses
- Robot lap counting
- Visitor counters
9. Decrement Operator (–)
Used to decrease value by 1.
Example:
int count = 5;
count--;
Result:
4
Equivalent to:
count = count - 1;
Practical Uses
- Countdown timer
- Remaining attempts
- Inventory reduction
10. Operator Precedence
Arduino follows mathematical rules.
Example:
2 + 3 * 4
Result:
14
Because multiplication happens first.
Using Parentheses
(2 + 3) * 4
Result:
20
Always use parentheses when logic becomes complex.
11. Real-World Project Examples
Ultrasonic Distance Meter
if(distance < 10)
Object is close.
Gas Leakage Detector
if(gasValue > 500)
Gas leakage detected.
Water Tank Monitoring
if(level >= 90)
Tank full.
Visitor Counter
count++;
Increase visitor count.
12. Common Beginner Mistakes
Mistake 1
Using:
=
instead of:
==
Wrong:
if(temp = 30)
Correct:
if(temp == 30)
Mistake 2
Forgetting parentheses.
Mistake 3
Confusing AND and OR.
Mistake 4
Dividing integers and expecting decimal results.
13. Best Practices
✅ Use meaningful variable names
✅ Use parentheses for clarity
✅ Test conditions carefully
✅ Keep calculations simple
✅ Add comments for complex logic
📊 Summary
In this lesson, we learned:
✅ Arithmetic Operators
✅ Assignment Operators
✅ Comparison Operators
✅ Logical Operators
✅ Increment Operator
✅ Decrement Operator
Operators are the tools that allow Arduino to calculate values, compare conditions, and make decisions in real-world projects.
📖 Key Terms
Operator
A symbol that performs an operation.
Arithmetic Operator
Performs calculations.
Comparison Operator
Compares values.
Logical Operator
Combines conditions.
Assignment Operator
Stores values.
Increment
Increases value by one.
Decrement
Decreases value by one.
🎯 Quiz
1. Which operator performs addition?
A. –
B. +
C. *
D. /
✅ Answer: B
2. Which operator checks equality?
A. =
B. ==
C. !=
D. >=
✅ Answer: B
3. What does && represent?
A. OR
B. NOT
C. AND
D. EQUAL
✅ Answer: C
4. What is the result of:
10 % 3
A. 3
B. 0
C. 1
D. 10
✅ Answer: C
5. What does count++ do?
A. Decrease count
B. Increase count by 1
C. Multiply count
D. Divide count
✅ Answer: B
🏠 Assignment
Task 1
Create a table of all arithmetic operators with examples.
Task 2
Write five comparison operator examples.
Task 3
Write three logical operator examples.
Task 4
Create a program that calculates the area of a rectangle using variables and arithmetic operators.
Task 5
Create a simple temperature monitoring logic using comparison operators.