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.

You may also like...

27 Responses

  1. Whenever I’ve used an SR-04 before, I found that you need one pin to trigger and another pin to receive. Have I missed something somewhere?

  2. David Ferguson says:

    Could you please tell me the values of the resistors used to wire up the ultrasonic sensor?

  3. Chris Wilde says:

    Hi Cymplecy,
    I’m getting a bit stuck with the Ultrasonic module. I’ve got it connected up as per the diagram, and have added the broadcast in scratch, however I’m unable to find the sensor item Sonar23. I’m assuming it should appear as a sensing block, but I can’t seem to find it. Any ideas where I’m going wrong?
    PS, your work is fantastic!!

  4. Chris Wilde says:

    Hi Simon,
    I’ve actually left it at work, but will have a try at some point tomorrow, and let you know how it goes.
    Thanks for the support thusfar

  5. davidms49 says:

    I cannot get the variables to work with the Berryclip as described, broadcast is fine, any suggests of what I am doing wrong ?

    ‘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’

  6. Simon Watts says:

    Hi Simon
    I accidently left a post on the useful links instead of here! Anyway I was just wondering if I cab use a DS18B20 temperature sensor with Scratch GPIO.

  7. Etienne says:

    Thx for your work !
    Can you make a complete explemple for the Ultrasonic Sensor Boards ?

  8. Etienne says:

    Thx for answer !
    I have used the pin number 8 (GPIO14)

    I have made :
    On GreenFlag
    broadcast “Sonic8”
    wait 1 sec
    say (“——^” sensor value) for 2 secs <- but i can't select sonic8 in the scroll menu :s

  9. Etienne says:

    I have Install ScratchGPIO5 software with :
    sudo wget -O
    sudo bash

    I have run it on the desktop and now :
    I can select the sensor in the scroll menu but when i say it i always respond “299”
    Do you have an idee ?

  10. Etienne says:

    Thank for your help, I have resolve my problem 🙂
    it was the sensor who was defective
    I have changed it and now it work 🙂

    To make it work i have do that :

    On GreenFlag
    broadcast Ultra8
    say ultra8 sensor value for 2 secs

    I have connect the sensor to the pin 8 (GPIO14)


  11. Andrew says:

    I bought my son an IWM Raspberry Pi Avoidance Robot Smart Car. This works with Python. The ultrasonic sensor is connected to GPIO17 – pin11.

    We have tried to get this to work with Scratch, as my son prefers it to python, but with no success. We have downloaded the latest version of Scratch GPIO and we have been able to turn LEDs on and off (broadcast pin11on) and detect external buttons (set button to pin 26 sensor value). But we cannot get the ultrasonic sensor to work (have not moved on to the motor yet – easy part first!)

    From your article it is not clear what we should do to use an ultrasonic sensor on pin11. We tried moving the connection to GPIO 23 – pin 16 so that it matches with your code example (broadcast Sonar23). No effect.

    Could you please supply some sample code showing what should work?

Leave a Reply to cymplecy Cancel reply

Your email address will not be published. Required fields are marked *