This is my 3rd tutorial with a focus on Scratch and Physical Computing. Regarding this topic, I have already published:
- Physical Computing – Scratch for Arduino, where as the name says, I explored how to use Scratch and Arduino to interact with the physical world.
- Physical Computing – Scratch for Raspberry Pi, same but using Raspberry Pi and Scratch 1.4
Here, we will learn the new version of Scratch, the 2.0, exploring some new and exciting projects.
We will learn:
- What is Scratch 2.0 and its differences with Scratch 1.4
- How to interact with LEDs, Push-Buttons and a Buzzer
- How to create Special Blocks
- How to handle with variables
- How to integrate animations and the Physical world.
Step 1: Bill of Material – BoM
For the experiences in this tutorial, you will need at least the items:
- Raspberry Pi (V2 or V3)
- LEDs (Red, Yellow, and Green)
- 1 Button
- 1 Active Buzzer
- Resistors: 3 x 220 ohm
- Full Breadboard
Step 2: The Scratch 2.0
As we know, Scratch is a great tool to teach beginners how to code, but before starting our tutorial, I really recommend that you go familiarize yourself with the Scratch 2.0 language, following some basic tutorials. Doing that will help you to better understand how to use a Raspberry Pi for Physical Computing.
There are 2 ways to do that:
- Go to Scratch Official Website, or
- Go to Official Raspberry Foundation site,
A new offline Scratch 2 version started to be delivered bundled with the last Raspbian OS release, and it is really great, with several new features that will help you to create a more professional codification.
WARNING: At least with me, the actual version introduced some “freezing issues”, that could be related with Flash. Those freezings appeard whe I tried to upload new background or sprits.
Opening the program:
When you have the Pixel desktop in front of you, go to left up corner Application Menu (the “Raspberry Pi logo”) and open it. You will find as one of the options: “Programming“.
- Open it and click on “Scratch 2” (note that the old version Scratch 1.4 (named as “Scratch“) is also there.
- Once you open the Scratch program, you will get the screen shown above.
- Click on “More Blocks“
- Click on “Add an extension“. The option “Pi GPIO” Library will be available. Select it and click [OK]
- The PI GPIO library will be installed and the “green led” shows that it is running properly.
- 2 news blocks will be now available to be used with your Physical Computing programs.
The first block:
set gpio <GPIO number> to [output high]
above has 3 possible parameters:
- output high
- output low
The second block:
> is high?
- “True” or “False” depending on GPIO digital level (“True = 1” and “False = 0”).
Scratch 2.0 also allows the creation of custom blocks, allowing code to be encapsulated and used (possibly multiple times) in a project. We will see how to do it in the examples.
Step 3: The GPIO Pin Numbering
We will not cover how to prepare the Raspberry Pi to be used in this tutorial, but If you need any help, please refer to this link: Installing Raspbian with NOOBS.
Our Raspberry Pi will be running with the latest Raspbian version of the Operational System, having PIXEL, as our desktop environment.
Once we will interact with the physical world it is good to remember that our gateway to that will be the Raspberry Pi GPIO connector. The above photo shows the connector, where I have drawn some fictional components in red.
Pin Numbering Important to mention that we will use Broadcom (BCM) pin numbering for the GPIO pins, as opposed to physical (BOARD) numbering. Any pin marked “GPIO” in the above diagram can be used as a pin number. For example, if a LED was attached to “GPIO13” (physical pin 33) you would specify the pin number as 13 and not “33”.
Step 4: Blinking a LED
t’s time for the “Hello World” of Physical Computing, the “blink of a LED”!
Let’s start connecting a LED to GPIO 13 of our Raspberry Pi (RPi, for short).
For starting, drag 2 new “set gpio” blocks to the SCRIPTS AREA,
set gpiot <13> to [output high] set gpio <13> to [output low]
Clicking on those blocks you will turn ON and OFF the LED
Now, let’s create a real blink code using Scratch 2.
For more detail: Physical Computing – Scratch 2.0 for Raspberry Pi
Current Project / Post can also be found using:
- physical computing with scratch
- raspberry rfid led project