Controlling the Pi's GPIO with Scratch
Linux Format|November 2021
Les Pounder shows how we can use Scratch with simple electronics.
Les Pounder

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/LXF282ScratchGPIO-Basics/ archive/refs/ heads/main. zip

Scratch 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.

Open Scratch 3 – on first start Scratch may take a little while to open. We’re going to assume that you have an understanding of how to code with Scratch. If not, we covered the basics in previous issues. Before we write any new code we need to click the blue folder icon in the bottom left of the screen to load the Extensions menu. From there select Raspberry Pi GPIO and a palette of new blocks is added to our code.

Reading and control

The Raspberry Pi’s GPIO has at the most basic level two states. It can be on or off. We can replace on with True, High or 1, while off can be replaced with False, Low and 0. When a pin is high, it’s active with voltage and current (power) and it can be used to power a device or as part of a circuit where we read the state of a pin and then react to changes.

Continue reading your story on the app

Continue reading your story in the magazine

MORE STORIES FROM LINUX FORMATView All

Zephix 3.0

A fan of portable, modular distros, Mayank Sharma runs across a relatively new one and wonders how it stacks up against his all-time favourite: Slax.

3 mins read
Linux Format
December 2021

Whonix 16

Mayank Sharma is a busy man, but when it comes to privacy and security then, like Louis Armstrong, he has all the time in the world.

3 mins read
Linux Format
December 2021

UBUNTU VS FEDORA

Jonni Bidwell wants to know everything – and he means everything – about the two most popular Gnome-based distros…

10+ mins read
Linux Format
December 2021

SERIAL PERIPHERAL INTERFACE: Driving SPI displays

Let’s go on an adventure into the SPI protocol, LCD displays and model trains with Sean Conway toot-tooting away!

5 mins read
Linux Format
December 2021

Sensor and motor control with the GPIO

Les Pounder shows how we can take Scratch further with a community-developed tool that builds on our Scratch knowledge.

7 mins read
Linux Format
December 2021

LICENCE TO BILL

Jonni Bidwell has been reading licence minutiae and fears big business threatens the future of copyleft and software freedom.

10+ mins read
Linux Format
December 2021

Devuan GNU+Linux

The backlash against Systemd resulted in this sysvinit-friendly distro, now on version 4. Mayank Sharma assesses its progress.

3 mins read
Linux Format
December 2021

AUTOMOTIVE GRADE LINUX

Mats Tage Axelsson shows how open source frameworks and Linux can help navigate you safely to your coding destination.

10+ mins read
Linux Format
December 2021

Atari VCS

Monetising nostalgia should be an easy win for Atari so Matt Safford can‘t believe it’s created another E.T.

3 mins read
Linux Format
December 2021

How To Create Efficient Backups

Backups aren’t just a safety net for Trekkies such as Shashank Sharma, but a way of life. But they can take up quite a lot of space if not done right…

6 mins read
Linux Format
November 2021
RELATED STORIES

United States Of America - Washington, D.C.

Our nation’s capital is known for its history, elegant hotels and the finest restaurants.

3 mins read
Global Traveler
July 2021

Creative SXFI Gamer

Creative’s bold shot at a competitive gaming headset

3 mins read
Maximum PC
June 2021

CRAVE

THE GEAR WE’RE LUSTING AFTER

2 mins read
Mac Life
April 2021

How to Grow Better Space Lettuce

NASA is funding research on a technique that could increase the yields of extraterrestrial crops

3 mins read
Bloomberg Businessweek
March 29 - April 05, 2021

Hi-Tech Lighting - Installing A LED Headlight!

All headlights are not created equal; they are available in four basic light sources: incandescent, quartz halogen, HID (high-intensity discharge) and LED (light-emitting diode). Some riders think that brighter headlights are better, when in fact it is the color temperature of the light source that gives a rider the ability to see objects at night. A whiter light gives a rider a greater depth of view at night. Incandescent and quartz halogen lights have a yellow cast and HID lights have a blue beam, while hi-output LEDs emit a true white light; their light color is the closest to sunlight’s color.

2 mins read
Born To Ride Southeast Magazine
February 2021

COLDER MONTHS CAN BE PERFECT TIME TO PHOTOGRAPH GARDENS

The coronavirus has led more people to spend time outdoors, and many find themselves walking the same paths and gazing at the same trees or shrubs day after day.

3 mins read
Techlife News
Techlife News #472

‘BLACK PANTHER' BOSEMAN'S $12M LEGACY OF LOVE

Why dying hunk married Simone

1 min read
National Enquirer
September 21, 2020

Linsey Godfrey

First Soap Role: Caroline Spencer, B&B, 2012-18

4 mins read
Soap Opera Digest
August 03, 2020

Cat Clyde

Talks Nature, Standing up For Equality + New Music

8 mins read
Glitter Magazine
Eris Baker

Better Off Live

Five times a live version was better than the original studio recording

3 mins read
Guitar World
August 2020