Arduino Lesson 5.2 — Read a Button

LessonsArduino → Lesson 5.2
Arduino

Read a Button

“Press it. Light on. Release it. Light off. Input meets output.”

⏱ About 20 minutes 🛠 Arduino IDE + Arduino board ⭐ Lesson 2 of 3

What you will need

Everything from Lesson 5.1 plus one push button.

You’ll need your Arduino, LED, resistor, and jumper wires from the last lesson — plus one push button (also called a tactile switch).


What you will have

A button wired to your Arduino that turns an LED on when pressed and off when released.

This is the foundation of every physical interface ever made — from elevator buttons to game controllers.


Video Walkthrough

Watch the full lesson walkthrough.


Your prompt

Copy this. Paste it into your AI. Hit Enter.

📋 Your Prompt — Copy Everything Below
Write an Arduino sketch that reads a push button connected to pin 7 with the internal pull-up resistor enabled. When the button is pressed the LED on pin 13 turns on. When the button is released the LED turns off. Include comments explaining what INPUT_PULLUP means and how digitalRead works.
✅ Copied! Now paste it into your AI.

Wire it up

Add the button to your existing LED circuit.

1
Keep your LED wired to pin 13
Everything from Lesson 5.1 stays the same.
2
Place the button on your breadboard
Push the button into the breadboard so its legs straddle the centre gap.
3
Connect one leg to pin 7 and the other to GND
Use jumper wires to make both connections.
Arduino Uno wiring diagram showing an LED on pin 13 and a push button connected to pin 7 and GND

Your wiring should look like this — LED stays on pin 13, button connects pin 7 to GND.


What to do after

Paste, upload, press.

1
Paste the AI code into Arduino IDE and upload
Select all → Delete → paste → click Upload → wait for Done uploading.
2
Press the button
LED on. Release it. LED off. Press and hold. LED stays on. That’s input and output working together.

🛠️ Didn’t work?

Check these first:

1 Is the button straddling the centre gap of the breadboard?
2 Is one leg connected to pin 7 and the other to GND?
3 Copy any error and paste to your AI: “This didn’t work. Here’s the error: [paste]. Please fix it.”

Vibe Tweak

Make it a toggle.

Right now the LED turns on while the button is held and off when released. Let’s change it so the first press turns the LED on and the second press turns it off — like a light switch.

Paste this into your AI, get the new code back, replace the old code in Arduino IDE, and click Upload.

Paste this to your AI
Update my Arduino button sketch so the button toggles the LED. First press turns it on. Second press turns it off. Add debouncing so it doesn’t flicker.
Then do this
Replace code → Upload → Press once: on. Press again: off.

Extra Credit

Want to go further?

📋 Extra Credit Prompt
Add a second LED to the Arduino circuit on pin 12. When the button is pressed once the first LED turns on. When pressed again the second LED turns on and the first turns off. When pressed a third time both turn on. When pressed a fourth time both turn off. Cycle through these four states.
✅ Copied!

Reflection

Before you move on — think about this.

“Every button you’ve ever pressed — on a keyboard, a game controller, a microwave — works on this same principle. Can you find the digitalRead line? What does it return when the button is pressed?”

Up next
Arduino Lesson 5.3 — Mood Light
Three LEDs. One button. Each press cycles through a different colour mood.
Next Lesson →
Greg the Vibe Coder · gregthevibecoder.com 18 lessons · No account required