Activity 2. Push-button
This activity uses push-button that provide input
signals on the Arduino Uno. The main objective is to understand the circuit
wiring and the corresponding code. (55 minutes)
Activity 2a
In this part the aim is for the Arduino Uno to read the states of a push-button.
- An LED follows the states of the push-button
- The push-button is connected to PIN_4. The built-in pull-up resistor is activated with an appropriate setting in pinMode(), so no external resistor needs to be used
- A voltmeter has been added to the circuit to check the voltage at the PIN_4
Tip. With this connection when the push-button is pressed the Arduino Uno reads “0” to the input and the LED is turned off
Step 1. Draw the next circuit in Tinkercad.
Step 2. Study the code and write it on the microcontroller:
/* Push-button and LED
Circuit Connections: PIN_0 => LED_Anode - LED_Cathode = > Resistor 220Ω => Gnd
PIN_4 => Pull-up resistor (built in) => push-button (Gnd)
*/
#define led_pin 0 //give the name “led_pin” to PIN_0
#define pb_pin 4 //give the name “pb_pin” to PIN_4
//The setup() function initializes and sets the initial values
//It will only run once after each powerup or reset
void setup(){
//Configure PIN_0 to behave as outputs
pinMode(led_pin, OUTPUT);
//Configure PIN_4 to behave as input with activated pull-up resistor
pinMode(pb_pin, INPUT_PULLUP);
}
//This function loops consecutively
void loop() {
//The LED follows the state of push-button
digitalWrite(led_pin, digitalRead(pb_pin));
}
Step 3. Run the simulation and check the correct operation of the circuit