In this tutorial, we learn how to use a push-button with Arduino and also turns on the built-in LED on pin 13 when pressing the push button.
A pushbutton is a switch when we pressed the button it makes a connection between its two legs.
The ability of Arduino to sense digital and analog inputs allows it to respond to you and to the world around you.
Here we introduce techniques that can use to do useful things with these inputs. This section will cover the electrical section of Arduino.
When wiring components to your Arduino be careful about how to connect and power the things. Arduino uses a robust controller chip that can take a fair amount of power, but one can damage the chip if it connects the wrong voltages or short circuit an output pin.
Mostly Arduino controller chips are powered by 5 volts, do not connect external power to Arduino pins with a higher voltage than this (or 3.3 volts if your Arduino controller runs on this voltage).
- Push Button.
- Jumper Wires.
Using a Push Button Switch
Create a sketch to respond to the closing of electrical contact. For example, a pushbutton, switch or an external device that makes an electrical connection.
Here we use the digitalRead function to determine the state of a switch connected to an Arduino digital pin set as input. The code lights LED when a pushbutton switch is pressed.
Circuit Diagram of Push Button with Arduino
Code for Push Button with Arduino
Here the setup() function configures the LED pin as OUTPUT and the switch pin as INPUT.
In the above code digitalRead function monitors the voltage on the input pin (inputPin), and it returns a value of HIGH if the voltage is 5 volts (high) and LOW if the voltage is 0 volts (low).
Determining How Long a Switch is Pressed
If an application wants to detect the length of time a switch has been in its current state. If you want to increment a value while a switch is pushed and you want the rate to increase the longer the switch is held (the way many electronic clocks are set).
The sketch demonstrates the setting of a countdown timer.
Pressing a switch sets the timer by incrementing the time count, releasing the switch starts the countdown.
The count is incremented by one when the switch is initially pressed.
Holding the switch for more than on second increase the increment rate by four, holding the switch for four-second increase the rate by ten.
Releasing the switch starts the countdown when the count reaches zero, a pin is set HIGH: