
The Button Toggle LED project introduces one of the most important programming concepts in embedded systems: State Memory.
Unlike the previous lesson where the LED only remained ON while the button was being pressed, this project allows Arduino to remember the LED's current state.
A single button press turns the LED ON. Pressing the same button again turns the LED OFF.
This project introduces:
By the end of this lesson, you'll understand how Arduino can remember information and make decisions based on previous events.
Arduino Uno MCU Board × 1BreadboardPush Button × 1LED × 1220Ω Current-Limiting Resistor × 1Jumper Wires × 4USB Cable × 1GND pin to the negative rail (-) of the breadboard.This creates a common electrical reference for the circuit.
Anode (+) to Arduino digital Pin 13 through a 220Ω resistor.Cathode (-) to the negative rail.Pin 2.Arduino will use its internal pull-up resistor to monitor button presses.
Pin 13.Pin 2.Pro Tip: If the LED changes state multiple times from a single press, add a short debounce delay inside the code.
Inside setup():
Pin 13 is configured as an OUTPUT.Pin 2 is configured using INPUT_PULLUP.This allows Arduino to safely read button states while controlling the LED.
Arduino continuously checks the state of the push button using digitalRead().
When a button press is detected, the program triggers a state change.
A variable is used to remember whether the LED is currently ON or OFF.
Each button press flips the stored value.
Example:
Based on the stored state:
Pin 13 becomes HIGHPin 13 becomes LOWThis creates the ON/OFF switch behavior.
After uploading the code:
The cycle continues with every button press.
LED Does Not Toggle: Verify that the push button is connected to Pin 2 and GND. Incorrect button placement is the most common issue.
LED Toggles Multiple Times: This usually occurs because of switch bounce. Add a debounce delay or use button state tracking logic.
LED Never Turns OFF: Check your toggle variable and ensure its value changes after every button press.
Upload Errors: Verify the selected board, COM port, and USB cable connection inside Arduino IDE.
By completing this project, you have learned:
In Day 4, you'll learn how to control LED brightness using PWM (Pulse Width Modulation), allowing Arduino to create smooth fading effects instead of simple ON and OFF states.