Example-2
LED Control using a Push Button
Objective
Use a push button to turn an LED ON when pressed and OFF when released. Students learn about input devices, pull-down resistors, and digital input/output in Arduino.
Learning outcomes
-
Understand the difference between input and output pins.
-
Learn how to wire and use a push button in Arduino circuits.
-
Understand the role of pull-down resistors.
-
Write an Arduino sketch to read a button press and control an LED.
Components (Tinkercad palette)
-
Arduino Uno R3
-
Breadboard (small)
-
1× LED (any color)
-
1× Resistor 220 Ω (for LED)
-
1× Resistor 10 kΩ (pull-down for button)
-
1× Push button
-
5× Jumper wires
Circuit wiring
-
Place the push button across the middle groove of the breadboard (so each pair of legs is on separate rows).
-
Connect one leg of the button → Arduino pin 2 (digital input).
-
Connect the same leg → +5V through the breadboard rail.
-
Connect the other leg of the button → GND rail.
-
Add a 10 kΩ resistor between pin 2 and GND (this acts as a pull-down, ensuring pin stays LOW when button not pressed).
-
Wire the LED:
-
LED anode (long leg) → Arduino pin 8 (digital output).
-
LED cathode (short leg) → one side of the 220 Ω resistor.
-
Other side of the resistor → GND rail.
-
-
Connect Arduino GND → breadboard GND rail.
-
Connect Arduino 5V → breadboard +5V rail.
Arduino sketch
// EXP-2: LED control using a push button (pull-down resistor)
const int BUTTON_PIN = 2; // push button input
const int LED_PIN = 8; // LED output
void setup() {
pinMode(BUTTON_PIN, INPUT); // using external 10k pull-down
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int pressed = digitalRead(BUTTON_PIN); // LOW or HIGH
if (pressed == HIGH) {
digitalWrite(LED_PIN, HIGH); // LED ON while pressed
} else {
digitalWrite(LED_PIN, LOW); // LED OFF when released
}
}
Step-by-step in Tinkercad
-
Create new circuit in Tinkercad.
-
Place Arduino, breadboard, LED, push button, and resistors.
-
Wire as explained above (keep GND wires black and 5V wires red for clarity).
-
Open Code → Text editor and paste the above sketch.
-
Start simulation → Press the button in the workspace → LED should glow while pressed.
How it works
-
Button not pressed → pin 2 is pulled LOW (through 10 kΩ resistor) → LED OFF.
-
Button pressed → pin 2 connected to 5V → Arduino reads HIGH → LED ON.
-
Code continuously checks the button state in
loop()
and updates LED accordingly.
Common mistakes & fixes
-
LED not lighting: Check if the LED is reversed (long leg should go to Arduino pin).
-
Button always ON: Missing or wrong pull-down resistor connection.
-
Pin mismatch: Ensure the code pin numbers match the actual wiring.
Viva / check-your-understanding
-
Why do we need a pull-down resistor with a button?
-
What happens if you remove the resistor?
-
Can we use Arduino’s internal pull-up resistor instead of an external one? (Yes:
pinMode(BUTTON_PIN, INPUT_PULLUP)
, but logic will invert.)
Extensions (mini-tasks)
-
Modify code so that button press toggles LED state (LED stays ON after one press, OFF after the next).
-
Use two buttons: one for LED ON, another for LED OFF.
-
Try with internal pull-up resistor and adjust code accordingly.
Lab record (suggested headings)
-
Aim
-
Circuit diagram (Tinkercad screenshot)
-
Code listing
-
Output observation (simulation screenshot)
-
Inference/Conclusion
Comments
Post a Comment