Arduino Project: Toggle the LED with Push Button

Home / Arduino Project: Toggle the LED with Push Button

This 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 it.


  • Make the LED turn on or off by pressing 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;

int old_button_state = LOW;
int led_state = LOW;

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

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

  if (CURRENT_BUTTON_STATE != old_button_state &&
    led_state = (led_state == LOW ? HIGH : LOW);
    digitalWrite(LED_PIN, led_state);
  old_button_state = CURRENT_BUTTON_STATE;

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.

There is no significance change to our code from previous project. Things we can notice are the existence of two new variables: old_button_state and led_state.


You should see that LED is turned on or off whenever we press the button.

Why it Works

We define a global variable named led_state to store the current state of our LED. It will be LOW when the LED is on and HIGH otherwise. In loop() we check the button’s current state. When we press the button, the switches is in HIGH logic and we toggle the content of led_state.

The global variable old_button_state is used to store the old state of push button. If we don’t do this, when we press and hold the button, the state will constantly be HIGH and we’d toggle the LED’s state continuously. Because this happens so fast it seems like the LED’s constantly on. When we release the button, the LED is in a more or less arbitrary state. Therefore we use previous push button to guard the state of LED.


About Author

about author


A man who is obsessed to low level technology.

  1. leerew

    led_state = (led_state == LOW ? HIGH : LOW);
    digitalWrite(LED_PIN, led_state);

    shouldn’t be

    led_state = (led_state == LOW )? HIGH : LOW;
    digitalWrite(LED_PIN, led_state);

    • xathrya

      both of them gives the same result actually. The first approach (as seen in the article) emphasize that the value assigned to led_state is an evaluation to the expression of ternary operator. While the second approach is to emphasize the expression which will be evaluated by ternary operator before making decision.

      And thanks for opening this possibility.

  2. e


    I do not have too much experience with the Arduino. I connected the above circuit and runs quite well. Not a problem.
    I would like to include a timing function that keeps the time the led is on and gives the “lapse time” (between on and off signal).
    I tried the “millis()” function, but I am not getting good results.
    Suggestions are appreciated,
    Thank you for the above circuit and code.

Leave a Reply

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

Social Share Buttons and Icons powered by Ultimatelysocial