Buttons are a common component used to control electronic devices. They are usually used as switches to connect or break circuits. Although buttons come in a variety of sizes and shapes, the one used here is a 6mm mini-button as shown in the following pictures.
Pin 1 is connected to pin 2 and pin 3 to pin 4. So you just need to connect either of pin 1 and pin 2 to pin 3 or pin 4.
The following is the internal structure of a button. Since the pin 1 is connected to pin 2, and pin 3 to pin 4, the symbol on the right below is usually used to represent a button.
Connect one side of the button to B18, and the other to GND. Press the button, and B18 will be Low level. In other words, we can know whether the button is pressed or not by reading the value of B18. Since we have already set pull-up for the button in the code, we don’t need to add a pull-up resistor in the circuit.
Let’s write a simple sketch to detect the input signal first.
Create a new code file named button.py.
For screen users, you can use the Python Shell. Click File -> New, or just press Ctrl + N, and ensure it’s saved under /home/pi/RPi_Beginner. Enter the name button.py and click OK to create.
For remote login, you can use the nano command, nano button.py.
Now, let’s write the code:
import RPi.GPIO as GPIO button_pin = 18 # Connect button at 18 GPIO.setmode(GPIO.BCM) GPIO.setup(button_pin, GPIO.IN, GPIO.PUD_UP) # Set button as input and pull up while True: button_status = GPIO.input(button_pin) # Read button input and give the value to status if button_status == 1: # Check status value print "Button Released!" else: print "Button Pressed!"
Keep the GPIO and LED wiring the same in the code, but set button_pin to 18. In GPIO.setup, set button_pin as input (GPIO.IN), and add GPIO.PUD_UP. We will check it later.
In the loop, we can also use the GPIO.input to read the button status, which returns two values: 0(Low) and 1(High). Use the if statement to determine the status by the value – whether the button is pressed or released. Pay attention to the operation symbol in the statement, which determines whether two parts are equal or not. Note that it’s two equal signs “==“, which is different from “=” which is used in assignment.
So, what exactly does the previous GPIO.PUD_UP do?
It means to pull up a pin. Since the voltage in the circuit will be unstable when the button is released and the pin 18 is disconnected in the circuit. Therefor to keep a stable high (UP) or low (DOWN) level, it’s necessary to attach a pull-up (GPIO.PUD_UP) or a pull-down (GPIO.PUD_DOWN). But why a pull-up instead of pull-down here? Because the other pin of the button is connected to GND, or, Low level. If we attach a pull-down, the button will be at Low level after it is released, which means it will always be Low no matter being pressed or released, thus we cannot tell the button status then. Therefore, it has to be a pull-up.
Now, save the code and run it.
When the button is not pressed, “Button Released!” will be sent on the screen continuously. When it is pressed, “Button Pressed!” will be displayed. As the code is always detecting, the corresponding information will be printed on the screen continuously. But this is too annoying if it just keeps prompting. How to solve this problem then?