Scratch & Raspberry Pi GPIO: A Great Combination

A guest post by Les Pounder, a Freelance IT Consultant and Trainer from the UK who is a regular contributor to many Linux magazines and podcasts. You can find out more about him at about.me/lespounder.

Scratch, the well known first step into programming, has been used in the classroom for the last few years. Scratch is also pre-installed on the Raspberry Pi, making it easy to access the application. An interesting project is merging Scratch and the Raspberry Pi GPIO together, making it easier for children to use the GPIO, via a friendly GUI. Read Raspberry Pi: Creating a Camera to learn more about the Raspberry Pi GPIO.

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

Here is the hardware you will need to follow along in this post:

First, we will need to set up Scratch to work with the GPIO. For this, we need to download a shell script and run it in a terminal.

In a terminal type:

sudo wget https://dl.dropbox.com/s/oql4qzm8jlnsbeb/install_scratch_gpio2.sh -O 

Then type the following:

sudo /boot/install_scratch_gpio2.sh

This will install a script that enables Scratch to talk to the GPIO. Now we need to wire up our Pi to some components. We will be using the standard pin mapping with pin 1 being the top left pin, marked “P1” on the Pi:
Connect a jumper cable to pin 6, which will be GND, and connect another cable to pin 11.
Now connect the components on the breadboard, as per the diagram:


Now, we need to start Scratch on our Raspberry Pi. To do this, double left-click on the “Scratch GPIO 2” icon, that is on the desktop.



We’ll start with a blank sketch. You will see a pop up that says “Remote Sensor Connections.” Just click OK to this.

Now, we need to click on the “Control” button, which provides us with a series of control constructs.


We will be using the control functions for this project.


In Scratch, you drag blocks, which represent the code that you wish to write, onto the center gray column.

So first drag the “When Green Flag Clicked” into the column, and look for a block called “forever.” Drag it from the left column to the center column, so that it connects with the first block.

The forever block will be an infinite loop, in which we will control the LED. Now, find the “broadcast” block, and drag it into the forever loop. On the broadcast block, click on the dropdown arrow. You should see a “new” popup appear. Click on it, and in the next box type:


Then click OK.

Next, drag the “wait 1 sec” block into the forever loop, and drag the “broadcast” block into the forever loop. Click on the dropdown link again, and click on “new”, but this time type:


Then click OK. Finally add a last “wait 1 sec” to the loop.

Your code should now look like this:


Now all you need to do is click on the green flag, located at the top right of the screen, and your code will run in an infinite loop, turning the LED on and off.

Have fun, and make sure the kids get a go!

Safari Books Online has the content you need

These books in Safari Books Online will help you create your Scratch and Raspberry Pi projects:

Scratch Cookbook will take you through the essential features of Scratch. You’ll then work through simple recipes to gain an understanding of the more advanced features of Scratch.
Raspberry Pi Cookbook helps you solve specific issues for using Raspberry Pi, the $35 system on a chip that’s taking the computer and electronics world by storm. This cookbook covers a wide range of topics from Linux and Python to sensors and displays.
Getting Started with Raspberry Pi will show you just how valuable this flexible little platform can be. This book takes you step-by-step through many fun and educational possibilities.
In Raspberry Pi Networking Cookbook is an essential reference full of practical solutions for use both at home and in the office. Beginning with step-by-step instructions for installation and configuration, this book can either be read from cover to cover or treated as an essential reference companion to your Raspberry Pi.
Raspberry Pi For Dummies will help you discover why the supply for the Pi cannot keep up with the demand! Veteran tech authors Sean McManus and Mike Cook show you how to download and install the operating system, use the installed applications, and much more.

About this author

lespounder Les Pounder is a Freelance IT Consultant and Trainer from the UK. He has worked with organizations to provide bespoke training in hardware hacking and computing, and is a regular contributor to many Linux magazines and podcasts. You can find out more about him at about.me/lespounder.

About Safari Books Online

Safari Books Online is an online learning library that provides access to thousands of technical, engineering, business, and digital media books and training videos. Get the latest information on topics like Windows 8, Android Development, iOS Development, Cloud Computing, HTML5, and so much more – sometimes even before the book is published or on bookshelves. Learn something new today with a free subscription to Safari Books Online.

Comments are closed.