Scratch is a very simple visual programming language, commonly used to teach basic programming concepts to learners of any age. In this project we’ll learn how to light up an LED when a button is pressed in Scratch, and then change a character’s colour when a physical button is pressed. With these techniques you can make all manner of fun and engaging projects, from musical keyboards to controllers for your Scratch games and animations.
Step 01 Installing the required software
Log into the Raspbian system with the username Pi and the password raspberry. Start the LXDE desktop environment using the command startx. Then open LXTerminal and type the following commands:
$ wget http://liamfraser.co.uk/lud/install_scratchgpio3.sh
$ chmod +x install_scratchgpio3.sh
$ sudo bash install_scratchgpio3.sh
This will create a special version of Scratch on your desktop called ScratchGPIO3. This is a normal version of Scratch with a Python script that handles communications between Scratch and the GPIO. ScratchGPIO was created by simplesi.
Step 02 Connecting the breadboard
Power off your Pi and disconnect the power cable. Get your breadboard, an LED, a 330-ohm resistor and two GPIO cables ready. You’ll want to connect the 3.3V pin (top-right pin, closest to the SD card) to one end of the 330-ohm resistor, and then connect the positive terminal of the LED (the longer leg is positive) to the other end. The resistor is used to limit the amount of current that can flow to the LED.
Then put the negative terminal of the LED into the negative rail of the breadboard. Connect one of the GROUND pins (for
example, the third pin from the right on the bottom row of pins) to the negative lane. Now connect the power to your Pi. The LED should light up. If it doesn’t, then it’s likely that you’ve got it the wrong way round, so disconnect the power, swap the legs around and then try again.
Step 03 Switching the LED on and off
At the moment, the LED is connected to a pin that constantly provides 3.3V. This isn’t very useful if we want to be able to turn it on and off, so let’s connect it to GPIO 17, which we can turn on and off. GPIO 17 is the sixth pin from the right, on the top row of pins. Power the Pi back on. We can turn the LED on by exporting the GPIO pin, setting it to an output pin and then setting its value to 1. Setting the value to 0 turns the LED back off:
echo 17 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio17/direction echo 1 > /sys/class/gpio/gpio17/value echo 0 > /sys/class/gpio/gpio17/value
Step 04 Controlling the LED from Scratch
Start the LXDE desktop environment and open ScratchGPIO3. Go to the control section and create a simple script that broadcasts pin11on when Sprite1 is clicked. Then click the sprite. The LED should light up. Then add to the script to wait 1 second and then broadcast pin11off. If you click the sprite again, the LED will come on for a second and then go off. ScratchGPIO3 uses pin numbers rather than GPIO numbers to identify pins. The top-right pin (the 3.3V we first connected our LED to) is pin number 1, the pin underneath that is pin number 2, and so on.
Step 05 Wiring up our push button
Power off the Pi again. This circuit is a little bit more complicated than the LED one we created previously. The first
thing we need to do is connect 3.3V (the top-right pin we used to test our LED) to the positive rail of the breadboard. Then we need to connect a 10Kohm resistor to the positive rail, and the other end to an empty track on the breadboard. Then on the same track, add a wire that has one end connected to GPIO 4. This is two pins to the right of GPIO 17. Then, on the same track again, connect one pin of the push button. Finally, connect the other pin of the push button to ground by adding a wire that is connected to the same negative rails that ground is connected to.
When the button is not pressed, GPIO 4 will be receiving 3.3V. However, when the button is pressed, the circuit to ground will be completed and GPIO 4 will be receiving 0V (and have a value of 0), because there is much less resistance on the path to ground. We can see this in action by watching the pin’s value and then pressing the button to make it change:
echo 4 > /sys/class/gpio/export echo in > /sys/class/gpio/gpio4/direction watch -n 0.5 cat /sys/class/gpio/gpio4/value
Step 06 Let there be light!
Boot up the Pi and start ScratchGPIO3 as before. Go to the control section and add when green flag clicked, then attach a forever loop, and inside that an if else statement. Go to the operators section and add an if  =  operator to the if statement. Then go to the sensing section and add a value sensor to the left side of the equality statement, and set he value to pin7. On the right side of the equality statement, enter 0. Broadcast pin11on if the sensor value is 0, and broadcast pin11off otherwise. Click the green flag. If you push the button, the LED will light up!