RGB-LED Lesson 2 (Scratch GPIO) – Getting Started
What is Scratch?
Scratch is a beginner friendly way to program the Raspberry Pi, designed for those who have not programed before and are put off by the random keyboard mashing type code normal programming typically produces.
Programs are simply created by dragging and dropping various types of blocks together to produce a sequence of instructions to follow. The default character who is tasked with following all these instructions is “Scratch” the cat!
Clearly, Scratch is designed to appeal to kids, however don’t let that fool you, there is a lot that can be done by putting a few blocks together and because you are not distracted by syntax, you can really focus on the design and the task.
This lesson will skip over the typical usage (details are provided below in the Getting Started section on some excellent general Scratch resources) and instead we will look at getting Scratch to talk to the hardware.
Scratch and Ways To Get Started
There are lots of great resources available on getting started with Scratch, and I hope that many of you have already had a go with it anyway.
With the MagPi:
The first place I would suggest is to go to is the “Scratch Patch” section in the MagPi magazine (most issues include this series each month and are well worth following). All issues are available FREE online at www.themagpi.com
With ICT Guy:
ICT Guy has a great collection of Scratch resources, including an excellent Visual Quick Start Guide :
With MIT Scratch Site:
There are also a lot of examples and tutorials available directly on the Scratch site itself:
Or try out some of the many projects they have on the site (most of which can be played through your web-browser, or downloaded and edited in scratch):
With No Starch Press:
An honourable mention should also go to the “Super Scratch Programming Adventure!” – http://nostarch.com/scratch
Basically, if you have kids who might be interested in the Raspberry Pi, this is about as awesome as you can get.
Setup GPIO Scratch
Since Scratch comes pre-installed on the recommended Raspberry Pi distribution (Raspbian “wheezy”), that part is available as soon as you start your Raspbian desktop.
Normally, Scratch’s powers of control extend only to the screen and speakers attached to the Raspberry Pi, and usually actions are directed by the program flow or by external inputs such as the keyboard and mouse.
However, Raspberry Pi blogger “SimpleSi” (http://cymplecy.wordpress.com/) has produced a plug-in to allow Scratch to talk to the GPIO pins on the Raspberry Pi too. This allows Scratch to control “real things” and to respond to the “real environment”, which literally opens up a whole new world of possibilities.
See the Scratch GPIO website for up to date information on how to install and use it:
Scratch GPIO is installed from a single setup file.
If an internet connection is available, obtain the Scratch GPIO setup file by running the following command.
sudo wget http://goo.gl/Pthh62 –O install_scratchgpio5.sh
Or download the file directly on another computer and copy it to your Raspberry Pi SD-Card or a USB drive (see the TIPsbelow for details on how to access it on the Raspberry Pi):
TIP: In the workshop, preconfigured SD-Cards are available which can also be connected to the USB of the Raspberry Pi. They will contain the required setup file.
Run the script to install Scratch GPIO using the following command:
sudo bash install_scratchgpio5.sh
It is as simple as that, Scratch GPIO is installed on your system!
When you next start your Raspbian desktop you should find an extra scratch icon to run Scratch GPIO (which starts a special script in the background along with the normal Scratch program).
More detail is available on his post “Scratch controlling the GPIO on a RaspberryPi”:
There is also a guide available for the Pi-Stop and setting up Scratch GPIO with that (this setup is the same, but there are some additional tips if you have problems).
How it works
Aside from the direct interaction mentioned above, Scratch can also shout (called “broadcast”) to anything which is listening and can also hear (called “receive”) any other things which are shouting too.
The python program “scratch_gpio_handler.py” runs in the background and is set up to listen any of these broadcast messages and turn them into GPIO actions.
Available GPIO Commands and Pins
GPIO Pins controlled by Scratch GPIO:
The current scratch_gpio_handler.py has the GPIO pins fixed to the following inputs and outputs. The pin numbers given, are the pins as counted on the P1 GPIO header itself.
|pinXon||pinXhigh||Turns pin X ON|
|pinXoff||pinXlow||Turns pin X OFF|
|allon||allhigh||Turns all pins ON|
|alloff||allow||Turns all pins OFF|
|pinpattern1010111||Sets each pin ON or OFF depending on 1 or 0 [21,18,16,15,13,12,11]|
You will need to see SimpleSi’s blog post for more information.
|motorX||Runs motor X (A = pin11, B = pin12)|
|sonarX||Trigger input on pin23, X = echo output on an input pin|
To use the input pins, see the blog pages for more information.
Using with the RGB-LED Education Kit
Connecting to the GPIO
The pins which are allocated to outputs are slightly different to the ones we’ve previously used with python. Below shows the GPIO connections we will use for Scratch (currently only 7 outputs are available so LED05 will need to be connected to 3V3 Pin1 – to keep it disabled).
Explaining the Control
When we have used the kit with python, we use the following defines to provide easy access and control of the pins:
<br />#Setup Active states<br />#Common Cathode RGB-LEDs (Cathode=Active Low)<br />LED_ENABLE = 0<br />LED_DISABLE = 1<br />RGB_ENABLE = 1<br />RGB_DISABLE = 0<br />
This reminds us what state each pin should be to ENABLE and DISABLE the LEDs.
The key aspect here is that to light up a specific LED we need both the required RGB pin(s) enabled (switched ON) and the specific LED pin enabled (switched OFF). So if we are using the broadcast block in Scratch we shall need to say “pin18off pin11on” to light up LED3 with RGB_RED.
Of course we shall also need to put the pins into a default state (i.e. all disabled). We can use the “pinpattern” broadcast to set all the pins as we require. The above wiring means that the first 3 “bits” are our RGB pins, and the last 4 are our LED1-4 pins.
WARNING – Take note of the order of the pins when controlling using the pinpattern broadcast.
The Pins are ordered as follows: 21,18,16,15,13,12,11.
We have wired the kit so that the LED pins are in the following order: RGB1234
We set the RGB pins LOW and the LED pins HIGH to switch all the LEDs off:
Similarly, to turn them all on:
While you are free to command pins individually using the pinXon/pinXoff commands, I find it easier to use the pin-patterns to ensure all the pins are in the state I am expecting (it also makes it easier if you start swapping blocks around).
Next Time – Some Simple Steps:
Using the above script, we can make Scratch move by moving the mouse, and change the colours shown on the RGB-LED kit to match Red, Green and Blue as Scratch moves over the background colours.
Detailed steps for building this script are given in Lesson 3 (Scratch GPIO) – Some Simple Steps (if you are confident with using Scratch already and are able to produce the above without help, you may skip it).
Taking things further:
Using the bit patterns may be confusing at first (in particular enabling the LEDs by switching the pins OFF), and long term this is not a friendly way to control the RGB-LED kit. Fortunately, we can do a little work behind the scenes and help Scratch out by providing some RGB friendly commands for him to shout about!
I shall cover that in another lesson soon.