A block-based language designed for kids, Scratch is a great way to introduce coding. With it, we can learn to create games and control electronics through the Raspberry Pi’s GPIO. As good as Scratch 3 is, there is little we can do with the GPIO, and the GPIO is the best. characteristic of the Raspberry Pi. So how can we go further in our Scratch GPIO projects? For this we need S3GPIO.
S3GPIO is Scratch 3 GPIO and it was created by Simon Walters, a member of the Raspberry Pi community who campaigned for kids to learn coding and electronics with Scratch. Walters’ first foray into the GPIO for Scratch was ScratchGPIO, based on Scratch 1.4. This old version is still available, but based on an older version of the language.
In this manual, we will install S3GPIO and learn how to use an ultrasonic sensor as a controller for a DC motor, effectively adjusting the speed according to our distance from the sensor. We will also learn how to create an emergency stop for the engine and how to incorporate the engine control into a code sequence.
All of the tutorial steps can be used with multiple motors, we just need to connect two more GPIO pins to the L9110S in order to independently control the motors.
For this project you will need
- A Raspberry Pi 3/4/400
- The latest Raspberry Pi OS
- Ultrasonic sensor HCSR04-P (3V model of HCSR04)
- L9110S Motor Controller
- Motor doctor
- External micro USB power supply for motor
- 7 x female to female wires
- 2 x male to female wires
- An Internet connection
The complete code for this project can be downloaded from our Github repository.
Connecting electronic components is relatively straightforward, but the amount of wires can seem confusing. We will divide it into sections. S3GPIO uses the physical pin numbering, not the BCM numbering standard set by the Raspberry Pi Foundation. This means we have two columns of 20 pins. Holding the Pi with the USB ports pointing toward the ground, the two columns start with pin 1 on the top left and pin 2 on 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 increasing from 2,4,6,8 and so on.
1. Using female to female jumpers, Connect the HC-SR04P ultrasonic sensor to the Raspberry Pi as follows:
- GND to any GND pin of the Raspberry Pi
- Vcc to 3V (pin 1)
- Trigger on pin 11
- Echo pin 13.
2. Connect the motor terminals to the MOTORA terminals (no matter which way) of the L9110S. Screw them in place so that they are secure.
3. Connect GND on the L9110S to any GND pin on the Raspberry Pi. This creates a common ground of reference, necessary for the proper functioning of the project.
4. Connect the + of an external 5V micro USB power supply to the VCC of the L9110S engine control device.
5. Connect the – (GND) pin of the power supply to any GND pin of the Raspberry Pi.
6. Connect two female to female jumpers that connect the A1-A and A1-B terminals of the L9110S to pins 8 and 10 of the Raspberry Pi. This means we can use the GPIO to control the L9110S, which makes the engine roar.
We now have an externally powered motor, which is connected to the Raspberry Pi, which allows us to control it from S3GPIO. We should also have a HCSR04-P ultrasonic sensor connected and ready to be used as a speed controller. Please check the diagram for this project to make sure your connections are correct.
Installation of S3GPIO
1. Turn on your Raspberry Pi and open a terminal. Enter the following command to download an installation. Note that this command will download the installer, which is usually a big security risk for production machines, but in this case it is safe.
wget https://git.io/vMS6T -O isgh8.sh
2. Run the command to install S3GPIO.
sudo bash isgh8.sh
3. Restart your Raspberry Pi for the changes to take effect.
4. Double left click on the S3GPIO icon and select “Run in terminal” to launch three applications. A web server, ScratchGPIO, and Chromium browser with a special S3GPIO extension will all be launched, and it may take a few moments until you see S3GPIO.
5. Click on “See inside” to open the online S3GPIO editor.
S3GPIO has the same interface as Scratch 3, but it also has a series of S3GPIO blocks in “My Blocks” that we will be using.
1. In the Events section, Drag “When Green Flag Clicked” into the encoding box. This is the start of our code and will test the HCSR04-P ultrasonic sensor.
2. From My Blocks, drag the S3GPIO command and connect it to the previous block. In the white box, type ultra11,13 These are the pins for the trigger and the echo on the sensor.
3. Drag in a Forever Loop Control and connect it to the previous blocks.
4. From My Blocks, slide S3GPIO read and put it in the eternal loop. In the white box, type ultra13 this will read the ultrasonic sensor’s echo pin, and behind the scenes it will be converted to a measurement in centimeters.
5. From appearances, drag a Say hello! to block and place it under the previous one. From variables, drag sensor and place it on the “Hello!” this will indicate the distance reading from the ultrasonic sensor.
6. From My Blocks, drag S3GPIO set to _ to _ and place it under the previous block. In the first blank, type Power8, and in the second place another sensor block from Variables. This will set the motor pin connected to pin 8 to the value returned by the sensor.
7. From Control, Drag a 1 second wait block, connect it to the previous block and edit the block to wait 0.1 second. Your code should now look like this.
Once the code is complete, let’s go click on the green flag to start. The engine should come to life. Now bring your hand closer to the two “eyes” of the sensor, the motor will slow down. Click on the stop button (top right of the scene) but the engine will not stop! We have to put a brake on.
Add more to the code: build a brake
1. In a new section of code, place a block When the space key is pressed, found in Events.
2. From My Blocks, drag two S3GPIO blocks defined _ to _ and link them to the previous one. Change the first S3GPIO set on Power8 to 0, and the second to Power10 to 0.
3. From Control, drag Stop all and place it under the previous block. Your code should now look like this.
Click on the green flag and check that the sensor responds to your hand. The motor should go fast or slow depending on where your hand is. When you’re ready, press the space bar to stop the engine.
Add more to the code: create a sequence
Besides using the sensor, we can control the motor with the help of a series of instructions, a sequence. This sequence will turn the motor in one direction, then reverse before stopping the motors permanently. The sequence will iterate (loop) 10 times before ending and wait for the user to trigger the sequence again.
1. From the events, Drag When the space key is pressed and start a new code sequence. Replace the space key with an r using the drop-down list.
2. From Control, slide repeat 10 and place it under the previous block.
3. From My Blocks, drag two S3GPIO command blocks and place them inside the loop. Change the first to Pin 8 On, and the second to pin 10 disabled.
4. From Control, drag Wait 1 seconds and place it under the previous blocks inside the loop.
5. Right click on the top S3GPIO block and select duplicate. It will duplicate the three blocks we just created. Place these three new blocks inside the loop, under the previous one. In these blocks, change pin 8 to Off, and Pin 10 Off to On.
6. Repeat the duplicate for these three blocks and place the three newly duplicated under them, still in the loop. Define S3GPIO commands so that both are turned off.
This project first appeared in an issue of Linux format magazine.