The Raspberry Pi is a versatile SBC (Single Board Computer) that allows a variety of embedded controllers to be built. The projects discussed in this hands-on series are a small example of the electronic devices and gadgets that can be built with a Raspberry Pi.
In part 7 of the Building Raspberry Pi controller series, you will learn how to make an interactive graphics bouncing ball using a few off-the-shelf electrical-electronic components and physical computing.
The parts list shows the various components required to build the controller.
Project Parts List
Raspberry Pi (Model A+, B, B+, Pi2, Pi Zero, or the Pi3)
1 Kilo-ohm resistor (brown, black, brown, gold) 1/4W, 5%
10 Kilo-ohm resistor (brown, black, orange, gold) 1/4W, 5%
HDMI monitor or equivalent
tactile pushbutton switch
T-Cobbler Plus or equivalent
jumper wires (female to male)
Pygame and the Raspberry Pi
One of the key elements behind the interactive graphics controller is the Python Pygame library. The Python Pygame library makes adding graphics to Raspberry Pi animation applications easy. The Pygame library allows games and interactive objects to be created without the hassle of developing special Python code or algorithms.
The interactive graphics controller you will build uses the Pygame library to add the bouncing ball image to a window called a canvas.
The canvas dimensions are built using the Pygame instruction calledsize. Thesize instruction defines thewidthandheightof the canvas as shown next.
size = width, height = 500, 500
In summary, the Pygame library provides the following programming enhancements to Python:
Graphics created will not flicker.
The graphics can be controlled with the appropriate motion speeds using the Raspberry Pi.
The graphics can be controlled using a keyboard, mouse, or external sensors.
In addition to adding graphics, a digital sensor will control the image of the interactive controller using the Pygame library. You will build the digital sensor using a tactile pushbutton switch or a photocell to move the beachball on the canvas.
Now, let's discuss the functional interaction between the bouncing ball and the digital sensor using physical computing.
Physical Computing and the Raspberry Pi
The second element to the interactive graphics controller operation is the ability to react to physical motion. The physical interaction for our project will be a hand motioning a ball dribble. This technique of the sensor responding to its environment using code and electronics is called physical computing.
The physical computing environment for the sensor (photocell) with a supporting electronic circuit wired on a solderless breadboard. The motion of the hand simulates a ball dribble.
The environment for the sensor is the solderless breadboard and supporting pulldown resistor circuit. As the hand passes over the sensor, an electrical signal is generated. This electrical signal is then processed by the Raspberry Pi by way of the Python code embedded inside of the microcontroller's memory. The Pygame library will create a bouncing ball canvas allowing the graphics to move according to the Python code motion parameters. The motion parameters for this interactive animation is a diagonal direction up-down movement of the beachball on the canvas.
You will use a photocell cell as the hand motion sensor along with the Python code and Pygame library to build your physical computing-based interactive graphics controller. With an understanding of physical computing and the Pygame library, you are now ready to build the interactive graphics controller.
Electronics Circuit Wiring Methods
To build the electronic photocell sensor circuit, you will use a solderless breadboard and jumper wires. The electronic photocell sensor circuit's output will be wired to a Raspberry Pi's GPIO (General Purpose Input Output) pin. You will wire the circuit's output directly to GPIO pin 25 of the Raspberry Pi.
Before building the controller, a tactile pushbutton switch test circuit will be used to check the electrical circuit interface of the digital switch. The pulldown resistor of 1Kilo-ohms will provide the digital voltage signals of 0V and 3.3VDC to the Raspberry Pi's GPIO pin 25 when the tactile pushbutton switch is released and pressed.
The 40 pin male connector unfortunately doesn't have the GPIO pins identified on the PCB (Printed Circuit Board).
Another method of wiring the electronic photocell sensor circuit to the Raspberry Pi is to use the Adafruit T-Cobbler Plus. The T-Cobbler Plus is an electrical breakout board providing access to the Raspberry Pi's GPIO pins.
The T-Cobbler Plus inserts into a standard solderless breadboard and jumper wires are attached to the desired GPIO pins. A flat ribbon cable attaches between the T-Cobbler Plus and Raspberry Pi's electrical connectors. The tactile pushbutton switch circuit wired to the T-Cobbler Plus mounted on a solderless breadboard. The T-Cobbler Plus is attached to a Raspberry Pi2 using a flat ribbon cable. The author's interactive graphics controller's tactile pushbutton switch test circuit.
Testing the Interactive Graphics Controller
Before the interactive graphics controller can be tested, you'll need to unzip the ButtonBouncingBall_materials folder within your Raspberry Pi file directory. You'll see the beachball2.png and the ButtonBouncingBall.py files in the zipped folder.
Next, run the application by opening the LXTerminal located on the Raspberry Pi's desktop. Type the Linux command "cd" (change directory) into the LXTerminal to gain access to the ButtonBouncingBall.py Python code. The key functions of the Python code are highlighted by comment statements. Review these comment statements to understand how the Python code works before running it..
Press the tactile pushbutton switch with your finger several times to see the beachball bounce diagonally down the canvas. Congratulations on building your interactive graphics controller! The final project build is to add a hand-dribble detection feature as discussed, using an electronic photocell sensor circuit.
Hand-Dribble Detection Feature
To bounce the beachball by hand-dribble requires the tactile pushbutton switch to be replaced with an electronic photocell sensor circuit. The electronic photocell sensor circuit is wired as a digital switch. Placing your hand over the photocell will provide a binary 1 (3.3V) control signal to be read by the Raspberry Pi. The Raspberry Pi will process this digital control signal and allow the beachball to move or bounce.
Removing your hand from the photocell provides a binary 0 (0V) digital control signal. The beachball stops bouncing as controlled by the Raspberry Pi.
utton switch for hand-dribble detection.
You can review the operation of the project shown in the small video clip located below. Run the Python application code using the Linux command discussed earlier. As you provide a hand-dribbling motion to the electronic photocell sensor, the beachball will bounce on the Pygame canvas.
Again, congratulations on successfully adding the hand-dribble detection feature to your interactive graphics controller. Next time, we'll explore adding a camera to the Raspberry Pi for photo imaging fun.