0Pricing
Arduino & IoT Academy · Lesson

if/else: LED Follows the Button

Light an LED only while the button is held.

if/else: LED Follows the Button is a free Arduino & IoT Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Make a Decision

Reading a button is great, but the magic is reacting to it. An if statement lets your board choose what to do based on the reading.

if Runs Conditionally

The code inside if only runs when its test is true. So you can light an LED only when the button is actually pressed.

if (state == LOW) {
  // button is pressed
}

Test for Pressed

Remember the inversion: with INPUT_PULLUP, pressed is LOW. So your condition checks whether the stored state equals LOW.

int state = digitalRead(2);
if (state == LOW) {
  digitalWrite(13, HIGH);
}

Add an else Branch

What about when it isn't pressed? The else block runs whenever the if test is false, giving you the other half of the behavior.

if (state == LOW) {
  digitalWrite(13, HIGH);
} else {
  digitalWrite(13, LOW);
}

LED Follows the Button

Together if and else make the LED mirror the button: on while held, off when released. That is your first real input-to-output link. 💡

Set Up Both Pins

The button pin is an input and the LED pin is an output. Declare both modes in setup() so each pin knows its job.

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}

One Equals vs Two

A classic trap: use == to compare, not a single =. One equals assigns a value, while two equals asks if they match.

Loop Keeps It Live

Put the whole thing in loop() so it checks the button thousands of times a second. The LED reacts the moment you press.

Read Then Decide

The pattern is always the same: read the input, then decide with if/else. Get comfortable with it; you'll reuse it everywhere.

Compare Right in the if

You can skip the variable and test the read directly. It reads cleanly and does exactly the same comparison inside the parentheses.

if (digitalRead(2) == LOW) {
  digitalWrite(13, HIGH);
}

From Sensing to Acting

You just closed the loop from input to output with pure logic. Swap the LED for a buzzer or motor and the same idea still drives it.

Quick Check

Pick the condition that lights the LED while the button is held.

Recap

You used if/else to make an LED follow a button: LOW lights it, HIGH turns it off. Remember == for comparison. Next you'll wire it all up. 🔌

Frequently asked questions

Is the “if/else: LED Follows the Button” lesson free?

Yes — the full text of “if/else: LED Follows the Button” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “if/else: LED Follows the Button”?

Light an LED only while the button is held. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “if/else: LED Follows the Button” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. INPUT vs INPUT_PULLUP
  2. Reading a Button State
  3. if/else: LED Follows the Button
  4. Wire a Button on a Breadboard
← Back to Arduino & IoT Academy