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
- Evaluate left-side expression value.
- Evaluate right-side expression value.
- Compare both values.
- 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 == LOWchecks pressed state for INPUT_PULLUP.temperature == 30checks exact numeric match.current == previouschecks 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; // assignif (a = 10) { ... } // wrong in comparisons
// assigns 10 first, then condition often becomes trueUsing == 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
- Read the current button input into
buttonState. buttonState == LOWchecks if button is pressed (active-low input).- LED turns ON when equal condition is true; otherwise LED turns OFF.
buttonState == previousStatechecks whether state changed since last loop.- Store current value into
previousStatefor next comparison. 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
- CPU evaluates left and right operands.
- Comparator logic checks numeric equality.
- Boolean result is produced.
- Program flow follows if/else branch based on result.
- 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
currentStateandpreviousState. - Use Serial output to verify compare results while learning.
- Keep one comparison per line when debugging complex conditions.
Practice Task
- Print
buttonStateand confirm LOW/HIGH behavior with your button. - Add an
int modevariable and createif (mode == 1)logic. - Detect and print only when state changed using == and != together.
- 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.