skip to Main Content

RPi Scratch 2 + GPIO Introduction


Using a button control a sprite within Scratch 2 and then to turn an LED on and off.

What You Will Learn:

  • Control the GPIO with Scratch 2
  • Wire a button with a pull down resistor

Parts List

  • Breadboard
  • Push Button
  • 10k Ohm Resistor
  • 1k Ohm Resistor
  • LED
  • Some Jumper Wires

What is a Breadboard?

A breadboard is a prototyping platform that allows the easy creation of circuits without having to solder.  Each row of the breadboard is connected internally (not across the middle break). This mean that putting something into position 1a and 1b connects them electrically together while something plugged into position 1a and 2a are electrically isolated.  

The power rails on the left and right hand side of the breadboard are connected in a column and can be an easy way to access ground of power if you choose to route it there.  The color red and by extension the red column is often used for power while blue is often associated with ground and thus used for a ground connection.

What is a Resistor?

A resistor is an electrical component that has a specific value of resistance measured in ohms.  Resistors are used to restrict the amount of current in a circuit or facilitate a drop in voltage.  Its value of resistance can be read in ohms using the colored bands on the body of the resistor.

What is an LED?

An LED is a light emitting diode.  Diode’s are a class of electronic components that are designed to only allow current to pass through them in one direction.  This means that LEDs are polar meaning they have an anode (positive) and a cathode (negative). Current must flow into the anode and the cathode must be attached to ground.  

There are several methods of identifying the anode and cathode:

Look at the lengths of the pins, If the LEDs’ pin have not been modified the longer pin is the anode and the shorter pin is the cathode
Look at the sides of the LED a portion of the case will be flat the pin on that flat side is the cathode
Look inside of the bulb, each pin is connected to a flat piece of metal internally.  The pin that is attached to the larger piece is the cathode
An LED must have a current limiting resistor between it and ground or between it and power.  An unprotected LED will pull more and more current until it burns itself out. The value of the resistor will have an effect on the LEDs’ luminosity.  The higher the value of resistance the resistor has the less current will get to the LED and thus the dimmer the bulb will shine. The amount of current can be calculated in milliamps with Ohm’s Law, using the value in ohm’s for resistance

\text{current} = \frac{\text{voltage}}{\text{resistance}}

Getting Started:

Setting up the Hardware

When placing the button in the breadboard make sure that the pins are securely placed, it can be difficult.  Also note that the kinked leg of the LED in the diagram refers to the anode (positive) pin.

Setting up Scratch 2

Click on the Raspberry Pi icon in the upper left corner of the screen, click on Programming look for the cat icon and then click on Scratch 2.

Once the  Scratch 2 window opens click on More Block and then Add an extension.

Click on Pi GPIO to select it and then click on OK.

Now that we’ve added the blocks we need to communicate with the GPIO, Click on the Events tab and drag the “When flag clicked” block into the block area on the right hand side of the window.  

Click on the More Blocks tab and “set gpio __ to __” blocks to the “when flag clicked” block.  Fill in the blanks with 25 to input.

Next click on the Control tab and add a “forever” block to the bottom of the program.

Staying within the Control tab place an “if __ then” block inside of the “forever” block.

Next click on the More Blocks tab, place the “gpio __ is high?” block inside the “if __ then” block (place the left edge inside the hexagon).  Fill in the blank with 25.

Click on the Looks tab and place the “say Hello! For 2 seconds” block into the body of the “if __ then” block.

This is now a complete program, if you click on the green flag to start it, when you press the button on your breadboard the sprite will say “hello” for two seconds.

Now let’s also control the LED with the button as well.  Click on the More Blocks tab and drag a “set gpio __ to __” block above the “forever” block.  Fill in the blanks with 12 and output low.  This ensures that the LED will be off when the program starts.

Place a “set gpio __ to __” block before the “say Hello! For 2 seconds” block and one directly after.  In the first block fill in 12 and output high, in the second block fill in 12 and output low.

Now if you activate your program by clicking the flag, the button should activate both the sprite and blink the LED.

Whats Next?

  • Can you change the duration of the hello message and LED activation?
  • Can you make the LED blink a second time per button activation?
Back To Top