Traditional Scratch programming involves sensing and responding to a variety of events:
Mouse clicks
Individual keystrokes
Keyboard-based user input
However, each of these events is what we can call digital. In other words, a mouse has either been clicked or it has not—there are no in-between states. Likewise, a mathematical calculation results in a particular result—there isn’t any gray area to speak of.
The PicoBoard is a separate piece of hardware you can use to bring analog, external events to your Scratch projects. Fans of the PicoBoard stress that the board allows you to connect your Scratch projects to the outside world.
For instance, how about a game that responds to voice input? Or perhaps a game that uses a custom joystick controller? You can do all this and more with the Picoboard.
The PicoBoard is, like the Raspberry Pi, a printed circuit board. As you can see in Figure 8.7, the
PCB consists of a number of inputs that cover a wide variety of analog sensory data:
1: 4 Expansion Connectors: Each plug links to a pair of alligator clip connectors that can be used to measure resistance in any external object. Scratch represents the connector states as 100 (no circuit) to 0 (complete circuit between alligator clips). Intermediate values represent the degree of resistance in the circuit between the two alligator clips.
2: Slider: Scratch quantifies the slider position in the range 0–100.
3: Light Sensor: The sensor is quantified (or rendered digitally) by Scratch in a range from 0 (totally dark) to 100 (maximum brightness detected).
4: Microphone: This sound sensor is quantified in Scratch in a range from 0 (silence) to 100 (loudest audio signal detected).
5: Button: The tactile (physical) button has two states: True (when pressed) and False (when unpressed).
6: USB: This port both provides power to the board and serves as a way to transfer data to and from the PicoBoard.
FIGURE 8.7 The PicoBoard is a sensor module that brings the external environment to Scratch.
Incidentally, analog signals are distinct from digital ones because analog signals operate on a continuum of continuously varying values. For instance, the human voice generates a wave-like pattern of data. When computers use analog-to-digital converters (which the Pi can do thanks to the Gertboard accessory), they attempt to replicate an analog waveform by using two values: 0 and 1.
The more bits you add to the conversion, the more faithfully you can reproduce the original audio signal. That’s why low bitrate MP3 audio sounds so much worse than high bitrate MP3 audio.
Obtaining a PicoBoard?
The PicoBoard is manufactured and sold by Sparkfun Electronics (http://is.gd/WyVO4D); as of this writing, the price is $44.95. Please note that the PicoBoard as it is sold by Sparkfun does not include the requisite mini USB cable that is required to use the board.
Note, also, that the PicoBoard uses the mini USB cable, not the micro USB cable that the Raspberry Pi uses.
Using a PicoBoard in Scratch
Recall that the Raspberry Pi requires at least 700 mA inbound to perform its work and that you should plan on using a powered USB hub to power any external devices.
To that point, be sure to plug your PicoBoard’s USB cable into your powered hub and not into the Pi itself.
I’ve found that the Raspbian OS automatically detects the PicoBoard, and the device is therefore immediately usable in the Raspberry Pi. This is good news because you need to manually install PicoBoard device drivers for Windows and OS X computers (you can download the drivers from the Cricket website at http://is.gd/GTkHm7).
Task: Using the PicoBoard in Scratch
Now that I’ve whet your appetite and you’ve received your PicoBoard, it is time to learn how to use it with your Raspberry Pi. Let’s begin!
1. Ensure that your PicoBoard is plugged in and that Scratch detects it. An easy way to test functionality is by using the ScratchBoard watcher.
2. Navigate to the Sensing blocks palette, right-click the slider sensor value block, and select show ScratchBoard watcher from the shortcut menu.
This action adds a Stage monitor (also called a watcher) to the Stage that displays the current values of all sensors on the PicoBoard. You can see this in action in Figure 8.8.
FIGURE 8.8 The ScratchBoard Watcher gives you at-a-glance status of all PicoBoard sensors. You can see the Watcher in the Stage area above and to the left of the Scratch Cat
sprite.
3. In Raspbian, open a web browser and download the Scratch project called “PicoBoard Tester” (http://is.gd/ry5nra).
4. In Scratch, click File, Open and navigate to the PicoBoard Tester project. Open the project and click the Green Flag icon to run it.
5. On the PicoBoard, jog the slider back and forth. Note both the graphical element on the Stage as well as the watcher readout value.
Note: The Difference Between the ScratchBoard and the PicoBoard?
The MIT Learning Lab people originally developed the PicoBoard; the device was initially yellow and carried the name Scratch Sensor Board. Before too long, ownership of the project changed hands a couple of times. Now Sparkfun Electronics owns the hardware; to celebrate they renamed the device PicoBoard and gave the PCB a nice red paint job.
6. Snap your fingers. Again, observe both the program’s graphical display as well as the value on the watcher. You’ll find that the PicoBoard microphone is pretty darned
sensitive!
7. Pick up your PicoBoard (carefully) and hold it close to a nearby light. By contrast, slowly cover up the light sensor with your cupped hand. Observe value changes in the Scratch project.
8. Click the tactile pushbutton on the PicoBoard and watch for changes in the Scratch app.
9. Finally, plug in one of the alligator clip pairs and touch the alligator clip metal ends together. Note the changes in the Scratch program that indicate you’ve completed a zero-resistance circuit between the probes.
Two final points to consider regarding this final project:
Try building your own sprites that take actions based on PicoBoard-detected events.
Remember to scour the source code of any project you download from the Scratch website.
You can learn a lot about best (and worst) practices by studying how other Scratchers think and develop their apps.
Chapter 9. Programming Raspberry Pi with Scratch—Next Steps
If you invested the necessary time to practice the Scratch programming skills you learned in Chapter 8, “Programming Raspberry Pi with Scratch—Beginnings,” you are ready to take the next step by developing and sharing a full-fledged Scratch application.
In this chapter, you learn how to create a pretty neat game, if I do say so myself. You’ve even got my blessing to remix the game and submit it to the Scratch Projects website (if you don’t know what remixing means, don’t worry—I cover that later on).
Note: Version Control, Revisited
As you learned in Chapter 8, the Scratch Team at the MIT Media Lab has upgraded their website, promotional materials, and the development environment itself to Scratch 2.0. Although Scratch 2.0 on its surface looks very different from Scratch 1.4, don’t be daunted. Everything you learned in the previous chapter and everything you learn in this chapter carries over root and branch from Scratch 1.4 to Scratch 2.0. Besides, this book is about the Raspi, and Raspi includes Scratch 1.4.