ScratchGPIO – Introduction for Beginners

This post refers to an older version of ScratchGPIO
Please use the new Version 5

This post is being left here as some books refer to the older version

Scratch Controlling the GPIO Pins on a Raspberry Pi
Part 1 of 4
(Version 4 – 20 Feb 2014)

This post is intended to make it as Simple as Pi to get up and running and make your Raspberry Pi control some lights and small motors and to respond to switches and sensors. Minimum Requirements – a Raspberry Pi with Raspbian installed (a working internet connection is very handy but not required)  a breadboard, some Light Emitting Diodes (LEDs), some resistors and some wire connectors. Total cost £5-£10. Blue italics are extra information for those who like to understand things a bit more – Pink italics are for the more advanced users and  can be completely ignored by normal users.

How to get a Raspberry Pi to control the GPIO Pins using Scratch —————————————————————
Your Raspberry Pi needs to be connected to the internet to install the software but not needed to run ScratchGPIO. Copy the text below ( left click just before the s of sudo and drag right until all the text in the line as been selected) then right-click and select copy. Open up an LX Terminal window and select Edit and the Paste that into an LX Terminal window and run it to download the installer.

[code]sudo wget -O[/code]

Once the installer has been downloaded then just type (or copy and paste the text below as before)

[code]sudo bash[/code]

(If logged in as a different user to standard pi, then  type sudo bash yourusername) This will install all the necessary extra software and some simple examples. (If you do not have internet on your Pi then, put your SD card into a card reader and try using your browser to right-click and save the script direct to your SD card and then put it back into you Pi and run the second instruction) Connecting Components Up ———————————————————————

EXTREME care should be taken when connecting hardware to the GPIO pins. You can cause your Pi to die by connecting the wrong things together – only do this if your confident of your ability to follow instructions correctly 🙂

AT A MINIMUM – get a breadboard and use some female-male 0.1 leads (available from lots of on-line suppliers or your local Maplin shop) Check out GPIO pin guides to make sure you know what pins are what. Wire up Pin 1 (3.3V) to (at least) a 330ohm resistor – connect that resistor to the long lead of an LED and then connect other end of LED to Pin 6 (0V). It should light up. If it doesn’t try reversing your LED. Now move the lead from Pin 1 to Pin 11. Using ScratchGPIO Run the special Scratch icon (Scratch GPIO) on your desktop. (It is actually a completely normal version of Scratch, it just runs a little Python background program as well that handles communications between Scratch and the GPIO and automatically enables Scratch’s Remote Sensor Connections(RSC)) To test out control from Scratch, click on File then Open and then click on the My Projects button and select blink11 and click on OK. Once the project opens, just click on the OK to enable Remote Sensor Connections. To run the script just click on the Green Flag.

blink11Your LED should now blink on for 1 second and off for 2 seconds – see trouble shooting if this doesn’t happen.

What more can I do with Scratch and the GPIO
As it comes, you can control six pins as outputs (Pins 11,12,13,15,16 and 18) and treat all the rest as simple inputs (22,7,3,5,24,26,19,21,23,8 and 10) (GPIO pin numbers/ordering do not follow anything that makes sense to most people so you just have to go with the seemingly random numbering arrangement)

As you can see in the blink11 script , you can simply use a broadcast message telling Pins to go on or off (Up to 3.3V and down to 0V) The valid messages are pinson along with the corresponding pin off messages. pinsoff

You can also say allonoff And you can replace the word  on with high and replace off with low if you want to talk in pure logic levels.

You can combine message together to make a single broadcast so to turn Pin11 and Pin13 on and all others off you can sayjoined oroff11on13on

Alternatively you can use the pinpattern broadcast to achieve the same result e.g:bpinpattern2This will also set just pins 11 and 13 on.

Inputs To check an input, you should go into the Sensing block and click on the word “slider” at the bottom and you’ll notice that you have pins 22,7,3,5,24,26,19,21,23,8 and 10. inputs If you connect a switch to one of these pins (through a resistor don’t forget) to OV, then you can detect when the switch is open or closed. The inputs will normally read 1 and go to 0 when they are connected (through a resistor) to ground. Click on the checkbox next to pin7 and try it out.

Using variables instead of broadcasts
For more advanced Scratchers, you can use variables instead (or as well as broadcast messages) .

For example: create a global variable called pin11 To make pin11 go on or off  use vonoff On can be replaced with high or 1 and off can be replaced with low or 0 so that you can use whatever logic scheme you’d like.

vallonoffTo set all outputs to on or off use

To use a “bit-pattern” to set/unset multiple outputs simultaneously usevpp (this will set Pin 11 , Pin 13,  Pin 16  and Pin 18 on and Pins 12 and 15  off)

vbug3Note – currently there is an unfortunate “bug” in Scratch in that it remembers variable states and only sends changes out.    Even when you press the Green Flag, it will not send the state of all the variables out, it will only send them when a variable changes. I recommend (nay insist even!) setting any gpio variables to an invalid value – say a full-stop and then to their initial state in a Green Flag start-up script.

Need more Pins as outputs?
If you need more than 6 pins to be outputs  then, for example, you can use broadcast pin7on and pin7 will change from an input to an output.

If for some reason,  you need more input pins you can use broadcast config11in to change pin 11 from an output to an input

Further documentation

Part 2 – using Motors and varying brightness of LEDs


To test if the software necessary to control the GPIO is correctly installed open a LXTerminal session and type

[code]sudo python[/code]

If this doesn’t give an error but doesn’t make a LED on Pin 11 blink then we have real problems Houston 🙁 Try connecting the lead going to Pin 11 back to Pin 1 to make sure the LED lights up then just in case you have a loose connection.

You may also like...

242 Responses

  1. Thanks Simon, you have been working hard – I’ll test this on a ‘clean’ card.

    • cymplecy says:

      The download link wouldn’t work properly using github as source so I’ve gone back to using dropbox so you may have to try again if you were quick off the block

  2. Ken says:

    Nice documentation. Can’t wait to try the new version.

  3. To use a “bit-pattern” to set/unset multiple outputs simultaneously use
    set pinpattern is this confused documentation or me being thick?
    Anyway it does not work on my new install.

    Everything else seems fine, no response from pinpattern.

    Also it appears not to initialize – sometimes, there has to be a ‘sequence’ (not worked out what yet, before the commands spring to life )

    • cymplecy says:

      Aah – yes – 🙂 I forgot I removed it and I haven’t put it back yet! I’ll get onto it 🙂 But what do you mean about it not initilising – if you mean using variables – note the Scratch “bug” in that it won’t re-send a variable value if it hasn’t changed – even on a Green Flag event 🙁 Thats why you have to use dummy values, wait a bit and then set initial values in Green Flag startup code..

    • cymplecy says:

      PinPattern restored – should work both as variable and broadcast 🙂

  4. Sorry Simon,
    its me again: cp: cannot stat `’: No such file or directory
    Running the new install fails to copy the above file, no sign of it!
    I’ve tried a number of times – same result – what am I doing wrong?

  5. Simon,
    trapped the error messages:
    tar: Skipping to next header

    gzip: stdin: invalid compressed data–crc error

    gzip: stdin: invalid compressed data–length error
    tar: Child returned status 1
    tar: Error is not recoverable: exiting now

  6. Herve Hoareau says:

    Thanks for this wonderful work. I started to use it to automate an old train circuit for my grand sons. Unfortunately I need more output pins (for traffic lights and points) and less input pins; is it possible to change usage of specific pins ?

    • cymplecy says:

      Yes – i Haven’t documented it yet as I still might change how its done but at the moment if you broadcast config22out then that will change Pin22 to being an output – it should work for all pins 🙂

      • Herve Hoareau says:

        I am not familiar with the broadcast method as I am using variables which work perfectly but I will try it.
        I suppose it could also be done by modification of PIN_USE in
        The first elements of my application using output pins are working. No bug in that part. I will check input pins next week.
        Thanks for help.

    • Bill Levien says:

      Herve – I would love to see more details on your train setup as I have though about doing the same!

  7. Hello,

    How does one actually get the sensor to work within a loop? i.e. I want to play a sound if the button at pin 22 is pressed and unable to do so. The slider does not seem to connect to anything like an if block. Please advise


    • cymplecy says:

      So what you do is put a green compare block inside the if whitepsace and then add the pin22 sensor block into one side of the green compare block (if that makes sense?)
      So in your case you end up with something like
      if Pin22 sensor value = 0
      would then execute the instructions inside the block when pin22 is connected (thru a resistor of course) to ground)


    • I need 4 input pins connected to buttons for my project – I see that pins 22 and 7 work perfectly but pins 3, and 5 always seem to be in an “on” state. Any reason why those two behave differently? Is there a simple way to turn one of the output pins (say 16 or 18) into input pins? Thanks very much for your program!

      • cymplecy says:

        Yes – Pins 3 and 5 have pull-up resistors conencted to them on board the RPi so they will naturally read high unless connected to ground.

        Only 11,12,13,15,16 and 18 are defaulted to ouputs – all the rest are inputs so you can choose your other two from 24,26,19,21,23,8 and 10.

        You can use Broadcast config16in if you want – I didn’t document it as I didn’t think anyone would need more than 9 inputs 🙂

        PS can you post a link to a large sound file that is causing you problems so I can test please 🙂

  8. prawntot says:

    It’s not working for me in Scratch, although if I follow the troubleshooting instructions the LED does blink without error. What should I check next to get scratch working? Thanks.

    • cymplecy says:

      Well lets try a few things – did you do both stages of the install process? Did the second stage seem to give a sucessful outcome e.g something very similarr to this
      sudo /boot/
      Self Extracting Installer

      Running Installer

      Install Details:
      Home Directory: /home/pi
      User: pi
      Group: pi



    • Andrew Tynan says:

      I had exactly the same problem – turned out that I had an old version of python-rpi.gpio installed (0.2.0-py2.7). I corrected the problem with the following :

      {to try remove the installed version of python-rpi.gpio using apt-get}
      >> sudo apt-get remove python-rpi.gpio

      {… to search the file system for other installed versions}
      >> find /usr | grep python | grep -i gpio

      {the response I got was:
      {Note that I had a mix of python-rpi.gpio versions – uptodate for python3, OLD for python2.7!}

      {… to manually remove the old python2.7 rpi.gpio directories – check you have the correct name for your directories}
      >> sudo rm -r /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.2.0-py2.7.egg/

      {You can check they have been removed by issuing the “find /usr | grep python | grep -i gpio” command again}

      {… to install the new software version – using apt-get}
      >> sudo apt-get install python-rpi.gpio

      {If I issue the “find /usr | grep python | grep -i gpio” command one final time I get the following response:
      …. I hope this helps…



  9. Herve Hoareau says:

    Hello Simon,
    Do you intend to have a version of Scratch GPIO using the new version V2.0 of Scratch which has been released a few days ago ?
    Thanks in advance.

  10. Claire says:

    Hello Simon.
    I am very new at this, and I cannot get past the first step.
    When I try to download the installer, I get this message:
    “The certificate of is not trusted. The certificate has not yet been activated.”
    Do you know what I need to do to get it working?
    Thanks so much.

  11. Claire says:

    Never mind. I’m all set.
    I was able to copy the file to the desktop and run it from there.

    • cymplecy says:

      Phew 🙂 Never seen that sort of error msg before – maybe dropbox was being strange at the time 🙂
      Glad you’ve got it going – any questions – just pop back 🙂


  12. Woosaj says:

    Can you tell me if there is any chance to simulate i2c with gpio in scratch and maybe you already tried this?

  13. mrteach says:

    Found your site and am very excited to try this with some kids next year. I played with Lego Mindstorms a bit, but too costly for now. Raspberry should come in a few days. Hopefully I can get it all working well. I don’t have all the parts you do (I’m VERY new to linux and all this stuff).
    Only thing I’ve done is program in VB a bit (simple) and play around with scratch. So no idea about most of this stuff.

    I was wondering If you post a parts list of your robot I would love to create one for my class to program and play around with.

    • cymplecy says:

      I want to, haven’t managed to source largish wheels yet, I’ve borrowed some out of old Lego mindstorms

      • MrTeach says:

        An electrical components list would be a great start =). I’m wondering what to buy (buying alot from ebay and takes weeks to come). I ordered a lot already.

        I was wondering if DC motors are as easy to control as well? Will they still use Motor A (128) or will it be different. I am thinking of buying a kit from sparkfly just to make it simple for now (contains wheels and some gear motors) might not be that fast, but I can improve it as I play around I hope. They are DC motors though. I assume I will need some kind of chip to control them. Do you know which ones?

  14. MrTeach says:

    Nevermind DC motors can probabl be run by ULN2003

  15. Richard Bignell says:

    I gave up buying individual bits and pieces for motor drivers, instead used Ebay!

    Dual H Bridge DC Stepper Motor Drive Controller Board Module Arduino L298N #236
    Item Number 140743762410 £3.41

    DC 5V Stepper Step Motor+Driver Test Module Board ULN2003 5 Line 4 Phase Arduino
    Item Number 310557450907 £1.72
    Magician Robot Chassis from Cool Components £11.14

  16. eightdog says:

    HI Simon,

    I am working on an interesting project which is right up your street and very much in line with your great work on RPi GPIO handling through Scratch. I would really like to tell you more about it, would you mind giving me your email address or a way to privately message you in this forum so I can introduce myself ?

    Kind regards,


  17. dargue says:

    Hi. This is fantastic – thanks for sharing. I just got the led blink working in Scratch with my Gertboard. For anyone else trying this, the pin-out on the Gertboard doesn’t match the setup in Scratch. I changed the GPIO setmode in the python script to BCM and that seems to have worked.

    • mrteach says:

      is it hard to use i2c with this script? I thought the script was limited to only the gpio pins available (without editing the script of course)

      • mrteach says:

        after looking into the script a bit more. Is this correct? You can do the I2C and extensions and they will just have new ‘pin’ names. e.g. motor88 would do gpio88 (technically). But what are the new pin names if you do add more.

      • mrteach says:

        I just thought of an idea too. If you do a mesh. You could technically just have 1 pi connected to various other things and allow students to just send broadcasts to their (pins) to play around with the idea of interfacing with the real world. of course you would need addition power.

      • cymplecy says:

        I’ve not implemented any I2C handling (as I’ve no I2C devices or boards 🙂 ) It’ll get done one day but not a high priority for me 🙂

      • Tony Chang says:

        We support i2c in scratch, we do a lot i2c devices support. see our example

    • cymplecy says:

      The current version doesn’t support using BCM GPIO numbering but I’ll try and add it back in again for the experts out there 🙂

  18. Seth says:

    This doesen’t seem to be working for me. I kow this seems strange, but using the blink11 example the LED doesn’t blink. Why? Everything is wired properly, the LED /is/ actually connected to pin 11, but why won’t it work?

    • Seth says:

      Forget that, I was using the numbers normally used in Python. (Where the actual pin 11 is 17) I guess I’m just too dunderheaded to look at the diagram.

      • cymplecy says:

        Its not a case of being dum, its just the problem that we’ve all got with the numbering systems – a lot of engineers want to use GPIO numbers but the rest of us prefer physical pin numbers as we can simply count along and get the right one 🙂

  19. Stefan says:

    I wish to understand how is put together and how you made Scratch be able to connect to the GPIO pins. What code is there in the background to make it all work ?

    • cymplecy says:

      Basically there is one Python program that gets called to run before Scratch is launched. The Python programme listens to Scratch broadcasts and variable updates using the Remote Sensor Connection feature of Scratch. So the Python progrram is the one really controlling the GPIO pins

  20. omar says:

    Hi, the installation instructions dont work, when I type sudo /boot/ on the LXTerminal I get not found or syntax error = ‘(‘ unexpected.
    I tried saving it to another folder and running it from there and I get ‘sudo /boot/ command not found’
    What am I doing wrong? can you advice

    • cymplecy says:

      Hi omar – your the 1st to say the intructions don’t work! Are you doing the 1st bit which downloads the to your /boot folder – is it coming up with some sort of error?


  21. Nick says:

    I have the same problem as prawntot. Running from command line does just what I’d expect however using Scratch project blink11 there is no joy. I have installed Debian Wheezy 2013-05-29 then ran apt-get update, upgrade then followed the instructions from cymplecy on this page. Nothing else was done with this distro. Was able to confirm that self extracting installer did give a successful outcome in regards to terminal output ending in “finished.” About scratch indicates that it is v1.4. Any thoughts on how I could trouble shoot this further?
    thanks, Nick

    • Nick says:

      More info. I have rpi.gpio-0.3 installed and when running apt-get install python-rpi.gpio, I am informed that this is the latest version. Thanks for any pointers, Nick

      • Nick says:

        Problem solved, I had inadvertently used 2013-05-29-wheezy-armel for a distro. Duh! Thanks for putting this together Simon. I will be hosting a workshop with some high school next week with pis and your scratch gpio in the US.

  22. javi503 says:

    desperately looking for help!

    I already downloaded the I can see it when I input the command “ls” however, I cannot install it with the following command:

    sudo /boot/

    The Pi says command not found

    What am I doing wrong?

  23. Dominic says:

    Is there any way of controlling servos or motors through this without using additional hardware?

  24. andybatey says:

    The text just above the image led-blink-test.jpg says to move the lead from Pin 6 to Pin 11, that should be Pin 1 to Pin11

  25. Grant says:

    HI there, is there a way of getting Scratch to send out i2c commands? I’ve got a WS2801 LED breakout board.. would like to broadcast messages out to it… can it be done?

    • cymplecy says:

      Yep, the new version supports the PiGlow which is an I2C board so I’ve got the basic code base in place. Email me to discuss how we can add in your device simplecy at

  26. Todd1988 says:

    I have had my Raspberry Pi a week and really appreciate your tutorials and work. I got the blink project to work not problem. But now I wanted to turn the light on and off with a button. However, I cannot get Scratch to respond to a GPIO button press. It works in python so I think I have it wired correctly.

    Here is the Scratch Project

    When [flag] clicked
    If pin24 sensor value = 1
    Broadcast pin11low
    Broadcast pin11high

    In the presentation window I have the sensor value visible.
    The initial value of pin24 is 1 so no light shown. I see no change in value when the button is pressed.

    Any help would be appreciated.

    Thanks, Todd

    • cymplecy says:

      Hi Todd, I’m away from computer today so I’ll look at this when I get home to see if I’ve mucked up my code during recent changes. Can you post your python code that works for me please, regards Simon

      • Todd1988 says:

        Thanks for the help.
        Below is the website where I got the code and the code.

        import time
        #initialise a previous input variable to 0 (assume button not pressed last)

        import RPi.GPIO as GPIO

        input = GPIO.input(24)

        prev_input = 0
        while True:
        #take a reading
        input = GPIO.input(24)
        #if the last reading was low and this one high, print
        if ((not prev_input) and input):
        print(“Button pressed”)
        #update previous input
        prev_input = input
        #slight pause to debounce

        • cymplecy says:

          Hi – your Python code is using the native BCM numbering scheme whereas I use physical pins numbers for Scratch – BCM24 is equivalent to physical pin 18 – try using 18 in your Scratch code 🙂


  27. Todd1988 says:

    That worked. THANKS for your help!! I moved my button to pin 7 and it worked. Spent a lot of time on something simple. But, I will not make that mistake again. Todd

  28. Matthew Huck says:

    Hi, Awesome work, looking forward to letting some students loose with it next Thursday for a school club, but I’m having one minor problem, broadcast works a treat to change pins, but the variables aren’t (well, they work on 1 of the Pi’s, but not the other). I’ve played around in the code and discovered that if I print out dataraw on the line with “Received from scratch-Length:” I get Data: sensor-update “pin16” 0 “pin22″ 0
    However, if I print dataraw here

    if len(dataraw) > 0:
    dataraw = ‘ ‘.join([item.replace(‘ ‘,”) for item in shlex.split(dataraw)])
    #print dataraw

    I end up with the
    sensor-update pin16 0 pin22 0

    I’ve changed the code that searches for the string later on when performing the sensor-update, and with the changes (removing the ” )it works….

    It appears the shlex.split also removes the “, and hence breaking the searches later on!

    A simple test program I tried on the console showed this too
    import shlex
    print shlex.split(“Hello “World””)

    Hopefully that will help.

    • cymplecy says:

      Ta – I added the dataraw = ‘ ‘.join([item.replace(‘ ‘,”) for item in shlex.split(dataraw)]) to sanitise variation in variable names/values (e.g putting a space between pin and 11 or mistakingly adding a space in a variable value! )

      I’ve realsied that I only used new structure for the new MotorPITx and PiGlow code and didn’t go back and make sure mu core stuff still worked OK 🙂

      I’ll get onto it right away 🙂


    • cymplecy says:

      Bug fix for ScratchGPIO – updated to V2.821 – using variable to control pins should be back to working again – please let me know if anything else doesn’t work :0


  29. Lee says:

    I’m sorry but I’m stuck on the first step. I’m getting ERROR 403: FORBIDDEN when trying to download it.
    Anybody have any advise, many thanks.

    • cymplecy says:

      mm – I’ve just tried the download myself and it worked OK – maybe you just tried while I was in the middle of uploading the 2.821 version earlier – I suggest trying again and seeing if you get better luck


  30. Alan taylor says:

    My name is Mikado7 and I am very new to the Pi and to Linux so starting with scratch seems the right way for me. (I am a very senior citizen with experience only of Sinclair and BBC micro computers and PCs)
    I am trying to get my 8 & 12 year old grandsons into computing, as I did with my own children through the BBC micro.

    Firstly I would like to say a big thank you for your program for accessing the GPIO from Scratch. You have obviously put in a great deal of work and yet your patience with questions that have already been answered in the discussions, is amazing. I have learnt a great deal already from your “Introduction for beginners” and the discussions following them.
    I would like to say that the download and operation of the blinking LEDs worked first time for me and I am ready to try motors when I get the ULN2003 chip.
    I am interested to know what part 4 is about as I can’t seem to find a link to it.
    I have clicked on “Follow” which I hope will keep me up to date on future projects, but are there any in the past that would be helpful? It is a great pity that you are not a teacher in our village primary school!
    Thanks again for all your work and help for me.


  31. Nice work Simon, thanks. Your guide was very easy to follow. When downloaded the Scratch GPIO I found I did not have a suitable cable to connect the LED to gpio. Well, a old data cable from a HD drive came in my help. Then I  can to see my LED blinking. Thanks again, and sorry for my limited english skills.

    • cymplecy says:

      The Piface is a great device and in fact this code was developed from their original software – ScratchGPIO is for those of us on a very tight budget 🙂 I believe Tom Preston the original authour os now incorporating some of my additions into the PiFace code so the great circle of open-source life will be going around once more 🙂


  32. Pato Acevedo says:

    Hi again. I want to show my first project /w script..
    Littje characters work better.

  33. Hi Simon I keep getting an error on install
    sudo: /boot/ command not found
    if I run as pi user or my own user.

    • cymplecy says:

      Well thats normally an indication that the download has failed. Can you run the 1st bit again and look at the gobbly gook and see if its worked – copy ans paste it here if possible

      PS I foudn out today that if i double-clicked on the line of code it selects it all which I’ve found is easier than trying to select the code by click and dragging.

      (I wish I could come up with a easier way of getting it installed though!)


      • This is a copy of the download : –

        julian@GridCamera:/boot$ sudo wget -O /boot/
        –2013-08-28 20:46:07–
        Resolving (…
        Connecting to (||:443… connected.
        HTTP request sent, awaiting response… 302 FOUND
        Location: [following]
        –2013-08-28 20:46:16–
        Resolving (…
        Connecting to (||:443… connected.
        HTTP request sent, awaiting response… 200 OK
        Length: 139488 (136K) [text/x-sh]
        Saving to: `/boot/’

        100%[===============================================================================================================================================>] 139,488 133K/s in 1.0s

        2013-08-28 20:46:43 (133 KB/s) – `/boot/’ saved [139488/139488]

        and this a nano of the file :-

        echo “”
        echo “Self Extracting Installer”
        echo “”

        export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`

        ARCHIVE=`awk ‘/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }’ $0`

        tail -n+$ARCHIVE $0 | tar xzv -C $TMPDIR

        cd $TMPDIR
        ./ $1

        cd $CDIR
        rm -rf $TMPDIR

        exit 0

        K^]�yq,�7�Y�2� ��^Ã
        L0^P� ^H�8*A^T+��^LbpU�����Jg���F�|Ô0k`�f|�^F^C^GI���k4Zx^A���^@Ѭ@.^^OĬ�^QEoЬpH@�9̬^L)Z-�h^AI�Gd^H^DO^OȬz-"��ͬ!d��3�2ï^D�U^�"]�^A ^C�Z�g�8�*s^L��^D-^J�A����8(ܬ ,7Ǭ^@��ӠӠ@�Ơߠ^_Ï ^X�,7��&c �^Q�,^ES^A^^Ç A&^D�Ҡ͠4^�^DӼɼe�.�Ӽ&ͼ^S�Y�d�^^X�QXƼ�^]^@Z^V^Q�D^DI2�^X^Q)�^�X^P�^@^Sb�2,^RK:FD`z�^R^X+�22c�^T5^H^RB��]�P^K5″^DJ�ÄJ��+C”�J�q×6��^Y&��^H^G��×BB$|ÒR]^H�Z”^D�hx�0ï���1Q�Z^_@^P�^[ n^W�$^D^X�-D^D��=�^���)R%^Vs�I�H^NL:L^]�hM�Y��^W�(^V!�u���&@��^U���A�q^X-GMÄ�m#�F���^��8�ï¿C�P�{�’1�^��:�|���^[^T�b}^N�^Hj���6��^ZEÞ�j���bÐÎ]�d��eÆ��!�^Mq��5�^Ã
        �HqO�”��I�^Z#���ȵ^W^S^^RƵ^A^YAr�i��x%����$��B µ^Nj�Ð�+�o^G�^V �qJD-�X ^B
        a��G^N^L�Ø&^Q*)^Yt�Q(i�Tn�^��Ê]�^ÃK��$�N^Gh�i^R�ÎGh��^TB�A^S&I^Dz��^P��ß���XTÃ�7t^BÖ�_Ü(}^AH^G-Ã�� Ú<�Ia�vŽ�I�ʽÕo^R�^N�]��U�^���5��^B�A�^X}� ^B�U��� *��ʾ

        Really want to play with my son and and I are converting a Big Trak to run on pi and I would like to use scratch for diagnostics and to program it .

        our rocket launcher lol we done the weapons first .

  34. done and the starnge thing is the file is there. I will try a fresh install tomorrow just in case becasue my webiopi has stopped working too . Will post back if it works or not. Thanks for the fast responce would not get this level of support from microsoft lol.

  35. Saran says:


    A bit of a problem on Occidentalis v0.2. Running “sudo python” works just fine, but neither one method from ScratchGPIO2 (broadcast, set, pin…) turns on the LED.
    Ideas? 🙂

  36. Saran says:

    Still the “sudo python” exits with “‘module’ object has no attribute ‘setwarnings’”.

  37. Saran says:

    I tried running this:
    import RPi.GPIO as GPIO
    and got:
    AttributeError: ‘module’ object has no attribute ‘RPI_REVISION’

    Since the RPI_REVISION is not available, it seems (according to info on that my Rev2 Pi is supported only in RPi.GPIO 0.4.0a or newer:
    – Added support for Revision 2 boards
    – Added RPI_REVISION

    I’ll upgrade to newer module and try again 🙂

  38. crabantou says:


    Thanks for the work, but … it does not work.

    First of all, he line “sudo wget …” does not work. I get the answer “no such type of file or directory”.
    I succeeded in downloading the file and then copying it in /boot and installed it.
    I got the “ScratchGPIO2” icon. The program launches, I have the example files, but nothing works. No effect on the gpio pins.
    When I look for the sensors, no pin is visible. I have only 9 choices, with no pin.

    And when I try “sudo python”, I get “command not found”.

    Directory /home/user/simplesi_scratch_handler is present and contains two files, “” and “”
    Python 2.7.3 is installed.

    What is missing ?
    Thank you for help.

    • cymplecy says:

      First of all, he line “sudo wget …” does not work. I get the answer “no such type of file or directory”.
      I succeeded in downloading the file and then copying it in /boot and installed it.

      It is a bit tricky to copy and paste the text but if you can get it all it does work – honest 🙂
      But if you managed it another way great 🙂

  39. crabantou says:

    Sorry update :

    I succeeded in making “” work with LED blinking, but still nothing from blink11 in “scratchGPIO2”.

  40. Saran says:

    Exact same error on exact same line:

    The SOLUTION is already posted (by me) in this thread. Just read a few of my last replies…

    • crabantou says:

      Yes, your very kind, but I allready tried “sudo apt-get install python-rpi.gpio” and got the answer : “you have the latest version”.

      Another idea ?
      (note my board is rev 1)


    • Alan Taylor says:

      Hi, Just a quickie I hope. I have managed to get your “2 motors working in Scratch with PWM” but as Inow want to operate remoteley using my laptop I guess I will have to do it in Debian. Please can you advise me how to do this or point me to an article describing how to do it. One other thing, how many GPIO pins can provide PWM? Many thanks for your help in the past. tubluv

      • cymplecy says:

        I use x11vnc on all my RPi to remotely use them using tightvnc on my PCs/laptops
        I use this to setup my RPi x11vnc

        I am developing a different way of working called SID (Scratch Interface Device) which means you can run Scratch on your main computer and just have the GPIO handler running on your RPi but its not fully developed yet (Too much to do – too little time to do it in)


  41. Steve says:

    Found your pits and I have installed the software and got it talking to my gertboard to control LEDs and a motor. Struggling with the switch inputs . Think I have got the board wired up correctly and using the correct pins in scratch, the gertboard examples state I need to set a pull up resistor in GPIO for it to work. Can I do this via scratch or would it require a modification to the python interface?

    I will investigate further and make sure I have not made a mistake, planning on taking my PI and the gertboard into school Friday for a code club. They have all been getting in with the scratch examples, thought I would show them how to control some hardware.


    • cymplecy says:

      Hi Steve
      I’ve not got a Gertboard myself and I think you are the first person who’s come along here with one 🙂 As default, I set 11,12,13,15,16,18,22 and 7 as outputs and all the other pins as inputs (With GPIO pull-ups enabled). If Gertboard uses any of the default output pins as inputs E.g 22 and 7, then you’d need to broadcast Config22In and broadcast Config7In (just once per session – I put them in a On Green Flag event) to get data from them.
      Outputs aren’t an issue because as soon as default input is used as an output then my prog will change it from input to output.

      I HIGHLY recommend fully testing any code ideas BEFORE facing the young Scratchers 🙂


  42. peter says:

    Hallo, könnt ihr hier einmal schreiben , wie überhaupt die Daten vom Scratch1.4 aufgenommen werden ? Ich versuche verzweifelt eine Netzwerkverbindung mit Sratch 14.4 aufzubauen zum PC , um überhaupt mal ein Wort nach Scratch1,4 zu senden . mit IP und Port 42001.


    Hi, you can write here again, as all the data from the Scratch1. 4 be included? I’m trying desperately to connect to a network with Sratch 14.4 to the PC, to ever send a Word to Scratch1, 4. with IP and port 42001.

    Thank you.

  43. Steve says:

    Hi Simon,
    I think the work you have done to enable control of the Pi’s GPIO though Scratch is fantastic.
    However, I hooked up a quick2wire interface board to my RPi so that it is easier/safer for my children to use. Do you have any plans to implement WiringPi pin numbers for the broadcast/pin patterns/variables as well as/instead of the native GPIO pin numbers?


    • cymplecy says:

      I’d never considered it before. I used to have support for the Broadcom GPIO numbering scheme but I abandoned it when the some of the physical pins changed from Rev 1 to Rev 2 boards but I am planning on adding that support back in as quite a few peripherals mark their outputs using that scheme but I’ve not seen anything that refers to wiring pi numbering.

      What quick2wire board are you using?


      • Steve B says:

        Hi Simon,
        I’m using this board…

        I may have made a mistake by calling them WiringPi numbers, they are in fact the GPIO numbering scheme. I was looking at this diagram when I wrote the last post…

        It’s good to hear that you are thinking of putting the GPIO labelling scheme back in – it will make life so much simpler when controlling the inputs and outputs through this extension board.


      • Steve B says:

        I’m getting myself confused here… It’s not the BCM numbering scheme I’m looking for, it’s a scheme that mimics the names of the pins.

        What I’m looking for is that ability to write something like:

        broadcast gpio0high
        broadcast gpio2high
        broadcast gpio6high

        gpio0 refers to pin11 or BCM GPIO 17
        gpio2 refers to pin13 or BCM GPIO 21 (R1 boards)/BCM GPIO 27 (R2 boards)
        gpio6 refers to pin22 or BCM GPIO 25


      • Steve B says:

        Maybe this page will help…

        • cymplecy says:

          So what is it your after? 🙂
          To be able to refer to RPI GPIO pins using @drogons wiringpi numbering scheme – if so – going to be a long time before that happens – if ever (Just too much work for little gain I’m afraid)

          Refer to RPi GPIO pins using BCM numbering scheme? – will be done before Christmas 🙂

          Create addon support for that quickwire board? = possible if SKPang send me one to play with – otherwise not (got 2 other boards in the queue at the moment for adding support for already)
          Just not enough time to do eveyrthing I’m afraid 🙁


  44. Inti Garcés says:

    Hi, I’ve tried but something was wrong. work properly and made the led to blink, but nothing happens when I try with Scratch2GPIO, I haven’t any sensor pin either. Any hint?

    • cymplecy says:

      Do you have an up to date copy of Raspian?
      if you run
      sudo python ./simplesi_scratch_handler/
      do you get an error msg about GPIO?


  45. Inti Garcés says:

    Hi Simon. running I received this error :
    “There was an error connecting to Scratch!
    I couldn’t find a Mesh session at host:, port: 42001”

    Reading I realized that pi user wasn’t the owner of “Documents/ Scratch Project directory”, My problem was solved making pi the owner of Document folder recursively.

    Thanks for your answer and your great work!

    • cymplecy says:

      What distribution are you using? On Raspbian the default pi user should own all subfolders of Documents


      • Inti Garcés says:

        Yes, I’m using Raspbian. You are right but pi user wasn’t the owner 🙁

        • cymplecy says:

          mm – I’d better download the latest see if they’ve got a bug in it – ta 🙂


          • cymplecy says:

            I’ve found the problem – if you use a clean Raspbian, then the Documents/Scratch Projects folder does not exist and my installer creates them but since installer is running sudo – they are created as being owned by root.

            The Documents/Scratch Projects folder is only created on 1st run of normal Scratch 🙂

            I’ll alter my script to make sure that is I end up creating them – they are owned by pi 🙂



    • Inti Garcés says:

      I commented this communication to my 9 years old son and it was very instructive for him. The error, your soon answer, the solution, the use of English language to communicate(we are from Spain), a lot of fun! 🙂
      We are using a Scratch 2 GPIO in a project of a terrorific and noiser costume for Halloween 🙂
      Thanks again for your work!

  46. Hi, this may be quite basic (my Linux is rusty!) but (using MobaXterm to control my Pi) I’m getting the errors below when I try to run your script:

    pi@raspberrypi ~/simplesi_scratch_handler $ ./
    [1] 1749
    pi@raspberrypi ~/simplesi_scratch_handler $ kill: invalid option — ‘9’
    Executing: /usr/lib/squeak/4.4.7-2357/squeakvm -encoding UTF-8 -vm-display-x11 -xshm -plugins /usr/lib/scratch/plugins/:/usr/lib/squeak/4.4.7-2357/ /usr/share/scratch/Scratch.image /home/pi/Documents/Scratch Projects/
    Xlib: extension “MIT-SHM” missing on display “localhost:10.0”.
    Traceback (most recent call last):
    File “/home/pi/simplesi_scratch_handler/”, line 24, in
    import RPi.GPIO as GPIO
    ImportError: No module named RPi.GPIO

    I get the same error about RPi.GPIO if I try to run the Python script from the CLI:

    pi@raspberrypi ~ $ sudo python ./simplesi_scratch_handler/
    Traceback (most recent call last):
    File “./simplesi_scratch_handler/”, line 24, in
    import RPi.GPIO as GPIO
    ImportError: No module named RPi.GPIO

    Scratch starts apparently OK in a Xterm window, but the blink11 script (nor anything else I try from your tests) is changing the state of any GPIO pins.

    I have also tried this as you describe, using lxsession to start a desktop and then starting Scratch_GPIO from the icon, but that seems to fail in the same way.

    • [Misleading typo] I meant to write: “the blink11 script (nor anything else I try from your tests) is NOT changing the state of any GPIO pins.”

    • One of the many things I’m not familiar with is where to find Python libraries. I did try installing the current RPi.GPIO, from and got:

      pi@raspberrypi ~/RPi.GPIO-0.5.3a $ sudo python install
      running install
      running build
      running build_py
      creating build
      creating build/lib.linux-armv6l-2.7
      creating build/lib.linux-armv6l-2.7/RPi
      copying RPi/ -> build/lib.linux-armv6l-2.7/RPi
      running build_ext
      building ‘RPi.GPIO’ extension
      creating build/temp.linux-armv6l-2.7
      creating build/temp.linux-armv6l-2.7/source
      gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_gpio.c -o build/temp.linux-armv6l-2.7/source/py_gpio.o
      source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory
      compilation terminated.
      error: command ‘gcc’ failed with exit status 1
      pi@raspberrypi ~/RPi.GPIO-0.5.3a $ pwd

      Possibly I’m doing that from the wrong directory or as the wrong user?

  47. Tony says:

    Hi Simon

    Thanks for this, a great way to get kids into programming and GPIO. I had the same problem with an older version of the GPIO that doesn’t support ‘setwarning’, but as soon as I removed the old version, following the uninstall GPIO instructions, then used apt-get install RPi.GPIO, it worked first time.

    Thanks again, Tony

  48. Thanks Simon. I tried what worked for Andrew, but it doesn’t seem as if I have an older vn installed. I get:

    pi@raspberrypi ~ $ pwd
    pi@raspberrypi ~ $ sudo apt-get remove python-rpi.gpio
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package python-rpi.gpio
    E: Couldn’t find any package by regex ‘python-rpi.gpio’
    pi@raspberrypi ~ $ find /usr | grep python | grep -i gpio
    pi@raspberrypi ~ $ sudo apt-get install python-rpi.gpio
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package python-rpi.gpio
    E: Couldn’t find any package by regex ‘python-rpi.gpio’
    pi@raspberrypi ~ $

    I have now tested my GPIO using the wiringPi gpio test CLI program; so my problem’s not hardware but just in driving the GPIO from Python (and therefore from Scratch).

    Am I merely missing Python.h (my C is rusty too!) from the correct location – as suggested by the fail of gcc when I try to install RPi.GPIO?

    • cymplecy says:

      I just don’t know the details of how package management works 🙁 I do know that if you download the last Raspbian distro and make a clean install then it works fine. All the problems people have is with using old versions. I think the package management system isn’t managing to update really old versions. I myself have never had this problem so I’m not much help I’m afraid 🙁


      • Thanks anyway. You’ve spurred me into refreshing my Linux admin skills (after too many years of having a team of techies to do that sort of thing for me ;->). I’ve built a new Raspbian and got the hang of using apt-get properly, and I now have a LED blinking happily on pin11, controlled from Scratch, so I’m ready to play with some stepper motors!

  49. don isenstadt says:

    your instructions and code are great.. I am a complete noob to scratch but I got the samples working.. so then I wanted to make the switch work.. I set up pin 10 and used a 270ohm resistor from pin 10 through my switch to gnd… I then tested it out and when when I use the input sensor block I can see the value go from 1 to 0 when I press the switch .. the problem is setting up the test in scratch.. how is this accomplished?? I would like the “reporter” to fit in a when hat block.. so
    if I press the switch and the value goes from 1 to 0 the program takes off .. but this does not seem to work?? Any examples or other doc would be greatly appreciated..

  50. Neil Higgins says:

    This is fantastic!! I have been wanting to demonstrate to my daughter how real computers control real-world stuff, and this is just the ticket for her to see for herself. Congratulations and thanks so much! One comment, one query. Comment (by no means a criticism): I set up two LEDs flashed by two sprites, one at 1 second period and the other at 2 second period. They do not stay synchronised at all. I guess that’s down to Scratch’s execution model. Query: Am I right in understanding that Scratch 2 will be entirely web-based and therefore incapable of this?

    • cymplecy says:

      Can you email me your script and see if I can improve things simplecy at google mail dot com
      There is a beta version of an off-lineversion of Scratch2 for PCs (and since Flash based – it won’t work on an RPi)
      I have got a method of controlling the ports from a desktop/laptop PC (look for SID in my other posts).
      its possible to get this working with Scratch 2 (not done it yet) so that would be the way to do it


      • Neil Higgins says:

        Err … It’s not clear how to send you the .sb file via the blog. Anyway it’s exceedingly simple – I just have one sprite pushing a pin high and low with one second waits, and another sprite pushing another pin high and low with half-second waits, both in infinite loops. I’m using pins 18 and 22 to drive LEDs on a Gertboard (not that that should matter). There’s a very obvious roll in the phase between the two LEDs. Cheers, Neil

        • cymplecy says:

          Neil – two things – I thought I’d answered this comment a while ago but now can’t find it so sorry for that !

          basically – it said – its down to Scratch not keeping time propery

          2nd’y – I think you’ve discovered an actual Scratch bug (which is good because hopefully we can fix it 🙂

          I was playing with trying to make a clock using the PiRingo (12 leds in a circle) and I found wired things happening (I was trying to keep one led steady to inidcate hour and the minute led was flashing)

          To cut a long story short – I had 2 wait’s executing at same time and things went wrong – when I replaced one of waits with using the timer
          set starttime to time
          wait until (timer – starttime) > 1
          (this replaces a wait 1 block)
          then the problem disappeared

          When I was making adjustments later on – I add in a another wait block and things went wrong again

          Anyway – going to double check using a different script but thought I’d better write this down now 🙂


      • higginsneila says:

        You have been busy since I last looked! A script that won’t reliably fail could imply that session history has come into play during your previous session – environment variables, global variables, etc.. If your session did not start with a clean slate, perhaps something was carried over from the previous script (?) When I get a chance I’ll try the same thing in Scratch (non-GPIO) and see if I observe the same timing irregularity.

  51. Wyn says:

    Hi Simon,

    Thanks for a fabulous resource.

    I have modified the code a little so that I can drive a Maplin robot arm from Scratch
    This is using the python USB bindings – so not GPIO – But I will send you the modified script if you would like.

    Right now it is a bit of an ugly hack, but if you are interested in including it I will tidy it up before sending.


    • cymplecy says:

      Excellent – I’ve got one of those arms but never got round to getting it working with Scratch. I was planning on building an direct interface to the motors and doing it that way but if you’ve done it via USB then that saves a lot of effort and expense 🙂

      Could I have a copy to try out please (I completely not worried about state of code – as you can tell from my own efforts :))


      • Steve says:


        I runa code club and have started taking my own PI in and getting the students controlling LEDs via scratch GPIO. I have noticed the post about the USB modification to control a Maplins robot arm which I have. Is this code available? The club has just received 3 PIs from google so next term I was thinking of using one to control the arm and the other two LEDs etc via a breadboard.

        If th code needs further work, testing etc I have for the time to assist,



  52. Hi,

    Your scratch code sounds very interesting. Would it be possible to make it available for all so that we can use and modify, please?


  53. Jack says:


    My daughter has been using your GPIO2 in her school project and got a number of sensor inputs working! Thanks to your code! She’s excited to learned you now have GPIO4 with output pins & promptly installed it on her raspberry pi. However, when she checks the ‘slider’ sensor value, the menu lists only ‘motoracount’ rather than listing all the pins … not even the input pins are showing & the program with sensors that worked on GPIO2 doesn’t work with GPIO4. Any idea what problems she might be running into? My daughter tells me she saw similar thing happened w/ GPIO2, but very intermittently & is solved by re-booting her raspberry pi … reboot does not help however with GPIO4. Any help to isolate issue is very much appreciated.


    • cymplecy says:

      It a feature so it doesn’t clutter up the sensor list with inputs that don’t do anything but I may have gone 2 far 🙂
      It also waits to see if you have any addon board in use so it doesn’t report anything until intitially.
      A workaround is to simple make Green flag broadcast AllOff and then pins 22 and 7 will appear.

      Obviulsy I’ll have to have a bit of a rethink since its people like your daughter that this is aimed at 🙂

      Can you email me a script that doesn’t work please (or a pic of a screen with it showing should do)

      simplecy at google mail dot com


      • Jack says:

        Sorry for some reason i did not get email notification of your response. Thanks for the quick reply.

        My daughter left her raspberry pi at school over the weekend & she will send you the script directly when she returns to school Monday.


  54. Jim says:


    I’ve successfully installed GPIO4 and successfully tested the output commands.

    Now I’m trying to read inputs. However when I click on slider in the sensor area I do no see any pins listed. Instead I see the following selections:

    How do I get access to the input pins?


    • cymplecy says:

      You’ve got an older version and I’ve just released the latest version today which fixes that bug 🙂 Would you mind just re-downloading and re-installing it and then use the ScratchGPIO4 desktop icon – not the ScratchGPIO4Plus icon. Then you should get the pin sensor values


  55. Ray says:

    Hi unable to download scratch gpio software,
    I am getting ” no such file or directory” message after entering the “sudo wget ……..” Command . When I put the URL into a browser Google gives 404 page not found error

    Any ideas.


  56. Hi, Simon
    I hope you remember me. We discussed at the physical computing forum of Scratch site about my mod for Raspberry Pi GPIO.
    I am writing a book for kids that focuses Raspberry Pi and Scratch with my friends now.
    Of course this book covers your Scratch GPIO! Thank you for your great work.
    I found some important changes in the recent version, 4.1.09.
    One is some input pins, e.g. pin22, are pull down now. These were pull up in version 4.
    Another is usable input pins are always appear in sensor menu. These were appended dynamically in version 4.
    I am little bit confusing about this specification changes. I understand evolution is very important. However I would like to know stable fixed specification.
    Thank you for your help.

    Kazuhiro Abe

  57. Hi

    Where is the “original” documentation for Scratch and GPIO? I have looked but only found a different source.

  58. I mean where it tells you about “Broadcast pin12high”, “Set pin11 =100” etc. It’s where the GPIO library meets Scratch. Is there documentation for that – or is that all your invention?

    • cymplecy says:

      The original code base was from Thomas Preston’s work at Manchester University for controlling the PiFace.
      I don’t remember there being much documentation at that time (18 months ago) and it has pretty much changed out of all recognition since then.

      I “invented” much of the broadcast syntax (well me and a lot of feed back from fellow enthusiasts on twitter) and I decided on variable values going from 0 -100 and using MotorA and MotorB mapping to pins 11 and 12.

      I want it to be as simple as possible for 8-11 year olds (and their teachers) to use and understand.

      I work as a primary school technician and have also been helping/teaching Scratch for about 7 years so I think I know the mindset of the teachers and pupils in primaries 🙂

      If you have any ideas or suggestions I’m very keen to hear them 🙂


      • I’ll let you know. Just started working with them in schools. I’m impressed by the ability to control motors and read sensors. I’ve been using Scratch for Arduino which has similar functions. All very exciting.

        • cymplecy says:

          Which Scratch for Ardunio have you played with

          There’s S4a which is an actual mod of Scratch so the blocks are hard coded into it.
          Also Ardublocks which is a very good for transition tool as it generates actual Arduino standalone code
          And theres my ScA of course (2nd class compared to the above but I’m in the middle of bringing it up to ScratchGPIO level)


    • cymplecy says:

      BTW I have started a simple method of trying to document all he syntax but its not complete yet

  59. A Bryant says:

    Thank you, came across this while trying to understand how to get a PiGlow working!

  60. Michael says:

    Hi Simon
    Congratulations and thank you for your great work on this. I have been working my way through it but I am having mixed success. I have got as far as variable speed motor control using a 2803 chip and an external power supply but I am getting nowhere with driving a stepper motor. I have some of the 28BY motors you mention and I wired one of them up as in your diagram. I set up a simple script to broadcast StepperA and then set MotorA first to 50 for 5 seconds, pause for 1 second and -50 for 5 seconds. The motor runs ok but it goes the same direction each time instead of reversing.
    I am using pins 11,12,13,15 on a rev2 board with 512MB, made in China.
    I hope you can help.
    As an aside, your beginners page from which I am sending this has a problem in that the instructions are clipped on the right hand side. I have tried both Chrome and Firefox so it shouldn’t just be a browser artefact.
    best regards

    • cymplecy says:

      I’ll plug a stepper into mine at the weekend and check to makes sure I’ve not introduced a bug.

      You might want to just try out this

      which uses same wiring sequence so that would double check whether its my software or a hardware issue 🙂


      • Michael says:

        Hi Simon
        Thank you for your patience and help. I used the python demo program to get more familiar with what is going on and I found that I had swapped the connections to pi pins 11 & 12. I now have the motor turning happily from python.

        I will now try scratchgpio again with the correct wiring.


    • cymplecy says:

      Re clipping – its just a WordPress blog – looks fine with me which bit in particular is clipped?


      • Michael says:

        Hi Simon
        Thanks for the instant reply! Now that I look again the problem is that everything from the first instruction on how to install is in a window which doesn’t wrap but which I see can be scrolled. I was afraid I might have missed something but I see now how to access the whole lot.
        I’ll check out the reference you gave for the stepper, I am only too aware of how easy it is to make an error in connections and not be able to see it later.

        • cymplecy says:

          Just a seasy for me to introduce code reversions as well 🙂
          I’m just away till Sunday so won’t be able to look at it until then – just prompt me on Sunday evening 🙂

  61. Steve says:

    Hi Simon,

    I have trouble reading a switch, I thought the internal resistors for the inputs we pulled high, so I connected pin 24 through a 1K resistor and the switch to ground. This is based on response to an earlier question.

    The input never changed state so I connected the other end of the switch to pin 1, 3.3 volts and it now works reliably. This makes me think the internal resistors on the inputs are set to pulled low, is that correct.

    I have been using scratch gpio in school as part of my code club, at the moment we are simulating traffic lights, I want to use the switch input for the “singing jelly baby” project.

    Keep up the good work

    Steve G

    • cymplecy says:

      Cuurent stable release pulls-low. This was done as the default state of high is considered not to be “right” as normally attached boards expect low signals as initial condidtion.

      So I threq the baby out wit hthe bath water! 🙂

      I wil be- re-instating default pull-high on all pins excpet 11,12,13,15,16 and 18 which will be set as outputs in V5 wit h the pull-down settings only enabled when the +Plus version is launched – they should keep it simple for beginners but satisfy the more advanced user.

      I can’t change V4 because someone has written a book and uses the pull-down convention so I’ll need to create a new link etc !

      The life of an open-source developer is not smooth 🙂


    • Michael says:

      Hi Simon
      (You said to remind you on Sunday)
      I can drive a stepper motor from python no problem so the connections are finally ok. I am still having no luck from scratch though. I have looked through the handler program and it seems to be using the same pins for stepperA although I can’t follow most of it.
      I have an led in parallel with the 2803 output from pin11 in the pi and it flickers while I am using python but stays off with scratch.
      I did an apt-get update and a rpi-update for other reasons recently but I reinstalled your scratchgpio4 after that just in case.
      Is there anything else I can try or have you had a chance to make sure it still works with a stepper motor? If you had time to post a scratch program that works showing where steppera gets broadcast and showing the speed and position variables in use it would be great.

  62. Michael says:

    Hi Simon
    Success! The problem was in the original greenflag broadcast. I had “StepperA” which I think is what is in your instructions so I tried changing to “stepper” as in your example and all is working perfectly now. Thank you very much for your help on this, giving your time like this is much appreciated.
    You may be amused to know that the external power supply I use to drive the motors is in fact a mains adaptor/charger from a Commodore calculator I bought around 1974! Its last job was to power an electric motor built from a kit bought in the Deutsches Museum in Munich – said to be the best science museum in the world.
    regards and thanks again

    • cymplecy says:

      Sorry about that!
      I’ve corrected the docs as well 🙂
      If you need to use 2 motors – you’ll have to use the dev version until I merge the fix into the stable version.


  63. Simon,
    Thanks for your excellent work.
    I’ve now realised that I needed to upgrade to GPIO version 4 for the revised / extended instruction set and have just done so.

    I’m using the same SR-04 Ultrasonic sensor as in your Introduction page 4, however I’m using two different ports rather than the one as you have. This works fine with a Python program but I cannot get the Scratch version to work.

    You have ‘Sonarnn’ with nn as 23 the pin number and ‘ultrann’ in your broadcast instructions but the difference between these two instructions is not clear. I can edit the pin selection in the Broadcast instruction but not the Sensor instruction and, of course, I need to have two different pin numbers rather than the same one as you have.

    Any advice greatly appreciated.


    • cymplecy says:

      ScratchGPIO deosn’t handle using one pin for pinging and listening on another pin. I took the desicion to use the 1 pin approach because you normally need to add some resistors anyway so you may as well configure it for 1 pin use and it makes the programming side of things easier and only uses 1 physical pin. But I could be persuaded to bring it back if there was a good reason.
      The difference between Sonar and Ultra is that Sonar cam first and you broadcast Sonar8 and 1 sec later – the sensor value sonar8 will be updated with the distanace found.
      Buts lots of times, the Scratcher just wants to be checking the distance on a continuous basis so I introduced Ultra8 (8 can be replaced by any pin number)- you just need to broadcast Ultra8 once and sensor value Ultra8 will be updated once a second with the distance value without the need to do any more broadcasts

  64. Kieron says:

    Hi Simon. Just wondering if you can answer a puzzle for me. I use RPi.GPIO in python scripts but always need to run IDLE as root on the Pi before the scripts will run. Ive used your scratch handler quite a bit, thank you, to allow my students to write scratch programs that access the GPIO header but ive noticed they’ve never needed root privileges to do this. Ive had a look at your scratch handler and it also uses RPi.GPIO. I was just wondering how that is. Did you find a way to use RPi.GPIO without root access ? Could you help me do the same ? Its a long story but I cant give IDLE root access anymore and its hampering my students progress. Thanks for any help you might be able to give.

    • cymplecy says:

      Unfortunately, I just run my handler by sudo python scratch… I think the best way to try and find a solution would be to ask on the RPi forum as I’m no Linux privileges expert


  65. Dave says:

    HI – Great to have GPIO from Scratch – Thanks for working this out!

    I have a problem installing. After bash i get… (below). Any ideas? line 1: –2014-04-07: command not found line 2: syntax error near unexpected token `(‘ line 2: `Resolving (…,,, …’

  66. Jon Bromhall says:

    Hi – firstly thanks for putting this together – very useful.
    I am after a bit of advice. I am new to RPis and approach it from a non programming background bar some php/sql work. However I want to show how Scratch can do most things kids would want to do (and adults)
    I have successfully connected a DS18B20 thermometer which I can read from either the terminal screen or from a small Python script updating the file. But how do I get this result into Scratch??? I’ve looked and looked and looked!!
    I think Broadcast holds the answer but not sure but how I get a result broadcast from the Python file, I have no idea and also no idea how Scratch would execute that file.
    Maybe there is a much easier way!!
    It’s the first bit of a homemade hot water solar panel system I am building. The rest will be controlling pumps and actuation valves.
    Thanks in advance

  67. Howard says:

    Hi. Nice job packaging this. It really is something that Scratch should have built in. It looks like it is when it’s installed too. Nice!

    I’m trying to get a PCF8591 to work. I’ve wired it on in the manner of and I can properly run the Python code he has on that page just below the schematic. I fiddle with the voltage divider pot and see values from 0 to 255. When I go into Scratch, I see ‘adc1’ through ‘adc4’ as sensor choices, so that tells me that the chip is communicating. ALso good. If I write a little Scratch program that simply reads the value and displays it or uses it to set the rotation of the sprite, I only get one reading and that value just comes in over and over. So if I set the pot midrange, I’ll get 128 as the only reading. I can then exit out and restart with the pot in a different spot and I’ll get a different reading that doesn’t change.

    Is there something obvious I’m missing? Thanks!


    • cymplecy says:

      Sounds like handler giving one reading and then failing. I’m away till tomorrow so can’t check till then. If you just tick the sensof check box next to the ADC your using you can see the display updating normally. I’m assuming it doesn’t with you but can you check anyway please

    • cymplecy says:

      I just powered up my ADC board from @4tronik and its working fine for me sorry. I’ve also got a robot prototype with one of the chips built into it and both devices return updated values several time a second.

      Just in case its something else – try rebooting and then going into LX terminal – cd scratchgpio5 sudo python and lets see if the handler is crashing out with some error


      • Howard Eglowstein says:


        Without knowing what the difference between that board and how an 8591 is simply wired in via I2C, I’m not sure I know how to move forward with this. I tried running the code in a window as you suggested:

        pi@raspberrypi ~ $ cd scratchgpio5/ pi@raspberrypi ~/scratchgpio5 $ sudo python PWM/Servo imported OK ADC/DAC imported OK Minecraft imported OK Board Revision 2 Debug enabled 2 PiGlow init i2cbus: 1 self.bus: 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: — — — — — — — — — — — — — 10: — — — — — — — — — — — — — — — — 20: — — — — — — — — — — — — — — — — 30: — — — — — — — — — — — — — — — — 40: — — — — — — — — — — — — — — — — 50: — — — — — — — — — — — — — — — — 60: — — — — — — — — — — — — — — — — 70: — — — — — — — — No PiGlow Detected No Compass Detected Error accessing device at address 0x40 No PWM/Servo Board PCA9685 detected init PCF8591 128 PCF8591 init completed ADC/DAC PCF8591P Detected No AdaMatrix Detected No PiMatrix Detected pi camera init user pi complete PATH: /home/pi/scratchgpio5 Starting to connect… Trying There was an error connecting to Scratch! I couldn’t find a Mesh session at host:, port: 42001 Trying There was an error connecting to Scratch! I couldn’t find a Mesh session at host:, port: 42001 Trying There was an error connecting to Scratch! I couldn’t find a Mesh session at host:, port: 42001 pi@raspberrypi ~/scratchgpio5 $ sudo python PWM/Servo imported OK ADC/DAC imported OK Minecraft imported OK Board Revision 2 Debug enabled 2 PiGlow init i2cbus: 1 self.bus: 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: — — — — — — — — — — — — — 10: — — — — — — — — — — — — — — — — 20: — — — — — — — — — — — — — — — — 30: — — — — — — — — — — — — — — — — 40: — — — — — — — — — — — — — — — — 50: — — — — — — — — — — — — — — — — 60: — — — — — — — — — — — — — — — — 70: — — — — — — — — No PiGlow Detected No Compass Detected Error accessing device at address 0x40 No PWM/Servo Board PCA9685 detected init PCF8591 128 PCF8591 init completed ADC/DAC PCF8591P Detected No AdaMatrix Detected No PiMatrix Detected pi camera init user pi complete PATH: /home/pi/scratchgpio5 Starting to connect… Trying Connected! Sender Init Running…. DEBUG:root:Final change sent value for pin 3 is 0 DEBUG:root:Final change sent value for pin 5 is 0 DEBUG:root:Final change sent value for pin 7 is 0 DEBUG:root:Final change sent value for pin 8 is 0 DEBUG:root:Final change sent value for pin 10 is 0 DEBUG:root:Final change sent value for pin 11 is 0 DEBUG:root:Final change sent value for pin 12 is 0 DEBUG:root:Final change sent value for pin 13 is 0 DEBUG:root:Final change sent value for pin 15 is 0 DEBUG:root:Final change sent value for pin 16 is 0 DEBUG:root:Final change sent value for pin 18 is 0 DEBUG:root:Final change sent value for pin 19 is 0 DEBUG:root:Final change sent value for pin 21 is 0 DEBUG:root:Final change sent value for pin 22 is 0 DEBUG:root:Final change sent value for pin 23 is 0 DEBUG:root:Final change sent value for pin 24 is 0 DEBUG:root:Final change sent value for pin 26 is 0 DEBUG:root:datalen: 0 DEBUG:root:RAW: cycle_trace has changed to disconnected Scratch disconnected cleanup threads started Sender Stop Set Threads told to stop Waiting for join on main threads to complete Stopping 3 Stopping 5 Stopping 7 Stopping 8 Stopping 10 Stopping 11 Stopping 12 Stopping 13 Stopping 15 Stopping 16 Stopping 18 Stopping 19 Stopping 21 Stopping 22 Stopping 23 Stopping 24 Stopping 26 Stopping Matrix cleanup threads finished Thread cleanup done after disconnect resetting pin mode reset pin 3 reset pin 5 reset pin 7 reset pin 8 reset pin 10 reset pin 11 reset pin 12 reset pin 13 reset pin 15 reset pin 16 reset pin 18 reset pin 19 reset pin 21 reset pin 22 reset pin 23 reset pin 24 reset pin 26 Pin Reset Done Starting to connect… Trying Connected! Sender Init Running…. DEBUG:root:Final change sent value for pin 3 is 0 DEBUG:root:Final change sent value for pin 5 is 0 DEBUG:root:Final change sent value for pin 7 is 0 DEBUG:root:Final change sent value for pin 8 is 0 DEBUG:root:Final change sent value for pin 10 is 0 DEBUG:root:Final change sent value for pin 11 is 0 DEBUG:root:Final change sent value for pin 12 is 0 DEBUG:root:Final change sent value for pin 13 is 0 DEBUG:root:Final change sent value for pin 15 is 0 DEBUG:root:Final change sent value for pin 16 is 0 DEBUG:root:datalen: 62 DEBUG:root:RAW: :sensor-update “adc1” 192 “adc2” 192 “adc3” 192 “adc4” 192 DEBUG:root:dataList: [‘sensor-update “adc1” 192 “adc2” 192 “adc3” 192 “adc4” 192’] DEBUG:root:Final change sent value for pin 18 is 0

        • cymplecy says:

          Its a mystery – all looks like it should work – no errors and you say it works with python code???? I’ve ordered a a PCF8591 chip and I’ll wire it up like that article and see what happens at my end then – we’ll get to the bottom of this!


          • cymplecy says:

            Well, my chip arrived and I wired it up as per that blog and it works fine for me 🙁 I’m at a loss to understand how it works OK with you using that Python code but not with my code 🙁

  1. 22nd April 2013

    […] Cymplecy has released a new version of his Scratch GPIO software for the Raspberry Pi. It allows you to run Scratch in parallel with a back-end script that interfaces between the visual environment and the GPIO. Read more here and download the set-up scripts […]

  2. 26th April 2013

    […] Scratch GPIO Version 2 – Introduction for Beginners: […]

  3. 14th June 2013

    […] first things first: try to get sole LEDs working. We followed a beguinner’s guide on GPIO and managed to light some LEDS, using the WiringPi […]

  4. 2nd July 2013

    […] depending on which language you want to program in – including visual designers such as Scratch. WiringPi lets you write in the same language that Arduino is derived from. Finally, you may need […]

  5. 8th August 2013

    […] This guide is based upon Simon Walter’s excellent tutorials. […]

  6. 17th September 2013

    […] and Raspberry Pi: Scratch Controlling the GPIO Pins on a Raspberry Pi […]

  7. 10th October 2013

    […] Raspbianには最初からScratchがはいってるしさ、ScratchとGPIOを繋ぐ「Scratch GPIO」(そのままじゃん!)なんていうのもあるのよ。 Scratch […]

  8. 6th November 2013

    […] is a way to allow Scratch and the Raspberry Pi to communicate, based on the work of cymplecy. He wrote a program (source code available) which allows the device and Scratch to communicate, and […]

  9. 28th February 2014

    […] Scratch GPIO addition – Great website with guidance here. […]

  10. 4th March 2014

    […] ScratchGPIO – Introduction for Beginners […]

  11. 17th March 2014

    […] Scratch programming on their PCs are just getting started with Raspberry Pi. We’re using this Scratch modification together with the hardware I’m making which is based on this circuit. The main thing here is […]

  12. 16th June 2014

    […] the Arduino only requires a few lines of code for this, the Raspberry Pi requires that you firstly download a library to control the GPIO pins (outputs/inputs), then choose which language you want to program with or use a visual language like […]

Leave a Reply

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