Buttons


In this lesson, we’ll make our LED (which is now back on pin 13) light up via a button. 

[what we are trying to do]

Much like LED’s, buttons and switches are components that you can have fun with as there are a wide variety of options for you to choose from to give your project style or character. Lil’ babies, big bois, vidjah game style; the sky is pretty close to the limit.

[pictures of buttons]

The button we’re using from the kit is a momentary switch, meaning that it is only connected, momentarily, when we depress the button. A cute way to visually understand this is by looking at the schematic symbol for a button, it simply separates two wires.

[button schematic symbol]

How do we use this dang thing?

If you’ve pulled your button out from your kit, you might have noticed that it has four wires coming out of it. What gives? This is by design to make the button sit more firmly in a finalized circuit. 

[picture of button]

If you take a look at the underbelly of the button, you’ll see a trough of sorts. The trough is in an indication of the two parts of our circuit that we’d like to separate/connect
[picture of button with underside visible][picture of a trough]

Doin’ pull-downs

For this exercise, we’ll take our circuit with the LED, from the blink example, and simply add a button to it.
[circuit image]

You’ll notice that the button separates 5V and GND (via a resistor), and then the arduino is connected to the grounded side. 

If you look at the resistor chart (or just read ahead in the code︎), you’ll realize that our resistor is 10K Ohms (10,000 if ya nasty) which is quite a bit more resistance than we’ve used with our LED. We won’t get into the math or science of why this is. The reality is we don’t need to, we just need to understand the principle at play. 

When a resistor is connected like this, to ground, it is called a pulldown resistor. If we were to reverse the configuration, such that we used a jumper for ground, we’d have what’s conversely referred to as a pullup resistor. Here’s why we need this. 

[schematic of resistor pulling up and pulling down]

Our arduweeny is set up (by our code, which we’ll get to in a sec) to read pin 2 and try to figure out if it is HIGH or LOW. The issue is, that if we have a wire not connected or floating, we’re not sure what the signal could be. 

Think about it this way; if the switch was replaced by two wires, just...hanging out, any number of things could happen: a kitty could lick them, a drop of sodey pop could issue forth from the aether, the point is that we don’t know.
[picture of kitty][picture of sodey pop]

By making sure that we have the side that bridges the connection always connected to ground (in this case), we assure that we know what the signal will be when not connected, 

If we think about the switch being connected, we can understand why we need a resistor with a high ohm-age. When the gap is connected, the signal can either go to ground or to the arduino. Having a high value resistor assures the signal will follow the path of least resistance. In fact this is where that saying comes from, I think. This means we are confident that the signal will be clearly HIGH or LOW.

If it doesn’t make sense (which is okay it can be confusing), try connecting the button without the resistor at all, and then with a wire in place of the resistor
[versions of those circuits]
And see if the values you get are still reliable.

Hookin’ it up

Here’s how I approach hooking up a button. I first take the connection I want to bridge; in this case 5V to ground (via a resistor), I usually also, almost always use a pulldown, rather than pullup resistor just in practice. Whichever connection has the resister is where I add the connection to the arduweeny. That’s it.
[pictures of the circuit in sequence]

Gimme the codez

No problem! Here it is.

You’ll notice a couple new things in our code: 
  • the pinMode() for the button is set to INPUT
  • digitalRead() checks whether the button is HIGH or LOW

Here it is all workin: 
[video of button turning off and on LED]

Here are some exercises