Scratchgpio - Introduction For Beginners: Cymplecy (Simplesi)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

http://cymplecy.wordpress.

com/2013/04/22/scratch-gpio-version-2-introduction-for-beginners/

cymplecy (simplesi)
Primary School Computer Tech Geekery

ScratchGPIO Introduction for Beginners


About these ads (http://en.wordpress.com/about-these-ads/)

This post refers to an older version of ScratchGPIO


Please use the new Version 5 (http://cymplecy.wordpress.com
/scratchgpio/)

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

sudo&wget&http://goo.gl/dANpKr&(http://goo.gl/dANpKr)&7O&isgh4.sh

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

sudo&bash&isgh4.sh

(If logged in as a different user to standard pi, then type sudo bash isgh.sh yourusername) This will install
27/03/2014 08:57

1 of 24

http://cymplecy.wordpress.com/2013/04/22/scratch-gpio-version-2-introduction-for-beginners/

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

(http://cymplecy.files.wordpress.com/2012/08
/led-power-test_bb.jpg) It should light up. If it doesnt try reversing your LED. Now move the lead

from Pin 1 to Pin 11.


27/03/2014 08:57

2 of 24

http://cymplecy.wordpress.com/2013/04/22/scratch-gpio-version-2-introduction-for-beginners/

(http://cymplecy.files.wordpress.com/2012/08/led-blink-test.jpg)

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 Scratchs 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.
(http://cymplecy.files.wordpress.com/2013/04/blink11.gif)Your LED should
now blink on for 1 second and off for 2 seconds see trouble shooting if this
doesnt 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
(http://cymplecy.files.wordpress.com/2013/04/pinson.gif) along with the corresponding pin off
27/03/2014 08:57

3 of 24

http://cymplecy.wordpress.com/2013/04/22/scratch-gpio-version-2-introduction-for-beginners/

messages.

(http://cymplecy.files.wordpress.com/2013/04/pinsoff.gif)

You can also say


(http://cymplecy.files.wordpress.com/2013/04/allonoff.gif) 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 say
(http://cymplecy.files.wordpress.com/2013/04/joined.gif) or
(http://cymplecy.files.wordpress.com/2013/04/off11on13on.gif)
Alternatively you can use the pinpattern broadcast to achieve the same result e.g:
(http://cymplecy.files.wordpress.com/2013/04/bpinpattern2.gif)This 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 youll notice that you have pins 22,7,3,5,24,26,19,21,23,8 and 10.
(http://cymplecy.files.wordpress.com/2013/04/inputs.gif) If you
connect a switch to one of these pins (through a resistor dont 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
(http://cymplecy.files.wordpress.com/2013/04
/vonoff.gif) 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 youd like.
(http://cymplecy.files.wordpress.com/2013/04
/vallonoff.gif)To set all outputs to on or off use
To use a bit-pattern to set/unset multiple outputs simultaneously use
(http://cymplecy.files.wordpress.com/2013/04/vpp.gif) (this will set Pin 11 , Pin 13, Pin 16 and Pin
18 on and Pins 12 and 15 off)
27/03/2014 08:57

4 of 24

http://cymplecy.wordpress.com/2013/04/22/scratch-gpio-version-2-introduction-for-beginners/

(http://cymplecy.files.wordpress.com/2013/04/vbug3.gif)Note 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 (http://cymplecy.wordpress.com/2013/12/27/scratchgpiodocumentation/)
Part 2 (http://cymplecy.wordpress.com/2013/04/22/scratchgpio2-using-motors/) using Motors
and varying brightness of LEDs
Troubleshooting
To test if the software necessary to control the GPIO is correctly installed open a LXTerminal session
and type
1

sudo&python&blink11.py

If this doesnt give an error but doesnt 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.
.
Posted by cymplecy on April 22, 2013 in Uncategorized.
230 Comments

About cymplecy
Geek
View all posts by cymplecy

230 responses
Older Comments
Steve says:
27/03/2014 08:57

5 of 24

You might also like