Operators

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

  1. Right side is evaluated first.
  2. The resulting value is written to the left-side variable.
  3. Old variable value is replaced by the new value.
StatementBeforeAfter
x = 5;x unknown or old valuex becomes 5
x = x + 1;x = 5x becomes 6
total = x * 10;x = 6total 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 an if by 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 16

When 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

  1. blinkDelay = 500 sets the delay value.
  2. blinkCount = 0 starts a counter from zero.
  3. blinkCount = blinkCount + 1 updates the counter each cycle.
  4. totalDelay = blinkCount * blinkDelay stores calculation result.
  5. 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 == in if lines.
  • 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

  1. Change blinkDelay = 500 to blinkDelay = 300 and observe output.
  2. Add int totalCycles = 0; then assign totalCycles = blinkCount;.
  3. Write one wrong example using = inside if, then fix it to ==.
  4. 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.

Run in Simulator