Sensor and motor control with the GPIO
Linux Format|December 2021
Les Pounder shows how we can take Scratch further with a community-developed tool that builds on our Scratch knowledge.
Les Pounder

YOU NEED

Pi model 3/4/400

The latest Pi OS HCSR04-P

Ultrasonic sensor

L9110S motor controller DC motor

Micro USB breakout PSU for motor

7x F2F wires

2x M2F wires

Internet

Get the code https:// github.com/ lesp/ S3GPIOLXF283/ archive/ refs/heads/ main.zip

Over the past few issues we’ve learnt how to create games, control electronics and create shapes by calculating angles, all with Scratch 3. As good as Scratch 3 is there’s only so much that we can do with the GPIO, and the GPIO is the best feature of the Raspberry Pi. So how can we take our Scratch GPIO projects further? Well, for that we need S3GPIO.

S3GPIO is short for Scratch 3 GPIO and it was created by Simon Walters, a Raspberry Pi community member who’s campaigned for children to learn coding and electronics with Scratch. Walters first foray into the GPIO for Scratch was ScratchGPIO, based upon Scratch 1.4. This version is still active – in fact we can find out more at http://simplesi.net.

In this tutorial we’ll install S3GPIO, learn how to use an ultrasonic sensor as a controller for a DC motor, effectively setting the speed based on our distance from the sensor.

Connecting the electronic components is relatively simple, but the amount of wires may seem confusing. We’ll break it down into sections. S3GPIO uses physical pin numbering, not the BCM numbering standard set by the Raspberry Pi Foundation. This means that we have two columns of 20 pins. Holding the Pi with the USB ports pointing to the floor, the two columns start with pin 1 at the top left, and pin 2 at the top right. The left column is the “odd” column, with pins following an odd number pattern: 1, 3, 5, 7, 9 etc. The right column is the “even” column, with numbers progressing 2, 4, 6, 8 etc.

Our HCSR04-P ultrasonic sensor has four pins. Using female to female jumper wires, connect GND to any GND pin on the Raspberry Pi, Vcc to 3V (Pin 1). Trigger to pin 11, and echo to pin 13.