The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 6 Physical computing with Scratch and Python 133

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


Next steps: reading a button
Outputs like LEDs are one thing, but the ‘input/output’ part of ‘GPIO’ means you can use pins
as inputs too. For this project, you’ll need a breadboard, male-to-male (M2M) and male-to-
female (M2F) jumper wires, and a push-button switch. If you don’t have a breadboard you can
use female-to-female (F2F) jumper wires, but the button will be much harder to press without
accidentally breaking the circuit.
Start by adding the push-button to your breadboard. If your push-button has only two legs,
make sure they’re in different numbered rows of the breadboard; if it has four legs, turn it so
the sides the legs come out from are along the breadboard’s rows and the flat leg-free sides
are at the top and bottom. Connect the ground rail of your breadboard to a ground pin of the
Raspberry Pi (marked GND on Figure 6-4) with a male-to-female jumper wire, then connect
one leg of your push-button to the ground rail with a male-to-male jumper wire. Finally, connect
the other leg – the one on the same side as the leg you just connected, if using a four-leg
switch – to the GPIO 2 pin (marked GP2 on Figure 6-4) of the Raspberry Pi with a male-to-
female jumper wire.


3V3
GP2
GP3
GP4
GND
GP17
GP27
GP22
3V3
GP10
GP09
GP11
GND
DNC
GP5
GP6
GP13
GP19
GP26
GND

5V
5V
GND
GP14
GP15
GP18
GND
GP23
GP24
GND
GP25
GP8
GP7
DNC
GND
GP12
GND
GP16
GP20
GP21

5 Figure 6-4: Wiring a push-button to the GPIO pins


Reading a button in Scratch
Start a new Scratch program and drag a when clicked block onto the scripts area.
Connect a set gpio to output high block, and select the number 2 from the drop-down to
match the GPIO pin you used for the push-button. Click on the down arrow at the end of the
block, then choose ‘input’ from the list to configure the pin as an input.

Free download pdf