Linux Format

Build robots with Scratch

Take your first motorised step with Les Pounder as he shows you how to use Scratch to build and control a simple robot.

- OUR EXPERT Les Pounder is associate editor at Tom’s Hardware and a freelance maker for hire. He blogs about his projects and adventures at bigl.es

Les Pounder shows you how to use Scratch to build and control a simple robot.

Robots are very cool and the Raspberry Pi GPIO is the ideal way to control them. We can do this using Scratch. In this tutorial we shall learn the basics of working with the GPIO in Scratch by first controllin­g an LED before applying the same principle to control the motors of a robot. If you’re unsure as to how to build a robot, we have a short guide in the boxout which may just help. So let’s build a robot!

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 Preference­s>Recommende­d Software. Scratch 3 is found in the Programmin­g category; place a tick in the box and click Apply to install. Once installed, Scratch 3 can be found in the main menu under Programmin­g.

Open Scratch 3 (on first start Scratch may take a little while to open). We’re going to assume that you have an understand­ing of how to code with Scratch, (if not see LXF180/1, 207, 209, 277/8). 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.

Our first project will get us familiar with the basics of using the GPIO with Scratch. Our focus moves to the Raspberry Pi itself and our electronic­s. In a breadboard, insert the LED and make a note of which leg is shorter. In the shorter leg side, connect one leg of the resistor.

The other leg can be placed in another row of the breadboard. Using two jumper jerky wires, connect the leg of the resistor to any GND pin on the Pi. Connect the other leg of the LED to GPIO17. Please see the highresolu­tion diagram in the download for this issue.

Moving back to Scratch and we start the code by dragging When Green Flag is Clicked from Events into the coding area. Next we drag repeat 10 from Control and connect that to the previous block. The remaining blocks will be inserted inside the repeat 10 loop. From Raspberry Pi GPIO drag two Set GPIO 0 to output high and place them in the loop. Change the first to read Set GPIO 17 to output high and the second to Set GPIO 17 to output low . These two blocks will turn the LED connected to GPIO 17 on and off 10 times. But to see the change we need to place a Wait 1 Second from control in between these blocks, and another after turning the LED off. When you’re ready, click the Green Flag above the stage to run the code. The LED should blink 10 times, on for one second, off for another second.

This test has confirmed that our GPIO is working with Scratch 3, and that we understand the principles of turning a GPIO pin on and off. This is digital IO control and is the most basic electronic concept.

Controllin­g motors

Robotics is a great way to learn coding. However, the one problem is that the Raspberry Pi’s GPIO can’t directly control a motor because it requires more power than a GPIO pin can safely provide. We instead control a

motor driver board that sends power from batteries to the motors. We chose Cytron’s Maker Drive motor controller because it’s clearly labelled and has test buttons for checking motors without any code.

Connect the GPIO of the Raspberry Pi to the M inputs using the female to female wires, as follows:

GPIO 17 > M1A

GPIO 27 > M1B

GPIO 23 > M2A

GPIO 24 > M2B

Now connect the motors to the screw terminals for M1A/B and M2A/B. It doesn’t matter which wire goes in which terminal. Connect the AA battery holder into VB+ (Red, + wire) and VB- (Black, - wire), then insert the four batteries. Press the four buttons on the Maker Drive board to test the motors work on battery power.

Delete the code from the previous project. Our first task is to set up the GPIO pins used to control the motors. When the code starts we need these motors to be off, otherwise we’re chasing a robot around the desk!

Drag When Green flag is clicked from Events into the coding area. Next drag four set GPIO 0 to output high from Raspberry Pi GPIO and connect them all under the Green flag block. Change the value of 0 in each block to 17, 27, 23 and 24 and set them all to low. This sets all of the GPIO pins for the motors to off.

Next we create a sequence of code that will stop the robot from running away by turning the motors off. Go to Events and drag When Space key pressed into the coding area. To stop the motors we turn them all off, and we can reuse the four blocks from the When Green flag setup script. Right-click those bricks and select Duplicate. This copies all of the blocks in the sequence, but we can throw away the green flag block. Connect these blocks to the When Space key pressed block.

To make the robot move forward we take another

When Space key is pressed from Events, changing the Space to Up Arrow , and then duplicate the four blocks that control our GPIO. To move forward, the robot has two wheels: one wheel will go clockwise, the other anti-clockwise. To do this we set GPIO 17 and 24 high, and 27, 23 low. Give the code a test. Press the up arrow to move the motors forward, then press Space to stop. Did the motors move as expected? If not, unscrew the terminal on the incorrect motor and swap the wires over, for example swap M1A for M1B, and M1B for M1A.

To make the robot move backwards we need to duplicate the previous blocks, and change the Up Arrow to Down Arrow and then set GPIO 17 and 24 low, 27 and 23 high. Give it a test – does the robot behave as you expected? If not, switch the motor screw terminals until you find the correct movement.

To move left, duplicate the blocks used for backwards movement. Change Up Arrow to Left Arrow and then alter GPIO 17 and 24 high, 27 and 13 low. Give it a test – does the robot behave as you expected? If not, switch the motor screw terminals until you find the correct movement.

To move right, duplicate the blocks used for left movement. Change Left Arrow to Right Arrow and then alter GPIO 27 and 23 high, 17 and 24 low. Does the robot behave as you expected? If not, switch the motor screw terminals until you find the correct movement. Save the code (File>Save) and take your robot for a drive and hopefully not off the table!

Double-clicking on a block will activate it. This can be handy for quickly testing functional­ity and it can be annoying when done accidental­ly. Pressing Stop should stop all the blocks from running.

 ??  ?? The code to control our robot is relatively simple. We need only a few blocks – many of which are duplicated – to digitally control the motors.
The code to control our robot is relatively simple. We need only a few blocks – many of which are duplicated – to digitally control the motors.
 ??  ??
 ??  ?? This is all the code that we need to understand how to turn an LED on and then off. Using this principle we can control a motor, duplicate it, control two motors and then build a simple robot.
This is all the code that we need to understand how to turn an LED on and then off. Using this principle we can control a motor, duplicate it, control two motors and then build a simple robot.
 ??  ?? Use these Scratch instructio­ns to move your robot. Hopefully it all works as expected. But beware: this is how the robot uprising starts!
Use these Scratch instructio­ns to move your robot. Hopefully it all works as expected. But beware: this is how the robot uprising starts!

Newspapers in English

Newspapers from Australia