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

[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.