Lesson 29: Using = Assignment Arithmetic Operator
Learn how = stores values in variables, updates values over time, and differs from == in conditions.
Progress indicator
Lesson 29 of 57
Learning Objectives
- Understand what the assignment operator (=) does.
- Write correct assignment syntax in Arduino.
- Differentiate initialization and later assignment.
- Use assignment with expressions and updates.
- Avoid confusing = and == in conditions.
Concept Explanation
What is the Assignment Operator (=)
The = operator stores a value into a variable.
Think of it as putting a value into a labeled box in memory.
Assignment Operator Syntax
variableName = value;
variableName = expression;How = Works
- Right side is evaluated first.
- The resulting value is written to the left-side variable.
- Old variable value is replaced by the new value.
| Statement | Before | After |
|---|---|---|
x = 5; | x unknown or old value | x becomes 5 |
x = x + 1; | x = 5 | x becomes 6 |
total = x * 10; | x = 6 | total becomes 60 |
Variable Initialization vs Assignment
Initialization creates a variable and gives first value: int x = 5;
Assignment updates an existing variable: x = 10;
Updating Variable Values
You can keep changing values in loop() to track counters, delays, and states.
= vs == (Comparison)
=assigns a value.==checks whether two values are equal.- Using
=inside anifby mistake causes logic bugs.
Assignment with Expressions
int a = 5;
int b = 3;
int c = a + b; // c becomes 8
int d = c * 2; // d becomes 16When to Use =
- Store initial pin numbers, counters, and delays
- Update values after each loop iteration
- Save results from calculations and sensor reads
Example Code
This example uses assignment to track blink count and calculated total delay.
int blinkDelay = 500;
int blinkCount = 0;
int totalDelay = 0;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(115200);
}
void loop() {
digitalWrite(2, HIGH);
delay(blinkDelay);
digitalWrite(2, LOW);
delay(blinkDelay);
blinkCount = blinkCount + 1;
totalDelay = blinkCount * blinkDelay;
Serial.print("Count = ");
Serial.println(blinkCount);
Serial.print("Total delay = ");
Serial.println(totalDelay);
}Example Code Explanation
blinkDelay = 500sets the delay value.blinkCount = 0starts a counter from zero.blinkCount = blinkCount + 1updates the counter each cycle.totalDelay = blinkCount * blinkDelaystores calculation result.- Serial prints confirm updated values each loop.
Practical real-life view
Imagine a notebook where you update a daily step count. Assignment works the same way: you replace the old number with a new number every time data changes.
What Happens Inside
- CPU evaluates right-hand expression first.
- Result is written into variable memory location.
- Previous value is overwritten.
- Next statements use the updated variable value.
Internal flow rule
evaluate right side -> compute result -> store left variable -> continue next line
Memory concept (beginner)
Variables point to memory cells. Assignment writes a new value into that cell, so later lines read the updated value, not the old one.
Common Mistakes with =
- Using
=instead of==inside conditions. - Assigning values before variable declaration.
- Assuming assignment compares values (it does not).
- Using a variable before giving it a valid assigned value.
- Writing very complex expressions in one line and making debugging harder.
Best Practices for =
- Use clear variable names before assignment.
- Initialize variables with safe defaults.
- Double-check
=vs==iniflines. - Keep assignments simple and readable.
- Break long expressions into small assigned steps for easier debug.
- Print updated variables with Serial while learning program flow.
Practice Task
- Change
blinkDelay = 500toblinkDelay = 300and observe output. - Add
int totalCycles = 0;then assigntotalCycles = blinkCount;. - Write one wrong example using
=insideif, then fix it to==. - Create
int averageDelay = totalDelay / blinkCount;after count is greater than 0.
Try it now
Open the simulator workspace and observe variables while stepping through examples.