Arduino Project: Push Button

Home / Arduino Project: Push Button

Arduino Project: Push Button

December 11, 2015 | Article | 1 Comment

Pushbutton is one of common component found in projects. The mechanism is simple, we press push button and we got a signal, otherwise it (should) gives nothing.


  • Make the LED turn on when we press the push button.
  • Make the LED turn off when we release the push button.



Following is all materials I use for this tutorial.

  • Arduino Uno R3
  • LED
  • Push button
  • Breadboard

You can change the Arduino board to higher specification if you like. The LED I use will emit red light as it is powered by electricity.

There are some kind of push button exists. In our project, we need only push button with four legs.

You also need an USB A to B cable for upload our program to our Arduino and also for serial communication.


Here is the circuit image, created using Fritzing.


To build the circuit, attach the LED as shown in the figure. Attach the long leg of an LED (positif leg or anode) to pin 13. Attach the short leg (negative leg or cathode) to the ground (GND). On Arduino Uno, there is also a built in status LED which connected to pin 13. When our Arduino running successfully, both status LED and external LED will start to blink.

The push button has four legs (or four pins) as shown above. Two opposite pins connect when the button is pushed; otherwise, they are disconnected. In one side, we connect one leg to Arduino pin 7. In other side, we supply 5V electricity to one leg. The other leg is connected to a 10k ohm resistor before going to ground.


Let’s see what code we use for this project and discuss it later.

const unsigned int BUTTON_PIN = 7;
const unsigned int LED_PIN = 13;

void setup() {
  pinMode(LED_PIN, OUTPUT);

void loop() {
  const int BUTTON_STATE = digitalRead(BUTTON_PIN);

    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);

Notice that we define two constant here, LED_PIN which is for LED, and BUTTON_PIN for the push button. Both are defined as different purpose, the LED is output pin while the push button is input.

Our code is as simple as checking the push button state and then output the corresponding voltage to LED.


You should see that LED is turned on whenever we press the push button and turned off when we don’t. To keep the LED turned on, we have to keep pressing the push button.

Why it Works

Why do we need resistor for push button?

The problem is that we expect the push button to return a default value (LOW) in case it isn’t pressed. But when the button isn’t pressed,it would not be directly connected to ground and would flicker because of static and interference. A little bit of current flows through the resistor,and this helps prevent random noise from changing the voltage that the input pin sees.

When the button is pressed, there will still be 5 volts at Arduino’s digital pin, but when the button isn’t pressed, it will cleanly read the connection to the ground. We call this a pull-down resistor. That is, we have to connect the Arduino’s signal pin to power through the pushbutton and connect the other pin of pushbutton to ground using a resistor.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.


About Author

about author


A man who is obsessed to low level technology.

1 Comment
  1. Arduino Project: Toggle the LED with Push Button - Xathrya.ID

    […] project is upgrade of previous project (Simple Push Button). In this project, we just need to press the push button once to turn it on or off, instead of hold […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Share Buttons and Icons powered by Ultimatelysocial