ScratchGPIO – 1st Project

To get started using Scratch to control stuff with a Raspberry you can either get:

  • An electronics breadboard, some Light Emitting Diodes (LEDs), some resistors and some wire connectors. Available from many retailers or as an all-in-one kit from CPC for £5.
  • One of the many add-on boards available e.g PiGlow, PiBrella, PiRingo, PiMatrix etc (All about £10 or less)

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

If in doubt of your electronics ability – then buy a ready soldered add-on board from one of the many Raspberry PI add-on manufacturers and use them instead.

So assuming you have a a breadboard and 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 330 ohm 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 (ScratchGPIO) on your desktop.

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 ScratchGPIO

As it comes, six pins are set to be used as outputs (Pins 11,12,13,15,16 and 18) and 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)

pinsonpinsoffAs 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  along with the corresponding pin off messages.



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.

inputsInputs 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.  If you connect a switch to one of these pins and the other side of the switch to 0V (pin6),  then you can detect when the switch is open or closed. The pins will normally give a value of 1 and change to 0 when the switch is closed. 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 “feature” 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 automatically 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.

For a complete list of all commands that ScratchGPIO understands see here.

Once you’ve mastered switch LEDs on and off, you may wish to start playing with motor control – if so try this out


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

sudo python

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.

54 thoughts on “ScratchGPIO – 1st Project

  1. Hi, thanks for this utility! Just wondered, why would it be that when I click the drop down in the “sensor value” block in the Sensing palette, I can only see options for pins 10,19,21,22,23,24,26,7,8 (plus slider, light, sound, resistance-A to D, tilt, distance)? I.e., options for pins 3 and 5 aren’t there. I’ve installed scratchgpio5 following the instructions on the previous page. Thanks!

    • The (plus slider, light, sound, resistance-A to D, tilt, distance) are for the Official old Sensor board.

      Pin 3 and 5 are missing as they are used for something called I2C and to stop conflicts happening, there are not avialble as plain inputs as defaults. If you find a need tot use them – give me a shout and I’ll look into for you 🙂

  2. How can I run from the desktop. A error is returned when trying to start this from the desktop? I have to open a terminal window and type sudo ./ to run the scratchgpio.
    How do open an existing python script that I already have created for example a file
    thank you for your help

  3. Hi. I have a working interface on breadboard which I can control using python. I’ve tried the blink11 program and further programs to give led sequences and it works fine when run from lx terminal. However, I can’t get scratch gpio5 to drive the interface! Your help would be appreciated.

    • If your run ScratchGPIO and do
      On Green Flag
      broadcast Version

      then check the sensor blocks (2nd last block in Sensor section) and see if you have a version sensor item there.

      This tests to see if the background handler program is running OK


      • Hi Simon.
        I gave it a try and, after running the script you suggested, there isn’t a version sensor item in the sensor section.
        How should I proceed?

      • Standard debugging is to re-download and re-install 🙂
        then if that fails
        run lx terminal
        cd scratchgpio5
        sudo python
        and we can see if you are getting any errors

      • As per your instructions to Tom, I ran


        When I run this, the handler fires up and scratchgpio7 starts working. When I close terminal, it stops again. I didn’t check if the other python scripts work, but my hopes is to be able to use the Adafruit PWM so I don’t want to have to fire up each script manually.

        I noticed that the scratchgpio7 folder is directly in the root directory and I had to SU to access it. I wonder if that has anything to do with it.

        For information’s sake, I’m using scratchgpio7 installed using the wget instructions on a Raspberry Pi 2 with Kano OS (based on Raspbian).

  4. Hi Simon
    Great! Reinstallation on a different sd card worked fine. Thank you.
    Scratch gpio5 is fantastic by the way.
    I have encountered one problem which arises when I use the broadcast pinpattern block.
    I decided to reassign the io pins to give more outputs and make my pcb easier to design and everything works fine as long as I run a simple reassignment script on starting up. However the pinpattern command still seems to want to drive the original 6 outputs. I’m very keen to do 8 bit binary counting using the outputs so this is a problem.
    My new pin assigments are: Inputs 15,19,21,23. Outputs 8,10,12,16,18,22,24,26
    How do I reprogram the pinpattern block?
    Kind regards
    ps you’ve been of great help, is there anything I can help you with?

    • The pinpattern is a very old think that I initially programmed in the early days so I haven’t played with it recently – I’ll have a revisit and see if I can improve it

      Thanks for kind words 🙂
      The main help I need is either
      A testing 🙂
      B Bits (WiFi dongles/SDCards) -if your a rich customer only 🙂

      • Hi Simon
        I’ll see if I can get the boss at work to provide some sd cards for education free gratis. I was a teacher (Science then HoD Technology) in secondary and now I work part time in the eng industry and do teacher training and special sci/tech events in primary and secondary. Lots of teachers are asking me about the pi and control so your blog has been very helpful. I was brought up on bbc micro then moved onto smartbox, logicator, dataharvest flowol etc. Scratch and the pi are rekindling my interest in control and I’ve developed a series of practical activities based around motors/mechanisms/problem solving which seem to be gathering interest in local schools – I live in the North East.
        Will let you know about the sd cards asap.

  5. Hi Simon
    I was born in Jarrow and live in Washington. SD cards have arrived. Let me know where to post them or maybe I can drop them off if you are local.

      • Hi Simon
        I hope the sd cards have come in handy.
        At last i’ve got some time to get on with my control interface project. Could you help me with the pin pattern command as mentioned previously and also with the following.
        At present I have to run a scratch program which assigns the io pins every time I want to run a control prog to operate my motorized fairground rides etc via my interface pcb. Is it possible to run a script on start up which asigns my io pins once only?
        Your help appreciated.
        My pin assignments are as follows..
        Inputs 15,19,21,23. Outputs 8,10,12,16,18,22,24,26
        Kind regards

      • I just use a series of broadcast configxxin for each input pin in your green flag event. And just set your output pins to 0
        What was the pinpattern issue again?


  6. Hi Simon,
    I’m trying to use a button as a bump sensor for a robot. I’m finding that when the ULN chip is working the sensor values for Pin 24 keep fluctuating between 1 and 0?? Any ideas why this is happening? I’ve tried different pins, all with the same outcome.

  7. Hi Simon
    The pin pattern problem arises because of my revised pin assignments.
    the pinpattern command still seems to want to drive the original 6 outputs. I’m very keen to do 8 bit binary counting using my new outputs so this is a problem.
    My new pin assigments are: Inputs 15,19,21,23. Outputs 8,10,12,16,18,22,24,26
    How do I reprogram the pinpattern block?
    Also, with regards to the initialisation of the io pins, is there a way to run a single script on starting scratch so that when any new programs are composed they are compatible with the new io pin assignments without the need for the initialisation commands again – presumably you have already done this for the commercially available io boards.
    Kind regards

    • The main problem here is your specific and unique requirements 😦

      I could alter ScratchGPIO but I think it would only be of use to you and no-one else so its not something I can get into as unfortunately 😦

      I did the pinpattern command a long time ago and I don’t think any teachers/schools use it at all.

      Unless you need very fast performance, I’d suggest a helper sprite that processes your own pinpattern type variable and switches pins appropriately.

      The same thing applies to pins assignments – if this was a board going to be used by a lot of people, then its worth me doing it – otherwise not and as I said before – all the pins can be assigned in a Green Flag event.

      So I’d add all the stuff you need into a helper sprite and then save it as rsc (Which is the name of the default script loaded by ScratchGPIO) in /home/pi/Documents/Scratch Projects and everything should be available to you.

      Alternatively, hack into and change the defaults in there

      PS If you want – I could knock up the pinpattern helper sprite for you

      • Hi Simon
        I see what you mean about the unique requirements of my io board.
        I can get by for now by running a simple config script prior to the main control pogram. I would like to find out more about the sprite idea you mentioned but don’t spend any time on this at the moment, i’m sure you are busy enough.
        In terms of the io board itself, my aim is to make lots of them (I have 15 at the moment) and will be using them in my training courses in schools and for teacher inset as from Sept this year. I have an advanced version in the prototype stage which I intend to use with seconday students. I run a local DT teachers network and we’ve decided to focus on the year5/6 ‘fairgrounds project’ in the coming year. If I get a good response I may produce larger numbers for sale generally.
        Regarding your suggestion about hacking into scratch GPIO5 I will look into this and would appreciate any pointers as to which specific commands to look for – an example of the changes required would be helpful.
        Many thanks once again

      • No problem now you’ve mentioned magic education words 🙂 What add-on name do you want to call it? Can you lend me one for testing?

      • Hi Simon. Could I have the add-on name ‘techtom’ for my io board? How will it appear in scratch and is there anything else i need to know about using it? You have the pin assigments in my earlier replies. I am busy testing out the simple boards (for primary) and it will be a month or so before the more advanced board is ready. I can lend you one for testing as soon as they are ready.
        Kind regards

      • You’ll need to use ScratchGPIOplus and set a variable called AddOn to TechTom – might I suggest that you could do with a more descriptive name 🙂 See PiDie and PiGlow and PiRoCon etc 🙂

  8. Hello Simon,

    First thanks for putting this together.

    Second, I’m trying to test an input from a particular pin. I know that the device is functioning because I have a python program that responds to “GPIO.add_event_detect(23, GPIO.FALLING, callback=myFunction)”
    I have attempted to detect this event using the ScrachGPIO5 but I can’t seem to get anything to happen. Basically, I want to know if pin 23 goes to a low voltage state. How can I accomplish this? I have tried outputting the “pin23 sensor value” but it never seems to change.


    • I’m unable to test at moment as main house WiFi is down and won’t be fixed till tomorrow 😦 Jsut for avoidance of doubt – you mean Pin23 and not GPIO23?

    • Just tried connecting pin23 to ground and it changes from a 1 to a 0 when I do that.
      Are you confusing BCM GPIO23 with physical pin23?
      ScratchGPIO uses physical pin numbers


  9. Hi Simon
    Regarding the pin pattern function. At this stage I simply want to broadcast a pin pattern to my io board (lsb on the right as in conventional binary counting). Another thing which would be very useful in explaining decimal and binary systems would be a converter in which a decimal whole number could be displayed in both decimal and binary. Could the contrers be sprites which could form part of the graphical display?

    Regarding the addon name for my io board, i see what you mean! In that case could i use the name PiTT ?
    Many thanks

      • Hi Simon
        Regarding ‘pinpattern’ and how it matches to my arrangement of output connectors and leds on my io board: I have arranged the leds/outputs as follows from left to right….(msb)26,24,22,18,16,12,10,8(lsb). I thought it important to mention this at this stage since the leds and ops play an important part in the binary counting and control activities i’m working on and have to match up. I’m doing a demo in a school today relating to this. Many thanks for all your help. Hopefully, as i become more proficient at scratch, i may be able to provide some lesson ideas for your followers.

  10. I don’t like ScratchGPIO 4, as there are NO ways to keep pin 8 and 10 as UART(TxD and RxD, ALT_0 function; necessary for serial communication bound on /dev/ttyAMA0), set individual mode of each pin (ex. pin 27 pull-down, pin 22 pull-up, other pins tri).
    Are there such features in ScratchGPIO 5?

    • If you run ScratchGPIO5plus then all pins are just left alone
      To configure say pin 11 with a pullup then
      broadcast config11in
      for pull down or tristate
      broadcast config11inpulldown
      broadcast config11inpullnone

  11. I’ve been messing with this for a couple days now. I originally installed version 4 and nothing worked. Tonight I’ve followed your instructions and (apparently) had a successful installation of version 5.

    I followed your wiring directions with pins 1 and 6, and the led lights. I move the wire from pin 1 to pin 11 and run the script and nothing. It does not light. I’ve looked through your blog posts and have tried the following: gpio17high, gpio17on, gpio17True, pin11on, pin11high, pin11True, allon, allTrue, etc…


    I ran the recommended $ sudo python from terminal and I get this:

    Traceback (most recent call last):
    File “”, line 9 in
    AttributeError: ‘module’ object has no attribute ‘setmode’

    I don’t know what else to try. I’d appreciate any info or advice you have.


      • It doesn’t appear to be an old Pi/SD card issue. I’ve got a RPi B 2 and the most recent copy of NOOBs which (if I understand it correctly) installed the most recent copy of Raspbian.

        As an update, I found your link and instructions for version 7 and installed that. I still had the same issue. However, I later found an article on how to use version 1.x where the commands gpioserveron, configxxout, and gpioxxon/off are used. I tried those and it worked when run from Scratch.

        So, I’ve got it working now when I setup a function with those commands, using configxxout for every pin I want to control.

        Is this expected? Thank you for all the work you’ve put into this. My kids have had a fantastic time getting this setup and having fun with the led. This will (hopefully) be the start of a robot project.

  12. I have two RPi’s a B+ and a newer 2. The older is running NOOBS and python and ScratchGPIO works fine. The RPi2 is running Ubuntu Mate (arm version). Python works like its supposed to, but when I load ScratchGPIO 7, the outputs go to a false low, and do not change state as directed by Scratch . . . Any Recommendations?

  13. Hello, I tried to figure out which GPIO names i should take using a MCP230xx Adapter.
    I noticed with debuging the handler that “MCP23017” is regocnized. As result scratchGPIO has found that adapter.
    But found no way to get the names of MCP23017 Pins @ PortA an @ PortB of this device.
    Is there a little tutorial for this device. Thank you in advance. Leo

    • I think it is recognised as its used in an Addon board called PiAndBash so try setting variable AddOn to PiAndBash and see if the ADC values appear in sensors

      • Hello and many thanks for your reply. I try to explain the problem with more details.
        1.)Maybe I was wrong, but I remember that I read that Adafruit I/O Expander 230XX which I use with scratchGPIO is automatically detected by the handler. This is what happend. Is the AddOn variable additionally recommanded? Should use the setting: “AddOn MCP230xx ?
        2.)The 23017 has only digital I/O Ports. What I’m trying to know is which PortsName I have use in communication with ScratchGPIO?
        Thank for helping me

      • Sorry – I just rushed that reply -got confused. Anyway, the code in the handler is there to support the embedded use of the MCP23017 inside an add on board called PiAndBash. I could prob add explict support for just the chip. I’ve ordered one off Ebay so will work on it once it arrives 🙂 BTW my new site is at

  14. Hello cympley.
    thanks for your reply.
    It’s a big pleasure for me to read,
    that you offer the possibilty to implement an MCP23017 AddOn, to us this Controller ‘as it is’.

    I ‘ve gone thru your well structured handler-code and found two board devices which are using your embedded MCP support: PiBrella and PiAndBash.
    Now I understand, that nothing will happen, when I only use MCP23017 as an addon.
    Thank you in advanced that you immediately plan to support the MCP23017
    Greetings Leo

  15. excuse me. I wrote down the wrong board name. I meant that MCP support is used for AddOn PiPiano not PiBrella.

  16. Hello Simon, thank for doing this great job with your scratch handler.
    Today I will go through version 8 of your handler, looking for the possibilty to work with “Adafruit I/O Expander 230XX” (
    You gave me the advice that current handler (version 7?) supports the MCP23017 on ‘PiAndBash’.

    Do think that it is possible to use an “Adafruit I/O Expander 230XX” now with your handler? Thank you in advance for helping me
    Greetings Leo

    • Sorry – I’ve not done anything on this front at all – I did buy a 23017 but have now lost it – TBH – I don’t think this is going to happen unless one of the major makers sticks on in their products
      Sorry 😦
      As I’ve said before – this is my old site – my new one is at

  17. Hello Simon, thank you for you answer. I think I know how much you are working on scratch.

    I asked an friend at university, if he could insert an Addon called ‘MCP23017’ into your handler version 8.
    It should support a “pure” MCP23017 added to the I2C bus.

    Are you interested in his work, if he would do it ? Then I would ask him to contact you.
    Greetings Leo

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s