ScratchGPIO – Add on Modules/Boards

Scratch Controlling the GPIO Pins on a Raspberry Pi
Part 4 of 4

Ultrasonic Sensor Boards ultrasonic1pinSingle Pin Ultrasonics – if you connect a cheap 4 pin Ultrasonic Module as per this diagram, then you only need one spare GPIO pin to trigger it and receive the returned pulse

So now you simply use it (assuming connected to pin 23)

Use Sonar23  followed by a wait 1 and then just use the sensor item sonar23 to get the distance measured in cm.  Any pin can be used for this purpose and if you had several of them you could broadcast each of them in turn.

Ultra8Alternatively, you can use this syntax which will tell your Raspberry Pi to continuously do a distance check every 1 second and update sensor item Ultra8 with the results without any further need to do another broadcast

Gordon’s Ladder Board
This cheap and useful educational board (link) is an ideal introduction to getting kids to turns things on and off and saves having to wire up a breadboard to get going.  It does need someone to solder it together and if you don’t have that skill or tools – jsut give a shout out at a RaspberryJam and I’m sure someone will help you out 🙂

LadderAdOnIf you’ve got one of these then just create a variable called AddOn and set it to Ladder as the first line in your Green Flag event. This tells the system that your using one and then you can use broadcasts such as:

ld2This script will simply turn all the LEDs out and then just switch on every other one.

ld3To vary the brightness of a LED, you can use a Power variable like this example:

The state of the switches can be accessed through the sensing blocks.ld4 Scroll down to the bottom and click on the arrow next to slider and you should see switch1 – 4 (they will only appear after you have  run a Green Flag event as above)


This board makes it very easy to control Motors and Servos without any extra hardware as well as giving you the ability to switch your Raspberry Pi on and off and power it from AA batteries.


See for full support info


CompassYou can plug in a HMC5883L based I2C device and access the magnetic heading value it outputs.  Simply set AddOn to Compass and then use the heading sensor value which is updated 4 times every second


This really cheap and useful board is an alternative to @drogon LadderBoard.

It has 6 LEDs, a switch and a buzzer

To use it just set the AddOn variable to Berry and then you have easy methods of accessing it

Broadcasts available
[Led1..6] [On/Off/High/Low] e.g broadcast Led1On will make first red led light up

[All] [On/Off/High/Low] e.g broadcast AllOn will make all LEDs and the buzzer come on.

[Buzzer] ]On/Off/High/Low] e.g broadcast BuzzerOn

You can use variables instead e.g Set Led1 On will turn on 1st LED
And also use Power1..6 (values 0-100) variables to vary the brightness as

There is only one sensor value returned and that is called switch (1 if not pressed – 0 if pressed)


This board is a complete robotics controller for your Raspberry Pi based mobile robot.

Scratch GPIO Version 2 – Using Stepper Motors

Stepper Motors


However, if you would like to build a neat little vehicular robot then you can do so by connecting up a pair of cheap Stepper Motors through a couple of very cheap ULN2003s.

The cheap 5V steppers that are widely available (BY28) don’t turn very fast (maximum about 12 RPM) which actually makes them idea as beginners motors as your vehicle won’t run away during testing 🙂

These motors have 5 connections, 1 for power and the rest to control the stepping.  (Their technical name is 5 Wire Unipolar Stepper Motor)

To use them in Scratch, you need to connect them up as follows

Stepper Motors Battery_bb

StepperA should end up (via the ULN2003 buffer of course)  being controlled by pins 11,12,13 and 15 – Stepper B  is via pins 16,18,22 and 7.

Pin 9 of the ULN2003s should be connected to +ve on the motor battery pack.

(The battery +ve MUST NOT be connected to any pins on the Pi)

stepperIn your Scratch program, you need to say that you are using Stepper motors instead of ordinary motors this is simply done by broadcasting Stepper in your Green Flag code block.

You can then simply using variables MotorA and MotorB as before with simple DC motors but this time they will each control 4 pins at a time.


The advantage of using stepper motors is that they can be just as easily stepped backwards as forwards.  So to make MotorA go backwards at full speed simply use set MotorA to -100

Position Control
As well as treating the stepper as a continuously rotating motor, you can simply tell the stepper to change its “position” by a number of steps.

e.g. To turn 500 steps (approx 180 degrees on my motors) use posa

Note the use of change and not set

Part 4 – Direct Support for External Modules/Boards

Advanced Stepper Control

Stepper motors need a delay between each step – this is currently set to 0.003 secs.   You can control this value by using a variable called StepDelay.

You can also change the type of stepping mode from 2Coil (default value that gives maximum turning force) to 1Coil or HalfStep