## Introduction: Arduino Buttons Made Simple

Welcome back Guys, Gals and fellow Robots

What will evil geniuses be without that famous big red button, with which they launch their terrifyingly evil, but yet awesome death-rays into earth's atmosphere.

Well in this instruct able we will look at how we can hook up a button to an Arduino, so that we may possibly design a counter death-ray to save humanity and be crowned as heroes.

You will need the following parts to participate in this epic build:

• 1 x Arduino
• 1 x Tactile Switch (unfortunately mine isn't an epic red like the one in the link)
• 1 x 10k resistor
• 4 x jumper wires

These parts can also be found on places such as

Ok now let's get to it...

## Step 1: Wiring It Up

Some Things to know:

The breadboard is where the magic happens in the world of electronics. Many if not all ingenious electrical inventions first came in to existence on this humble but confusing at first little piece of plastic. Above is my best attempt at drawing an illustration of how the holes in the breadboard are connected. The holes are connected in rails as indicated by the lines. The outer 4 rails (two left and two right) are connected downward and span the length of the board. The 2 inner sets of holes are connected horizontally, but seperated by the gap in the middle of the board.

• The Button:

When looking at the underside of the button (see image above) you will see 4 little legs and a line. The line indicates a divide, thus from the image above, we can see that the two legs on the left are connected and the two legs on the right are connected. When the button is pressed all four the legs become connected.

• Current limiting resistor

The 10k ohm resistor is used to prevent a short circuit and damage to the Arduino. The Arduino is only capable of sinking or sourcing 40mA per pin. Using ohm's law I=V/R we can see that 10k ohm resistor will ensure that the Arduino is safe.

Building the Circuit:

Now that we have all the boring technical stuff out of the way we can build the circuit. Here is what to do:

1. Use a jumper to connect 5V output from Arduino to one of the outer rails (will call it rail 5V).
2. Use a jumper to connect GND output of Arduino to one of the other outer rails (will call it GND)
3. Place button over the central gap of the breadboard so that the line underneath crosses the breadboard gap.
4. Use jumper to connect 5V to one set of the button's legs.
5. Use 10k resistor to connect GND rail to the other set of the button's legs
6. Use jumper to connect pin 7 of Arduino to same set of legs on button that has the 10k resistor connections

just take a close look at the image above to help you wire it correctly.

Good job... Now to make it all come alive....

## Step 2: Software :)

Note:

• If the concepts below are not familiar to you, check out my first tutorial where I explain what isn't explained here.

Almost there...

Simply copy the code in the Image above or download the Buttons.ino file and then upload it to the Arduino, then press that button.

Time for an Explanation:

First we define our button and led pins at the very top of the code, thus making them global variables so that all the functions below will be aware of their existence.

In the setup() function:

we declare our led pin as a OUTPUT, so that Arduino knows where to make the magic happen. Arduino by default puts its pins as INPUTS and therefore we don't have to declare our button pin as an INPUT in order for the program to work.

In the loop() function:

We declare a statement of the form

if(condition){

***make some magic happen***

}else{

***do something else***

}

we check if the condition within the () brackets are met, and if yes then Arduino makes magic happen. If the condition is not met then Arduino does something else as suggested above. since this if else statement is within the loop() function, arduino constantly checks the value returned by the built in Arduino function digitalRead().

When the button isn't pressed there is 0 volts on pin 7, then the digitalRead() function returns a value of 0, which is equivalent to a logical "no" or "false" so then Arduino doesn't make any magic happen but runs the code in the else{} section.