Lesson 41: Using < Less Than Comparison Operator
Learn how < checks strict lower-than conditions and helps you trigger actions before a value reaches a limit.
Progress indicator
Lesson 41 of 57
Learning Objectives
- Understand what the less-than operator (<) checks.
- Use < for strict upper-limit comparisons.
- Compare < with <= and choose the correct one.
- Apply < in if/else conditions with sensor values.
- Predict branch behavior at edge values (below, equal, above).
- Avoid common comparison mistakes in Arduino code.
Concept Explanation
What is the Less Than Operator (<)
The < operator checks if the left value is strictly smaller than the right value.
If both values are equal, the result is false because < does not include equality.
Less Than Syntax
if (valueA < valueB) {
// runs only when valueA is smaller than valueB
}How < Works
- Evaluate the left and right expressions.
- Compare their numeric values.
- Return true only if left is smaller than right.
- Return false when left is equal to or greater than right.
Comparing Variables and Values
temperatureC < minTempfor cold alerts.batteryPercent < 20for low-battery warnings.sensorValue < LIMITfor under-threshold actions.
< vs <= (Comparison)
<means strictly smaller.<=means smaller or equal.
| Expression | Value = 1200 | Behavior |
|---|---|---|
value < 1200 | false | Only 1199 and below pass |
value <= 1200 | true | 1200 and below pass |
Using < in Conditions
Use < when your logic should run only before a limit is reached. This is useful in warning logic and pre-limit control behavior.
Real embedded examples
if (tankLevel < 10): show low-water warning.if (batteryPercent < 15): reduce power usage mode.if (lightValue < darkLimit): turn hallway LED ON.
When to Use <
- Low threshold checks (below minimum).
- Pre-limit warnings before reaching a boundary.
- Strict comparisons where equal should not pass.
Edge-Value Behavior (Important)
Most logic bugs happen at boundary values. Always test three cases: just below limit, exactly at limit, and just above limit.
| sensorValue | Condition | Result |
|---|---|---|
| 1199 | 1199 < 1200 | true (if-branch) |
| 1200 | 1200 < 1200 | false (else-branch) |
| 1201 | 1201 < 1200 | false (else-branch) |
Example Code
This sketch turns LED ON only when the sensor value is below the limit.
const int LED_PIN = 2;
const int LIMIT = 1200;
int sensorValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
sensorValue = analogRead(A0);
if (sensorValue < LIMIT) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Value is below limit -> LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("Value reached or exceeded limit -> LED OFF");
}
Serial.print("sensorValue = ");
Serial.println(sensorValue);
delay(300);
}Example Code Explanation
const int LIMIT = 1200;defines the strict boundary for low-value behavior.sensorValue = analogRead(A0);gets the latest analog reading each loop.if (sensorValue < LIMIT)checks only values lower than 1200.- If true, LED turns ON and Serial prints a below-limit message.
- If false (equal or greater), LED turns OFF and Serial prints the else message.
Serial.print+Serial.printlnoutput live values so you can verify branch decisions.delay(300)slows updates to keep transitions readable in simulator/monitor.
Real-life analogy
Think of a speed warning set at 60. Rule: warn only when speed is less than 60 for slow-speed guidance. At exactly 60, condition speed < 60 is false.
What Happens Inside
- Both comparison operands are evaluated.
- The MCU comparison unit checks whether left is strictly lower than right.
- A boolean result is produced (true/false).
- The compiler-generated branch instruction chooses if or else block.
- GPIO write updates LED state to reflect the selected branch.
- Serial output confirms runtime decision for debugging.
- The loop repeats and checks again with new input values.
Common Mistakes with <
- Using
<when equality should also be allowed. - Mixing
<and>accidentally in threshold checks. - Skipping Serial prints and guessing comparison behavior.
- Comparing values with different scales (for example raw ADC vs mapped percentage).
Best Practices for <
- Use clear constant names like
MIN_TEMPorLIMIT. - Document whether the boundary should include equality.
- Test around edge values: just below, exactly equal, and just above.
- Keep threshold constants in one place so changes are safe and predictable.
- Use clear Serial logs that include both current value and limit.
Practice Task
- Set
LIMITto 1000 and test values 999, 1000, and 1001. - Change
<to<=and note behavior change at exactly 1000. - Print both
sensorValueandLIMITeach loop. - Add a second condition with
&&to require system enable flag.
Try it now
Open the simulator workspace and test below-limit and exact-limit behavior using <.