Learn How to Use Raspberry Pi GPIO Pins With Scratch
In this tutorial, I’ll show you how to install the ScratchGPIO expansion package, how to assemble a breadboard with LEDs and buttons, and run a simple Scratch game that uses the LEDs and breadboard buttons to play.
With the Raspberry Pi’s emphasis on education it makes perfect sense that Scratch is pre-installed on the default Raspbian build and available as an icon on the desktop.
Simon Walters, @cymplecy on Twitter, has created an expansion for Scratch that allows your program to read and write the status of the Raspberry Pi GPIO Pins. Simon blogs about his creation, ScratchGPIO at Cymplecy and continues to maintain it on his github repository.
Gather the Components
- Raspberry Pi – Model B with power supply, SD card, Monitor, keyboard, and mouse
- Red, Yellow, and Green LEDs
- 3 – 220 Ohm Resistor (Red, Red, Brown)
- 2 – 1K Ohm Resistor (Brown, Black, Red)
- 2 – Momentary On Push Button
- Full or Half sized breadboard
- 6 – Male to Female Jumper Wire
Assemble the Breadboard
The breadboard will house the LEDs and the buttons with witch ScratchGPIO will interact. The LEDs need a current limiting resistor of 220 OHMs and the buttons need the 1K OHM resistors to ensure the pins do not pass too much current.
Using the image below, setup the breadboard and jumpers to the GPIO pins shown in the diagram. The pin numbers that the GPIO module and Scratch code use are the physical pin numbers not GPIO or logical numbers. Refer to the images on the RPi Low-level peripherals page to identify the pin numbers.
- Connect the button leads from physical pins 3 and 5 to the buttons on the breadboard as show below. These pins are designated as input by default in the ScratchGPIO software.
- Connect the Red, Yellow, and Green LEDs on the breadboard to the Pi’s physical pins 11, 12, and 13 which are designated as output pins by ScratchGPIO.
- Ensure the proper resistors are on the breadboard. The 220 ohm resistors are for the LEDs and the 1K ohm resistors are for the buttons.
Setup the Raspberry Pi
There are several guides to purchasing and installing an OS on your Raspberry Pi. Install the latest Raspbian operating system. Once your Raspberry Pi is configured and booted up to the graphical desktop move on to the next step.
The ScratchGPIO software does not change or replace scratch, it adds a set of python scripts that integrate with Scratch and add the ability to control and read the GPIO pins at the hardware level. Scratch will continue to function as normal after the install.
There will, however, be a new icon on the graphical desktop of the Raspberry Pi that will launch the GPIO support scripts and Scratch together. The installer is simple and easy to use. Follow the instructions below to download and install the ScratchGPIO package.
- Double click and launch LXTerminal
- Execute the following commands:
- The installer script will complete politely saying Thank You and Finished. Saying You’re welcome is optional but certainly the polite thing to do.
- Close the LXTerm window and notice the new icon titled: Scratch GPIO4
Get the Scratch Example Program
The topic of writing Scratch programs is deep and wide. This tutorial does not cover the details of Scratch programming, but the Scratch website does an excellent job with introducing and teaching Scratch if you want to learn more.
In the steps below you will download and run a scratch game that takes advantage of the GPIO extensions and the breadboard setup you built. The game sets the output pins 11, 12, and 13 high to light the LEDs and show the health of the game’s hero, Cat.
As Cat loses health the LEDs are turned off one by one until they are all dark and the game is over. During the game play, the program checks the status of the input pins 3 and 5 and when the pins are low, or grounded, Cat moves up and down to try to avoid the charging opponent, Rex.
- Launch Midori from the desktop of your Raspberry Pi
- Download the gpiogame.sb Scratch file
- Save the downloaded file to /home/pi/scratch/Documents/Scratch Projects
- Close Midori
- Launch Scratch GPIO4 from the desktop
- Click OK on the dialog box that says Remote sensor connections enabled. This message indicates that Scratch is communicating with the GPIO subsystem.
- Select File > Open
- Select the file gpiogame to load the game
- Click on the Green Flag on the upper right corner of the Scratch window to start the game
- Press the buttons on the breadboard to help Cat avoid Rex. Click the green flag again if you want to restart the game.
As you play the game you will see how the three LEDs on the breadboard reflect the health level of Cat and that the buttons indeed move Cat up and down. I’ll point out the bits of code that interact with the Pi GPIO pins, below. For reference you can find full documentation on the ScratchGPIO package on the authors blog.
The LEDs are powered when the pins they are connected to are turned high or on. The LED will glow and the resistor will prevent too much current from passing. Click on the Stage icon in the lower left panel of the Scratch window. This will bring the script for the stage object into the editor in the center of the window.
The code snippet below is the LED and health handling routine. When the code block receives the UpdateHealth broadcast message, it assesses the health variable and sets the pins low when health is lost.
For more detail: Learn How to Use Raspberry Pi GPIO Pins With Scratch