In the previous tutorial we took a look at how to enable and blink a led.
Thought this is handy on its own, we want to take a look at user input using a small button. And that’s what we are doing today.
The requirements didn’t change from last episode. but we do need extra wires, switch and resistor
- Raspberry Pi 3B+ (any Pi with 40 pin headers will do)
- Ethernet/Wifi connection
- (micro) sd card with Raspberry Pi OS
- Dupont jumper wires
- Screen (optional)
For pushbuttons we need to use a GPIO pin as an Input. For this to work properly we’ll use an external resistor to set the idle state.
We can either use a Pull Up (resistor between 3V or Vcc and the input-pin) or a Pull Down (resistor between GND and input-pin) the switch will be in the other position (either to GND or to Vcc). Please note that the inputs of a pi are fragile and can not accept voltages above 3V3 (3,3V).
Lets take a look at some python code for inputs.
In out first example we’ll enable the led when the push button has been pressed.
To read the value of an input channel, call:
Where channel is the channel number as used in setup. It will return a value of 0, GPIO.LOW, or False (all are equivalent) if it is low and 1, GPIO.HIGH, or True if it was at a high level.
Lets connect this to our led output we made created in the previous episode.
First copy that example:
cp hello_world.py hello_world2.py
Once copied we’ll start editing our new file:
sudo nano hello_world2.py
behind our setup of the output we’ll include the input setup of our button, I connected my switch on pin 11 (GPIO 17). And we’ll force the led to be of after setup:
To make things more readable we’ll change the pin’s (7 and 11) with variables. This will make things more readable when looking back at your code.
led = 7 switch = 22 GPIO.setmode(GPIO.BOARD) GPIO.setup(led,GPIO.OUT) GPIO.output(led,0) GPIO.setup(switch,GPIO.IN)
Ok so we setup out input now lets check the status of that input and set the led id the switch displays a ‘one’, for this to work best we’ll wire our resistor to be a Pull-Up.
if GPIO.input(switch): GPIO.output(led,1) print("Switch is pressed")
Delete all other code so we’ll be left with following in our new file, and run it
import RPi.GPIO as GPIO import time led = 7 switch = 11 GPIO.setmode(GPIO.BOARD) GPIO.setup(led,GPIO.OUT) GPIO.setup(switch, GPIO.IN) GPIO.output(led,0) count = 0 timeout = 0.2 while(count < 50): if GPIO.input(switch): GPIO.output(led,1) print("Switch is pressed") count = count +1 time.sleep(timeout) GPIO.cleanup()
You’ll see that the led initially is turned of and when the button is pressed it’ll turn on.
Good now we can let go if the button and the led will turn back of right?
Wrong, once pressed the led will stay on until the code finishes.
Lets solve this, what we want to add is an else block. In the occasion the if statement is not met the code in the else block will be ran instead.
if GPIO.input(switch): GPIO.output(led,1) print("Switch is pressed") else: GPIO.output(led,0) print("Switch is not pressed")
If we re-run our python script we’ll see that when we release the button we’ll also disable the led.
Lets rethink our code a little and switch state when the button is pressed:
while(count < 500): if GPIO.input(switch): state = not state print("Switch is pressed", state) else: print("Switch is not pressed", state) GPIO.output(led,state) count = count +1 time.sleep(timeout)
As you can see if we detect that the switch is pressed we’ll change state. The ‘not’ in our code inverts the variable state that is initially 0.
Run this code and test before you continue reading.
Oke, so as you have seen the state changes every 200ms, this is because we didn’t take into account that we can only change the state once and not after every timeout.
We can correct this error by adding a single lined while loop (and empty one, using the pass statement of python) and changing the location of the output:
while(count < 500): if GPIO.input(switch): state = not state print("Switch is pressed", state) GPIO.output(led,state) while(GPIO.input(switch)): pass else: print("Switch is not pressed", state) count = count +1 time.sleep(timeout)
The pass statement does nothing and so the while loop will continue on waiting until you released the button.
So we now can control both input’s and output’s, next thin we want to be able to do is check out some more options then a led and a button.
In the next episode of Pi Electronics we’ll take a look at some other easy to handle IO like a relay, led matrix, button matrix.
If you have any question, pointers or remarks. Head over to our contact page and contact us from there. Don’t forget to like, share and head over to other tutorials.
Otherwise I’ll see you in our next tutorial!