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.

Objective

  • Make the LED turn on when we press the push button.
  • Make the LED turn off when we release 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.

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

Result

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.

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="">