"Decision matrix engaged. Routing execution flow based on real-time sensor telemetry and hardware states."
Conditionals are the "brain" of any microcontroller. Without them, a script just runs top-to-bottom and stops. With conditionals, your ESP8266 can react to its environment—turning on a fan when it gets too hot, connecting to backup Wi-Fi if the main network drops, or putting the system to sleep when the battery is low.
Before branching logic, the system needs to evaluate statements to True or False.
-
Comparison: *
==(Equal to),!=(Not equal to) -
>(Greater than),<(Less than) -
>=(Greater than or equal),<=(Less than or equal) -
Logical: *
and(True if BOTH are true) -
or(True if AT LEAST ONE is true) -
not(Inverts the boolean state) -
Identity & Membership:
-
is(Checks if two variables point to the exact same object in memory) -
in(Checks if a value exists within a list, string, or dictionary)
Here are the different ways to structure decision-making in MicroPython, alongside exactly when you should use them in real-world hardware projects.
- Concept: Executes a block of code only if a specific condition is met. Does nothing otherwise.
- IoT Use Case: Emergency interrupts and critical thresholds. Use this when you only care about anomalous events (e.g., detecting a gas leak or a button press) and want the system to ignore normal operations.
# main.py - Basic IF
gas_level = read_mq2_sensor()
if gas_level > 800:
trigger_siren()
send_alert_email()- Concept: Provides a primary path and a guaranteed fallback path.
- IoT Use Case: Toggling binary hardware states. Perfect for Day/Night modes, On/Off relays, or Connected/Disconnected network states.
# main.py - IF/ELSE
motion_detected = pir_sensor.value()
if motion_detected == 1:
relay.on() # Turn on lights
else:
relay.off() # Keep lights off- Concept: Checks multiple, mutually exclusive conditions in sequence. Once one is true, it skips the rest.
- IoT Use Case: Handling multi-tier states or device indicators. Commonly used for battery level mapping to RGB LEDs (Green/Yellow/Red) or processing different incoming MQTT command strings.
# main.py - ELIF CHAIN
battery_voltage = get_adc_voltage()
if battery_voltage >= 4.0:
led.color(0, 255, 0) # Green
elif battery_voltage >= 3.5:
led.color(255, 255, 0) # Yellow
else:
led.color(255, 0, 0) # Red - Needs charge
deep_sleep() # Protect battery- Concept: Placing an
ifstatement inside anotherifstatement. - IoT Use Case: Multi-step verification. Essential when dealing with network stacks where step B will crash if step A hasn't happened. For example, verifying Wi-Fi is connected before checking if the MQTT broker is reachable.
# main.py - NESTED IF
if wifi.isconnected():
if mqtt_client.ping():
mqtt_client.publish("sensors/temp", str(temp))
else:
mqtt_client.reconnect()
else:
connect_wifi()-
Concept: A one-line
if-elsestatement used strictly for assigning values. -
Syntax:
value_if_true if condition else value_if_false -
IoT Use Case: Fast, clean variable assignment. Excellent for formatting JSON payloads, formatting text for an OLED display, or mapping
1/0sensor outputs to human-readable strings without wasting 4 lines of code.
# main.py - TERNARY OPERATOR
water_detected = leak_sensor.value()
# Quick assignment for JSON payload
status_msg = "FLOOD" if water_detected else "DRY"
payload = {"status": status_msg}