Operators

Lesson 38: Using == Equal To Comparison Operator

Learn how == compares values, returns true/false, and drives decision-making in Arduino conditions.

Progress indicator

Lesson 38 of 57

Learning Objectives

  • Understand what the equal-to operator (==) does.
  • Use == to compare variables and fixed values.
  • Understand == vs = and why they are different.
  • Use == safely inside if/else conditions.
  • Understand comparison result flow inside CPU decisions.
  • Avoid common beginner mistakes when comparing values.

Concept Explanation

What is the Equal To Operator (==)

The == operator compares two values and checks if they are equal.

If values are equal, result is true. If not equal, result is false.

Equal To Syntax

if (valueA == valueB) {
  // equal
}

bool same = (sensor == 1023);

How == Works

  1. Evaluate left-side expression value.
  2. Evaluate right-side expression value.
  3. Compare both values.
  4. Return boolean result (true/false).

That boolean result is then used by if/else to choose which code block runs.

Comparing Variables and Values

  • buttonState == LOW checks pressed state for INPUT_PULLUP.
  • temperature == 30 checks exact numeric match.
  • current == previous checks if two variables are equal.

== vs = (Comparison)

  • == compares values.
  • = assigns value to a variable.
  • Using = instead of == inside conditions is a common bug.
if (a == 10) { ... } // compare
a = 10;              // assign
if (a = 10) { ... } // wrong in comparisons
// assigns 10 first, then condition often becomes true

Using == in Conditions

In Arduino, == is frequently used in if statements to decide output behavior based on button input, sensor values, or state variables.

Real examples

  • if (buttonState == LOW) for INPUT_PULLUP button press checks.
  • if (mode == 2) for menu/state-machine mode selection.
  • if (current == previous) for no-change detection.

When to Use ==

  • Checking button states (HIGH/LOW).
  • Checking mode values and state variables.
  • Comparing counters with threshold values.
  • Detecting state changes against previous values.

Example Code

This example uses == to check button state and detect whether state changed.

const int LED_PIN = 2;
const int BUTTON_PIN = 0;

int buttonState = HIGH;
int previousState = HIGH;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == LOW) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Button pressed -> LED ON");
  } else {
    digitalWrite(LED_PIN, LOW);
    Serial.println("Button released -> LED OFF");
  }

  if (buttonState == previousState) {
    Serial.println("State unchanged");
  } else {
    Serial.println("State changed");
  }

  previousState = buttonState;
  delay(250);
}

Example Code Explanation

  1. Read the current button input into buttonState.
  2. buttonState == LOW checks if button is pressed (active-low input).
  3. LED turns ON when equal condition is true; otherwise LED turns OFF.
  4. buttonState == previousState checks whether state changed since last loop.
  5. Store current value into previousState for next comparison.
  6. delay(250) slows the loop so state transitions are easier to observe.

Real-life analogy

Think of a password check: only when entered value == stored value does access continue.

What Happens Inside

  1. CPU evaluates left and right operands.
  2. Comparator logic checks numeric equality.
  3. Boolean result is produced.
  4. Program flow follows if/else branch based on result.
  5. The loop repeats and re-runs comparison with updated input values.

Common Mistakes with ==

  • Using = instead of == in conditions.
  • Comparing wrong type/variable by mistake.
  • Forgetting active-low button logic with INPUT_PULLUP.
  • Comparing floating values for exact equality where tolerance is needed.

Best Practices for ==

  • Keep condition expressions simple and readable.
  • Use clear variable names like currentState and previousState.
  • Use Serial output to verify compare results while learning.
  • Keep one comparison per line when debugging complex conditions.

Practice Task

  1. Print buttonState and confirm LOW/HIGH behavior with your button.
  2. Add an int mode variable and create if (mode == 1) logic.
  3. Detect and print only when state changed using == and != together.
  4. Intentionally write if (mode = 1) and observe why it is wrong, then fix it.

Try it now

Open the simulator workspace and observe == comparison results step by step.

Run in Simulator