#9Beginner

LED Control with a Push Button (Push)

4.9(110)
179 completed
LED Control with a Push Button (Push)
Push control project that turns on the LED when the button is pressed and turns it off when it is released. Teaches button reading and conditional expression usage with digitalRead.

Video

Circuit Diagram

LED Control with a Push Button (Push) - Circuit

Source Code

Arduino (C++)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   LED Control with a Push Button (Push)
4// ─────────────────────────────────────────
5// The LED stays on while the button is held and turns off when it is
6// released (push). Shows reading a button with digitalRead and if-else.
7//
8// Pins:
9//   D13 -> Button, other leg to 5V
10//   D13 -> 10k resistor to GND (pull-down)
11//   D7  -> LED (with a 330 ohm resistor)
12
13void setup() {
14  pinMode(13, INPUT);    // Button input (external pull-down)
15  pinMode(7, OUTPUT);    // LED output
16}
17
18void loop() {
19  if (digitalRead(13) == HIGH) {   // Is the button pressed?
20    digitalWrite(7, HIGH);         // Pressed: LED on
21  } else {
22    digitalWrite(7, LOW);          // Released: LED off
23  }
24}
25

FAQ

What is a 10 kΩ resistor used for?▾

It is a pull-down resistor: it ensures a stable LOW reading by pulling the pin to GND when the button is not pressed. Without this resistor, the pin would be left floating and would read a random HIGH or LOW, causing the LED to flicker on its own.

Can I connect a button without using a resistor?▾

Yes, using `pinMode(pin, INPUT_PULLUP)` enables the Arduino's internal pull-up resistor. In this case, the button is connected between the pin and GND, and the logic is reversed: it reads LOW when pressed and HIGH when released.

Which pins on the button should I use?▾

On 4-pin buttons, the two opposite pins are already connected internally. Place the button over the empty space in the center of the breadboard and use the diagonal pins; this way, you can be sure you’re selecting the correct pins.

What is the difference between "Push" and "On-Off"?▾

In push mode, the LED lights up only while you hold down the button. In on-off (toggle) mode, each press changes the LED's state, and the LED stays on even after you release the button; this requires a state variable.

LED Control with a Push Button (Push) | BlueGrays