Push Button Experiments

After completing the Push Button project, test yourself by trying these experiments.

1 Very often we need to make connections similar to the one used for the push button where an external resistor is needed as shown here. Push button schematic
To simplify the connections, an internal pull-down resistor (i.e. the resistor is connected to GND) is built-in to each of the digital pins on the Arduino so that an external resistor is not needed. Built-in pull-down resistor












To use the internal pull-down resistor, set the pinMode command to OUTPUT but use it for input.
Built-in pull-down resistor
Remove the external 22K ohm resistor on the breadboard and then run your modified program.
You should get the same result.
2 In addition to the internal pull-down resistor, each digital pin on the Arduino also has an interal pull-up resistor, i.e. the resistor is connected to +5V instead of to GND.

In order for this to work, notice that the other end of the push button is connected to GND instead of to +5V.
Built-in pull-up resistor












To use the internal pull-down resistor, set the pinMode command to INPUT_PULLUP and use it for input.
Built-in pull-up resistor
Run your modified program.
You should get the opposit result, i.e., the LED turns off when you push the button.
3 Instead of using the push button to control the internal LED connected to pin 13, make the push button to control an external LED that is connected to pin 3.

You will first have to connect an external LED like the picture on the right. Keep the push button connections.

Next, modify the code to control the external LED instead of the internal LED. Hint: change pin 13 to pin 3 in the code.
External LED connection
4 Add a buzzer to what you have from experiment 3. Make it so that when you press the push button the external LED will turn on and the buzzer will sound one note, and when you release the button both of them will turn off.















The if command checks the buttonState variable to see if it is HIGH or LOW. If it is HIGH then the first digitalWrite command is executed to turn on the LED and then the tone command is executed to sound the buzzer,

otherwise the second digitalWrite command in the else part of the if command is executed to turn off the LED followed by the noTone command to turn off the buzzer.
Button-LED-buzzer