ScratchGPIO Documentation

This post is out-of-dat.-  Please update to latest version

This post is to hopefully document all the available broadcasts and variable usage that ScratchGPIO understands and responds to


Scratch Variable “bug”

Although using variables is necessary once you start writing complex scripts, there is a “bug” or a “feature” (depending on yourpoint of view) in the way that Scratch sends out variable changes to any external programs.

If you say broadcast pin11On then Scratch will always send Pin11On out to any listening programs

But if you use a variable called Pin11 and say set Pin11 to On then it only sends this information out if the previous value of Pin11 variable was not On i.e. only changes are sent out

pindocThis code will not switch pin11 on the second time as the variable has not changed from its previous value.

One workaround is to use the green join block and broadcast any variables


This method will ensure than any variable changes are always sent out

You may also like...

25 Responses

  1. Mighty_Pooh says:

    Hi there Cymplecy.
    I hope you can help me reverse your script.
    Im using your script with Scratch and it works great.
    What im doing right now is sensing on pin 7 and making scratch move when i do that.
    What i would like to do is sense in Python. but nothing seems to happen.
    This is the script im currently trying from a basic guide.

    import subprocess
    import RPi.GPIO as GPIO
    import time
    input = GPIO.input(7)

    while True:
    if (GPIO.input(7)):
    print(“Button Pressed”)
    Shouldnt that script basicly do the same to my Button as
    have scratch sense if a press is on pin 7?

    • cymplecy says:

      Hi – I hope you understand that I’ve got a lot of work just doing ScratchGPIO so I haven’t the time to help out with general RPi.GPIO stuff. I’d post on the main Raspberry Pi forum Python section where the real experts hang out


      • Brian says:

        Okay thanks for the answer 🙂 Great job with the ScratchGPIO project by the way 🙂

        I did find a solution in the meanwhile though.
        For anyone interested Pin 7 on the Pi rev 2 will have GPIO number 4.
        So what my script should look like when when scratch pin7 works is this.

        import subprocess
        import RPi.GPIO as GPIO
        import time
        input = GPIO.input(4)

        while True:
        if (GPIO.input(4)):
        print(“Button Pressed”)

  2. Chris Wilde says:

    Hi Simon,
    I really love this version of Scratch and the children I am using it with are thoroughly enjoying the experience so thanks!!!! Am I correct in thinking that the values for pin sensor values can only be 0 or 1? I am asking because I’d like to play around with different component inputs. At the moment I’m playing around with a potentiometer… I’m thinking I’d like to set a motor speed to pot input. I’ve attempted to set it up as a sonar too, but only get the values of 1 and 299. Am I doing something wrong or is this functionality that is not currently worked in?
    Many Thanks,

    • cymplecy says:

      The RPi pins are digital so only supply 1 or 0 states. ScratchGPIO has (well hidden) support for PCF8591P based I2C add-on boards so if you had one of them (or wired up your own circuit) then it can be used to read in 4 separate analogue inputs.

      The sonar works (I use it myself a lot) so have you wired one up as per it should work and give values from 1 to 299 (I set limit at 299 as I think its wishful thinking to think that these cheap and cheerful devices will give meaningful results to 5m)


  3. buddhafisk says:

    Hi Simon,
    My son and I are loving Scratch GPIO, but there’s one thing I can’t get working…
    We need more output pins, and I can’t seem to force pins like 7 and 22 to work as outputs within Scratch, though I can make this work from the command line.
    Any suggestions?

  4. buddhafisk says:

    Got it. I found v4, installed it and all is well. I don’t recall exactly where I got the original, but it must have been an old page.
    The syntax from the forum was for a command line tool (WiringPi, I think). It was the first tool I found to control the GPIO pins, but Scratch GPIO is so much better for kids.
    Great work!

  5. mfp says:


    Thanks for this Blog. I am finding it very useful in getting to understand the Raspberry Pi GPIO pins.

    We have done various projects with lights as outputs and wanted to move to using a switch as an input but I am a little confused. I understand that you use Broadcast config11in to set pin 11 as an input, but what do you need to enter so that you can sense if the switch is on or off?

    • cymplecy says:

      You need to go into sensing blocks 2nd last one, click on little black arrow and you’ll see the pins. Click on the tick box and it’s value will sort on the screen. You then normally put it inside a green = block and check it’s value that way.
      Come back if this didn’t make sense

      • mfp says:

        Thanks for your help.
        That has helped and we have got the Raspberry Pi to change costume using wires in a Jelly Baby as a switch, strange I know but a little bit interesting for Children. Unfortunately it is not working when we try to connect a tactile button switch on a breadboard. I am guessing the problem is with the wires on the breadboard but I have virtually no electronics experience. Do you have any advice on how a switch should be physically connected as lots of the diagrams have three wires and I am not sure what is needed.

      • mfp says:

        Hi, hope all is well and thanks for your support to date. We are making progress however it is still not working as expected.

        We have managed to get the switch working in a circuit which includes the switch and a light so that we can check the circuit. The circuit is connected to Pins 6 and 10 on the RPi.

        Scratch code is:
        When flag clicked
        Broadcast configpin10in
        If pin10 sensor value = 1
        Say pin10 sensor value for 2 secs
        Switch to costume constume1
        Say Hello for 2 secs
        Wait for 1 second
        Say pin10 sensor value for 2 secs
        Switch to costume constume2
        Say Super Cat Engaged for 2 secs
        Wait for 1 second

        The light in the button circuit is switching on when the button is pressed but the costume on the screen is not changing. When if statement is changed to the to If space button is pressed the cat changes costumes.

        Are you able to confirm if I am connecting to the correct pins?
        Should I be adding anything different into the code?

        Thanks your help and support is very much appreciated.

        • cymplecy says:

          Hi sorry for delay
          In ScratchGPIO the input pins are normally held at 0V so to make them change from 0V to 3.3V you need to wire like this
          Pin1 (which is 3.3V)
          Resistor (between 330 Ohm and 2K2 Ohm should be fine)
          Pin 10

          So in this circuit – pin10 is normally at 0V and by pressing the switch – you will make it go up to nearly 3.3V.

          The resistor is needed to limit the amount of current going into Pin10
          If you click on the little checkbox next to pin10 sensor value in the sensing block – it will appear on the main stage and you can see if it changes value


        • cymplecy says:

          Also in ScratchGPIO4 pin10 is automatically set as an input so no need for the broadcast config10in

  6. mfp says:

    Hi, thanks for your reply. Not sure what is happening but does not seem to want to do it. I will try again at some point tomorrow, otherwise I can use a keyboard button as an input.

    We do appreciate your site and your help. We would not have got this far without your support. We can successfully control lights by the Pi which is great and there are lots of combinations we can continue to experiment with. Most of the GPIO documentation is in Python and its a lot to learn a new language and electronics at the same time. So it is great to have an introduction in Scratch and then to be able to move onto Python when the child is a little older.


  7. Sagar Juneja says:

    Hi There,

    I am using ScratchGPIO7 (Scratch1.4) on my Raspberry Pi Model 2 to create some games where i plan to use input GPIO pins to sense input value from the sensors.
    Now the problem i am facing is that in sensing block the “sensor value” function doesnt show input GPIO pins. It only give options of giving sensing input from – Slider & Resistance A, B, C, D.
    The bottom line is that i am unable to use GPIOs inputs pins for giving the input to my game. Please help.


  1. 27th December 2013

    […] Further documentation […]

  2. 31st October 2015

    […] For a full list of available broadcasts Simon has helpfully included some documentation on his webpage here for […]

Leave a Reply to buddhafisk Cancel reply

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