0% found this document useful (0 votes)
29 views17 pages

Physical Computing With Scratch

This document provides instructions for a project to control electronic components like LEDs, buttons, buzzers and motion sensors using Scratch on a Raspberry Pi. The steps guide the reader on how to set up circuits and write code to make the components respond to events and each other. Challenges encourage applying the skills to build a basic burglar alarm.

Uploaded by

Leshynez Valoare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views17 pages

Physical Computing With Scratch

This document provides instructions for a project to control electronic components like LEDs, buttons, buzzers and motion sensors using Scratch on a Raspberry Pi. The steps guide the reader on how to set up circuits and write code to make the components respond to events and each other. Challenges encourage applying the skills to build a basic burglar alarm.

Uploaded by

Leshynez Valoare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Projects

Physical Computing With


Scratch
A guide to getting started with physical computing and
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:

sudo apt install scratch3

Downloads
A completed project file can be found here (http://rpf.io/p/en/physical-computing-with-scratch-get).

What you will learn


How to control and LED with Scratch
How to detect button presses with Scratch
How to detect motion with Scratch

Additional information for educators


If you need to print this project, please use the printer-friendly version (https://projects.raspberrypi.org/en/pro
jects/physical-computing-with-scratch/print).
You can find the completed project here (http://rpf.io/p/en/physical-computing-with-scratch-get).
Step 2 Lighting an LED

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.

You can buy a pin guide (https://thepihut.com/products/gpio-reference-board-model-b-plus-and-a-plus?ref=i


sp_rel_prd&isp_ref_pos=5) to help you identify the GPIO pins, use the image below, or open a terminal and type
pinout to see the numbers of all the pins.

Connect your LED and resistor into Raspberry Pi as shown below.

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.

Move the lead that is connected to 3.3V to GPIO 17.

The LED should switch off, as this pin is not currently on.

Open Scratch 3 from the menu.


To use the GPIO pins, you need to add in the Raspberry Pi Simple Electronics extension
Now you can create a script to turn the LED on and off.

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:

Test that your button is working using this simple script

when button 2 is pressed


say hello for 2 seconds

Press the button and the sprite should say hello.


Can you use the extension to control an LED using a button?

I need a hint
Here are the completed scripts

when button 2 is pressed


turn LED 17 on

when button 2 is released


turn LED 17 off
Step 5 Buzzers

You can control other electronic components using Scratch 3 on the Raspberry Pi as well.
To do this you need to add another extension.

Add the Raspberry Pi GPIO extension in Scratch 3

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

Or even control it with your button

when gpio 2 is high


set gpio 17 to output high

when gpio 2 is low


set gpio 17 to output low
Step 6 Passive infrared motion sensor (PIR)

Humans and other animals emit heat all the time.


A PIR sensor detects changes in the amount of IR radiation (heat) it receives. When there is a change, then a pulse is
triggered. This means that a PIR sensor can detect when a human (or any animal) moves in front of it.

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.

Wire up your PIR sensor to your Raspberry Pi

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

when gpio 17 is high


say you moved

when gpio 17 is low


say you're still

If your PIR doesn’t seem to work, then you might like to try tuning it a little.

Tune a PIR sensor


When you first connect your PIR sensor, you need to wait for about a minute before trying to detect motion.
There are three ways in which you can tune and adjust a PIR sensor.
Sensitivity
You can adjust the PIR sensor’s sensitivity so that it picks up motion at different distances. Turn the
potentiometer fully clockwise, and the sensor will detect motion up that’s to about 3 metres away. Turn the
potentiometer fully anti-clockwise, and the sensor will detect motion that’s up to about 7 metres away.
Delay time
The delay time is the amount of time that the PIR sensor send signals to the Pi for once it is triggered. This is
adjustable from about 3 seconds up to about 300 seconds. It will probably best if you turn this potentiometer
fully anti-clockwise to keep the delay time as short as possible.

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!

Challenge: Make a burglar alarm


Now that you know how to use LEDs, buttons, buzzers and a PIR. Why not try and make your very own burglar alarm.
1. When motion is detected in the room, the LED should flash and the buzzer should beep.
2. Only pushing the button will turn off the LED and the buzzer.
Step 7 What next?

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)

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons license (htt


ps://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/physical-computing-with-scra
tch)

You might also like