SCRATCH
Controlling the Pi’s GPIO with Scratch
Les Pounder shows how we can use Scratch with simple electronics.
OUR EXPERT
Les Pounder is associate editor at Tom’s Hardware and a freelance maker for hire.
YOU NEED
Any Pi model Latest Pi OS 1x LED 1x 220 Ohm resistor (RED RED BROWN GOLD) 5x F2M jumpers Breadboard Push button 1x PIR sensor Get the code: https:// github.com/ lesp/LXF282- Scratch-GPIO-Basics/ archive/refs/ heads/main. zip
S cratch is meant for learning to code. It uses blocks to depict code and we can build sequences (algorithms) with relative ease.
If something goes wrong we can take it apart and diagnose our code. As we’ve covered in previous issues, Scratch can also be used with the GPIO of a Raspberry Pi to learn electronics and in this tutorial we shall learn how to work with inputs such as buttons and a PIR sensor to control simple outputs such as LEDs.
Don’t let the simplicity of this fool you. Adding the PIR sensor to our robot project (LXF279) and tweaking the code will enable our robot to detect an obstacle, and then move around it. But before we can do that we need to know the basics, so here we go!
Scratch 3 should come pre-installed on your Raspberry Pi OS image, but just in case it’s missing it can be installed from the main menu, under Preferences>Recommended Software. Scratch 3 is found in the Programming category; simply place a tick in the box and then click Apply to install. Once installed, Scratch 3 can be found in the main menu under Programming.