Lesson 39: Using > Greater Than Comparison Operator
Learn how > compares numeric values and helps trigger actions when values exceed thresholds.
Progress indicator
Lesson 39 of 57
Learning Objectives
- Understand what the greater-than operator (>) does.
- Use > to compare variables and constant values.
- Understand > vs >= and choose the correct comparison.
- Use > in if/else conditions for threshold-based decisions.
- Understand strict-threshold behavior in real Arduino signals.
- Avoid common mistakes in greater-than comparisons.
Concept Explanation
What is the Greater Than Operator (>)
The > operator checks whether the left value is larger than the right value.
If left is bigger, result is true. Otherwise, result is false.
Greater Than Syntax
if (valueA > valueB) {
// runs when valueA is bigger
}How > Works
- Evaluate left expression value.
- Evaluate right expression value.
- Compare both values numerically.
- Return true if left is strictly larger.
Strictly larger means equal values do not pass the condition. For equal values, use >= when needed.
Comparing Variables and Values
sensorValue > 2000checks threshold crossing.currentTemp > maxTempchecks limit exceed condition.counter > 10checks progression beyond a point.
> vs >= (Comparison)
>means strictly greater than.>=means greater than or equal to.
| Expression | Value = 2000 | Meaning |
|---|---|---|
value > 2000 | false | Only values 2001+ pass |
value >= 2000 | true | Value 2000 and above pass |
if (score > 80) { /* only 81+ */ }
if (score >= 80) { /* 80 and above */ }Using > in Conditions
In Arduino projects, > is often used for threshold decisions such as turning on outputs when sensor readings go above a limit.
Real examples
if (temperature > maxAllowed)for overheat warning.if (distanceCm > safeDistance)for motion decisions.if (batteryPercent > 20)to allow high-power actions.
When to Use >
- Temperature or light threshold alerts.
- Detecting value growth beyond safe range.
- Flow control after counters exceed target values.
Example Code
This sketch turns LED ON only when the sensor value is greater than the threshold.
const int LED_PIN = 2;
const int LDR_PIN = A0;
const int THRESHOLD = 2000;
int sensorValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
sensorValue = analogRead(LDR_PIN);
if (sensorValue > THRESHOLD) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Sensor is above threshold -> LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("Sensor is not above threshold -> LED OFF");
}
Serial.print("sensorValue = ");
Serial.println(sensorValue);
delay(300);
}Example Code Explanation
- Read sensor value from analog input.
sensorValue > THRESHOLDcompares current reading with limit.- When true, LED turns ON and serial message confirms threshold exceeded.
- When false, LED turns OFF and message shows condition not met.
- Serial prints current value for real-time debugging.
delay(300)keeps readings easy to observe in Serial Monitor.
Real-life analogy
A speed alarm triggers only when speed is greater than the limit, not when it is exactly equal to the limit.
What Happens Inside
- CPU loads both operands into comparison logic.
- Comparator checks if left operand is numerically larger.
- Comparator returns a boolean result.
- Program branches to matching if/else block using that result.
- Loop repeats with new sensor values, producing dynamic threshold behavior.
Common Mistakes with >
- Using
>when>=was required. - Comparing wrong variable or wrong threshold value.
- Not printing values during debugging, making condition errors hard to detect.
- Choosing
>when inclusive threshold behavior was required.
Best Practices for >
- Use clear constant names like
THRESHOLD. - Print compared values while tuning thresholds.
- Double-check whether strict or inclusive comparison is intended.
- Keep thresholds in named constants so tuning is easy and safe.
Practice Task
- Change
THRESHOLDto a lower value and observe LED behavior. - Replace one
>with>=and compare outcome. - Print both threshold and sensor value each cycle for debugging.
- Add a second condition with
&&(for example, only if system is enabled).
Try it now
Open the simulator workspace and test threshold comparisons with live values.