One powerful feature of the Raspberry Pi is the row of GPIO pins along the top edge of the board. GPIO stands for General-Purpose Input/Output. These pins are a physical interface between the Raspberry Pi and the outside world. At the simplest level, you can think of them as switches that you can turn on or off (input) or that the Pi can turn on or off (output).
The GPIO pins allow the Raspberry Pi to control and monitor the outside world by being connected to electronic circuits. The Pi is able to control LEDs, turning them on or off, run motors, and many other things. It's also able to detect whether a switch has been pressed, the temperature, and light. We refer to this as physical computing.
There are 40 pins on the Raspberry Pi (26 pins on early models), and they provide various different functions.
If you have a RasPiO pin label, it can help to identify what each pin is used for. Make sure your pin label is placed with the keyring hole facing the USB ports, pointed outwards.
If you don't have a pin label, then this guide can help you to identify the pin numbers:
You'll see pins labelled as 3V3, 5V, GND and GP2, GP3, etc:
3V3 | 3.3 volts | Anything connected to these pins will always get 3.3V of power |
5V | 5 volts | Anything connected to these pins will always get 5V of power |
GND | ground | Zero volts, used to complete a circuit |
GP2 | GPIO pin 2 | These pins are for general-purpose use and can be configured as input or output pins |
ID_SC/ID_SD/DNC | Special purpose pins |
WARNING: If you follow the instructions, then playing about with the GPIO pins is safe and fun. Randomly plugging wires and power sources into your Pi, however, may destroy it, especially if using the 5V pins. Bad things can also happen if you try to connect things to your Pi that use a lot of power; LEDs are fine, motors are not. If you're worried about this, then you might want to consider using an add-on board such as the Explorer HAT until you're confident enough to use the GPIO directly.
You can test whether your GPIO pins and LEDs are working by building the circuit below. You can use any resistor over about 50Ω.
-
The LED is connected directly to the 3V3 pin and the GND pin via the 330 Ohm resistor, and should light up.
-
Be sure to connect your LED the correct way round; the longer leg should be connected to the 3V3 pin:
-
To control the LED, you'll need to adapt your circuit to use a switchable pin.
-
In the diagram below pin 17 has been used, but you can use any numbered pin you wish.
- Locate the Scratch program by clicking on Menu followed by Programming, and selecting Scratch.
- The familiar Scratch interface will then load:
- Click on Control in the top-left display. Drag the
when GreenFlag clicked
block onto the scripts area:
- Scratch uses broadcast blocks to communicate with the GPIO pins; the first broadcast you need is
gpioserveron
which activates the GPIO functionality:
- As your GPIO pin can be used as either input or output, you'll need to specify in which mode your pin is being used with the
config17out
broadcast:
- From this point on, you can control your LED using two broadcasts:
gpio17high
to turn it on andgpio17low
to turn it off. Using these two messages and some pauses, you can make an LED flash continuously:
-
As well as controlling the physical world, you can react to it using an input device such as a button.
-
Connect your button to a breadboard, then connect one pin to a ground pin and the other to a numbered GPIO pin. In this example pin 2 has been used:
-
Before Scratch can react to your button, it needs to be told which pin is configured as an input pin.
-
Assuming you have started a new Scratch file, you'll also need to start the GPIO server. The following code will configure pin 4 as an input:
-
Once you have built the code above, you need to click the green flag in order for it to run and for your pin to be set up.
-
Next, you need to go to the Sensing menu in Scratch:
- From here you need to find the block and click the triangle to reveal a menu. Select gpio2 from the menu and click the tickbox to the left:
- You should now see the current state of the pin in the stage area:
- Now when you press your button, the state should change from 1 to 0.
-
Now that your button is all set up and working, you can make it do something. You can start off by making it control a sprite.
-
Begin with a
forever
loop with anif
block inside it. This will continually check theif
condition and perform some action if the condition is met. The action in the example below will make the current sprite move forward: -
Finally, to make this work you need to add the condition, which is that we want the move action to happen when the button value = 0:
If everything is correct, your button should make the sprite move.
To finish off, you can combine your two programs so that the button can turn the LED on and off.
-
Adapt your script and use an
If Else
block so that it looks like the example below: -
Now when you push the button, the LED should light up.
There are lots of other things you can control or monitor with your Raspberry Pi. Have a look at the worksheets below, to see how easily this can be done.
Using an active buzzer
Making traffic lights
Using a PIR sensor