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.

Objective

  • Make the LED turn on or off by pressing the push button.

Prerequisite

Requirement

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.

Circuit

Here is the circuit image, created using Fritzing.

PushButton

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.

Code

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

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.

Result

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.

  1. led_state = (led_state == LOW ? HIGH : LOW);
    digitalWrite(LED_PIN, led_state);
    delay(50);

    shouldn’t be

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

    • Hi,
      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.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">