Push Button
Last updated
Last updated
The push buttons included in your Photon kit are classified as momentary switches, which means they detect when they are being pressed or pushed. For example, the keys on a computer keyboard are momentary switches: they are only "on" when you press them (and they turn "off" when you release them).
This is different from maintained switches, which toggle between one state and another state. For example, a light switch is a maintained switch: pressing (or flipping) the switch will turn it on, and it will stay this way until pressed again (or flipped back).
The push button has 4 metal legs on its base (two legs on one side, and two legs on the opposite side). Unlike most other parts that connect to only one side of a breadboard, the push button has to connect to both sides of a breadboard.
Place the push button along the middle divider of the breadboard, so 2 of its legs will connect to the left side, while the other 2 legs will connect to right side. Carefully line up the legs with breadboard holes, and then firmly press the button down to "snap" its base into place. Then use jumper wires to connect 2 of the button legs to the Photon.
Push Button | Photon Pin |
Leg (any leg) | any I/O pin |
Other Leg (on same side) | GND |
Experiment 2 of the online SparkFun Photon Experiment Guide shows how to connect a push button. Here is the connection diagram for Experiment 2 (ignore the wiring for the LED and resistor):
TIP: Be sure each jumper wire for the button is connected to the same row as one of the button legs. Both jumper wires should be placed on the same side of the breadboard (either the left or right side).
The push button does not require any special code library.
In the global variables, you should declare a variable to represent the specific pin on the Photon board that the push button is connected to. This will make it easier to understand your code (and easier to modify the code if you change which pin the button is connected to). The example below declares a variable called "button" (but you could use a different variable name).
If you are using multiple push buttons, then be sure to give each button a unique variable name. Use variable names that will make sense when someone reads your code.
Within the setup()
function, you have to include a statement to set the pin mode for the button pin variable:
If you are using multiple buttons, be sure to set the pin mode for each button's pin variable.
Code for checking the button would be placed within the loop()
function or within a custom function.
The current state of the push button can be detected by using a digitalRead()
statement.
If the button is currently being pressed, digitalRead()
will return a value of LOW
.
Often, a local variable is used to store the result of the digitalRead()
, so that your code can use this result to make decisions based on whether the button is being pressed.
Even though the push button is a momentary switch (which is only "on" when being pressed), you can modify your Photon code to make the button act like a maintained switch (which toggles between states with each press).
The key to doing this is to create a global variable that will store the current status of the part that you want the button to control (such as an LED light being "on" or "off"), so you can switch the status every time the button is pressed.
It will also help to include a delay()
statement before reading the button again because it takes a person a fraction of a second to physically press and release the button. This small delay will allow the code to detect each press as a single event (instead of as multiple presses).