Pushbuttons
A pushbutton provides an easy way for the user to interact with your sketch. This example requires the following external components connected to your Arduino - please consult the appropriate component information page if you have not used these before:
The code for this example is shown in the box below. You can load it into
your Arduino IDE from the File menu: File → Examples → 02.Digital → Button
The aim is to control the internal LED so that it is on when the button is pressed, but off otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Before you can run the example, you will need to construct the following circuit. Please make sure that your Arduino is not connected to the PC while you are making hardware changes.
When everything is ready, connect your Arduino, upload the sketch and test your circuit. Here are some troubleshooting tips:
-
If the LED is dimly lit all the time, your button may not be properly connected. Try pressing it firmly into the breadboard.
-
If the LED does not come on, check all of the connections to make sure they are the same as shown in the diagram
What is the resistor for?
There may be unexpected sources of voltage in a circuit which produce unwanted effects. In this case, we want to keep the voltage at the input pin low when the switch is open. To do this, we connect a 1kΩ resistor from the pin to ground. When the button is pressed, the voltage 'prefers' the low-resistance route through the pin which then recognises a high voltage signal. This arrangement is known as a pull-down resistor.
Add an external LED
This variation combines the button example with the blink example. You will need to make some changes to your circuit. Follow the example in the diagram below.
We are connecting the LED on pin 13. Because the internal LED is also connected to this pin, we do not need to make any changes to the sketch.
If your external LED does not work as expected, try the following:
- If the LED does not come one when you press the button, check that it is connected the right way round. See the LED information page for more help.
*If the LED still does not come on, check all of the connections to make sure they are the same as shown in the diagram
Here are some points to notice:
-
The external LED takes its power from the output pin. There is no need to connect its anode to the power bus.
-
The cathode of the LED is connected to the ground on the power bus. All of the ground pins on the Arduino (there are three of them) are kept at the same low voltage.
-
The circuit is modular - it can be divided into sections. Remember this when you are constructing more complicated circuits. You can combine elements from earlier examples for more complex behaviour.
Add another button
This variation requires changes to the sketch as well as some hardware changes. Basically, we are going to make a second copy of the existing circuit. Let's do the hardware first:
Some things to notice:
-
Colour-coding. The wires on the diagram are carefully coloured so that it is easy to understand. Connections to the high voltage on the power bus are red and the ones to the low voltage are black. The two sets of signal connections are different colours. Try to use similar colours when you construct a circuit on your breadboard. This will make it easier to find problems and make changes.
-
The two sets of components are laid out in the same way. This is like colour-coding - it makes it easier to find problems and make changes.
Now for the code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Some things to notice:
-
It is a good idea to use meaningful variable names
-
It is also a good idea to show which variables are related - in this example,
button0
controlsled0
-
Variable names do not have to match the pin numbers