Logic: If … Then

Getting Started with Arduino If…Then Statements

Hey Arduino fans! 👋

When you’re building projects, your Arduino often needs to make decisions — like turning on an LED when a button is pressed, or triggering a motor when a sensor detects something. That’s where the if…then statement comes in. It’s one of the simplest ways to add logic to your code.

What Is an If…Then Statement?

An if statement checks a condition and runs some code only if that condition is true. It’s like telling your Arduino:

“If this happens, do that.”

Simple, right?

How It Works

Here’s a basic example:

int sensorValue = 600; // Example sensor reading

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (sensorValue > 500) {
    digitalWrite(13, HIGH); // Turn on LED
    Serial.println("Sensor value is high!");
  } else {
    digitalWrite(13, LOW);  // Turn off LED
    Serial.println("Sensor value is low.");
  }

  delay(1000);
}

Breaking It Down

  • if (sensorValue > 500) → checks if the sensor reading is greater than 500.
  • digitalWrite(13, HIGH) → runs this code if the condition is true.
  • else → optional, runs if the condition is false.
  • You can add multiple conditions using else if.

Why Use If…Then?

  • Makes your Arduino react to inputs like buttons, sensors, or switches.
  • Helps you control outputs like LEDs, motors, or buzzers based on conditions.
  • Keeps your code organized and readable, especially for beginners.

Real-World Examples

  • Turn on a fan when temperature rises above a threshold.
  • Light up an LED when a button is pressed.
  • Control multiple devices based on different sensor readings.

Once you get comfortable with if…then statements, you’ll start adding smarter behavior to your Arduino projects. It’s one of the first and most useful tools in your programming toolbox! ⚡

Visualizing Arduino If…Then…Else Logic

Hey Arduino enthusiasts! 👋

Understanding how if…then…else works can be much easier if you see it in action visually. Think of it like a flowchart for your Arduino brain — your code makes decisions just like you do in real life.

How the Flow Works

  1. Check a Condition
    Your Arduino asks: “Is this true?”
    • Example: “Is the button pressed?”
  2. If True → Do Something
    • The Arduino executes the code inside the if block.
    • Example: Turn on an LED.
  3. If False → Do Something Else (Optional)
    • If the condition isn’t met, it can run an else block.
    • Example: Turn the LED off.
  4. End / Repeat
    • After running one block, the Arduino loops back (if inside loop()) and checks the condition again.

Simple Flow Diagram

          +------------------+
          | Condition True?  |
          +------------------+
             |          |
            Yes        No
             |          |
     +---------------+  |
     | Execute If    |  |
     | Block Code    |  |
     +---------------+  |
             |          |
             +----------+
                  |
           Continue Loop

Why It Helps

  • Makes your code easier to understand for beginners.
  • Helps you plan projects before coding — you can draw the flow for buttons, sensors, or multiple conditions.
  • Visualizing logic is especially helpful for more complex projects with multiple if…else if…else statements.

Real-World Example

Imagine a temperature-controlled fan:

  • If temperature > 30°C → turn fan on
  • Else if temperature > 20°C → set fan to low
  • Else → turn fan off

A flowchart lets you map this decision tree before writing a single line of code.

Visual thinking + if…then…else = smarter Arduino projects! ⚡