Physical Computing With Scratch
Physical Computing With Scratch
Step 1 Introduction
In this project you will learn how to control various electronic components using the latest version of Scratch on the
Raspberry Pi.
What you will make
What you will need
Hardware
A Raspberry Pi 4
An LED
A 47 - 330 ohm resistor
A tactile button
A buzzer
A PIR motion sensor
Male to Female jumper leads
Female to Female jumper leads
Software
Scratch 3 - if not already installed, open a terminal and type:
Downloads
A completed project file can be found here (http://rpf.io/p/en/physical-computing-with-scratch-get).
Before you begin controlling Light Emitting Diodes (LEDs) with code, it is a good idea to test all your hardware, to make
sure it is working.
The Raspberry Pi’s General Purpose Input/Output (GPIO) pins can be used to power a simple LED circuit.
Note that the long leg of the LED needs to be connected to the 3.3V pin. The short leg goes to a GND pin
The LED should now be on, so check your wiring if it does not light up.
Step 3 Using a switchable pin
To control the LED, you’ll need to adapt your circuit to use a switchable pin.
The LED should switch off, as this pin is not currently on.
when clicked
turn LED 17 on
wait 1 seconds
turn LED 17 off
Can you make the LED flash on and off repeatedly?
I need a hint
Here’s what the completed script would look like.
when clicked
forever
turn LED 17 on
wait 1 seconds
turn LED 17 off
wait 1 seconds
Step 4 Connecting a button
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:
I need a hint
Here are the completed scripts
You can control other electronic components using Scratch 3 on the Raspberry Pi as well.
To do this you need to add another extension.
Connecting a buzzer
An active buzzer can be connected just like an LED, but as they are a little more robust, you won’t be needing a resistor
to protect them.
Set up the circuit as shown below:
Coding a buzzer
Now you can sound the buzzer by using the code below:
when clicked
forever
set gpio 17 to output high
wait 1 seconds
set gpio 17 to output low
wait 1 seconds
The pulse emitted when a PIR detects motion needs to be amplified, and so it needs to be powered. There are three
pins on the PIR; they should be labeled Vcc, Gnd, and Out. If these labels aren’t clear, they are sometimes concealed
beneath the Fresnel lens (the white cap), which you can temporarily remove to see the pin labels.
As shown above, the Vcc pin needs attaching to a 5V pin on the Raspberry Pi.
The Gnd pin on the PIR sensor can be attached to any ground pin on the Raspberry Pi.
Lastly, the Out pin needs to be connected to any of the GPIO pins.
The PIR acts a little like a button. Add the following code, so that the sprite can detect any motion in your
area.
when clicked
set gpio 17 to input pulled low
If your PIR doesn’t seem to work, then you might like to try tuning it a little.
Trigger jumper
The trigger jumper is a small metal and plastic rectangle that can be removed and placed over a set of three
pins.
The trigger jumper can be set to two positions. In the diagram above, it is set to single trigger mode. This
means that the PIR sensor will send signals when it detects motion, but will not signal again if it detects motion
again within the delay time.
If you move the jumper, it will be set to repeat trigger mode. In this mode, the PIR sensor sends signals when it
is triggered, and if it detects motion again within the delay time, it will signal the Pi again, and the delay time will
be reset.
Usually, you will want to use the PIR sensor in repeat trigger mode.
Challenge!
Why not have a go at some of our other physical computing projects using Scratch (https://projects.raspberr
ypi.org/en/projects?software%5B%5D=scratch&hardware%5B%5D=electronic-components)
Or maybe have a play with the SenseHAT using Scratch (https://projects.raspberrypi.org/en/projects?softw
are%5B%5D=scratch&hardware%5B%5D=sense-hat)