Chapter 1
IN THIS CHAPTER
Understanding how to use small computers
Using a Raspberry Pi to sense the environment around you
Making your computer do physical things
We think it's more difficult to learn about the software (Python) than the hardware, which is why in the last several hundred pages we've focused on learning how to program in Python. But now it's time for you to learn how to make your computer do something with Python. Making your computer do mechanical things — and interact with the world around you — is called physical computing!
One reason why we want to you to learn about physical computing is that little computers doing physical things (typically called embedded systems) are everywhere. And we mean everywhere. Look around your kitchen. Your refrigerator has a computer — or maybe two or three if it has a display. Your blender has a computer. Your oven has a computer. Your microwave has a computer. If you use Phillips Hue lights in your house, your light bulbs have a computer. Your car has upwards of 20 computers.
One more example. How about the lowly toaster? If you have a Bagel button or a display on your toaster, it has a computer in there. Why are there so many computers in your house? Because it's significantly less expensive to control gadgets with a computer than with special hardware. (You can buy computers in bulk for about 15¢ each!)
Most of these computers are much simpler and slower and carry much less RAM (random access storage) than your average PC. A PC may have 4GB to 16GB of RAM (1GB equals approximately 1 billion bytes), but the computer running your toaster probably has only about 100 bytes of RAM.
All these little computers are doing physical computing, sensing and interacting with the environment. The refrigerator computer is checking its temperature and then turning on the cooling machinery if necessary, minimizing the amount of electricity it uses. The stove updates its display on the front panel, monitoring the buttons and dials and controlling the temperature so you get a good lasagna for dinner.
The functionality of these very small computers was too limited to use in the examples in this book, so we compromised and used the Raspberry Pi, a $35 computer that has an immense amount of hardware and software available for use (especially with Python). It’s more complex than a toaster but much simpler than the computer in your TV.
A Raspberry Pi is a popular SBC (single board computer) that has been around since 2012. It was created by the Raspberry Pi Foundation to teach basic science and engineering in schools around the world. It turned out to be wildly popular, with more than 30 million computers sold. A bunch of other Raspberry Pi models are available, from the Raspberry Pi Zero ($5) to the new Raspberry Pi 4B (~$60). We use the Raspberry Pi 3B+ for the projects in the book, but you could also use the 4B.
To demystify some of the technology that we deal with every day, let’s talk about the major blocks of hardware on the Raspberry Pi 3B+ computer, as shown in Figure 1-1.
To get our computer to do mechanical actions and sense the environment (apart from the computer screen and keyboard), we need a computer and one of two other devices — a sensor or an actuator. A sensor is a small piece of electronics that can detect something about the environment (such as temperature or humidity), and an actuator is a fancy word for a motor or cable that does things in the real world.
In the remainder of this chapter, you will learn about the necessary ingredients of your first physical computing project, which turns on and off an LED. This first project is the physical computing version of “Hello World” that we all do when we're learning software. Blinking LED, here we come!
At the beginning of the chapter, we talked about computers in the kitchen. All those computers sense the environment (the oven temperature, for example) and most are doing something to affect the environment (your blender chopping up ice for a nice Margarita, for example). You'll be able to build and design your own projects (and believe us, after you get acquainted with the hardware, you'll be able to design wonderful things). You just need to jump in and do your first project.
Then, in further chapters, you build more complex things that will be the launching point to your own projects, all programmed in Python!
By now you have your Raspberry Pi computer set up and running on your monitor, keyboard, and mouse. If not, do that now (remember our friend, www.raspberrypi.org
). The next few paragraphs will be a lot more fun if you have a Raspberry Pi computer to work with!
The Raspberry Pi is the perfect platform for physical computing with Python because it has a multiscreen environment, lots of RAM and storage to play with, and the tools we need to build the projects we want.
A powerful feature of the Raspberry Pi is the row of GPIO (general-purpose input-output) pins along the top. Into this 40-pin header, we can plug a large number of sensors and controllers to do amazing things to expand the Raspberry Pi.
Using Python software, GPIO pins can be designated as input pins or output pins and used for many purposes. Two 5V power pins, two 3.3V power pins, and a number of ground pins have fixed uses. (See the description of voltages in the next chapter.) Figure 1-2 shows the function of each GPIO pin.
A GPIO pin outputs a 1 or a 0 from the computer to the pin. See the next chapter for more on how this output is accomplished and what it means. Basically, a 1 is 3.3V and a 0 is 0V, but you can think of them just as 1 and 0.
A number of GPIO Python libraries can be used for building projects. The one we use throughout the rest of this book is the gpiozero
library, which is installed on all Raspberry Pi desktop software releases. The library documentation and installation instructions (if needed) are located on https://gpiozero.readthedocs.io/en/stable/
.
Now we're going to jump into the “Hello World” physical computing project with our Raspberry Pi.
To do this project, you need two pieces of Grove hardware:
https://shop.switchdoc.com
or at Amazon for about $20. (You can get $5.00 off the board at shop.switchdoc.com by using the discount code PI2DUMMIES at checkout.) Lots more on this board in the next chapter.
https://shop.switchdoc.com
or Amazon for $3. (See Figure 1-4.) The LED module allows you to turn an LED on and off by using the Raspberry Pi.For more on Grove connectors, see the next chapter.
For a number of you readers, this will be the first time you've ever assembled a physical computer-based project. The following step-by-step procedure makes the process easy:
Gently push the Pi2Grover board onto the Raspberry Pi GPIO pins, making sure the pins are aligned (see Figure 1-6).
No pins should be showing on either end. Make sure the pins on the Raspberry Pi and on top of the Pi2Grover board are not bent.
We've finished assembling the hardware. Now it’s time to create the Python software.
Now that we have the hardware connected, we can apply power to the Raspberry Pi. If all is well, the Grove blue LED and a blue power LED on the Pi2Grover board will light, the yellow LED on the Raspberry Pi will flash (during bootup), and the red LED on the Raspberry Pi will light (but may then turn off depending on your power supply).
To get started, follow these steps:
Using your keyboard, open a Terminal window.
If you don’t know how to open and use a Terminal window and the command line on the Raspberry Pi, go to www.raspberrypi.org/documentation/usage/terminal/
for an excellent tutorial.
HelloWorld.py
.
from gpiozero import LED
from time import sleep
blue = LED(12)
while True:
blue.on()
print( "LED On")
sleep(1)
blue.off()
print( "LED Off")
sleep(1)
For an excellent tutorial on using the nano text editor, visit www.raspberrypi.org/magpi/edit-text/
.
sudo python3 HelloWorld.py
The LED blinks on and off once per second and the following appears on the screen in the Terminal window:
LED On
LED Off
LED On
LED Off
LED On
LED Off
LED On
LED Off
LED On
LED Off
LED On
To stop the program, press Ctrl+C (^C, in geek terms).
The following statement imports the LED
function from the Python gpiozero
library:
from gpiozero import LED
Then we import the sleep
function from the Python time
library:
from time import sleep
Next, we assign an LED on GPIO 12 (remember D12/D13 on the Pi2Grover board?):
blue = LED(12)
Now we start the loop that will go forever:
while True:
Turn on the LED:
blue.on()
print( "LED On")
Wait for one second to go by:
sleep(1)
Turn off the LED:
blue.off()
print( "LED Off")
sleep(1)
The program then loops forever — until you stop it.
Wow, you've now entered the world of physical computing. Just wait until the next minibook, where you control robots with Python.
Because we have all this hardware set up, we'll do one more project. This time, we use PWM (pulse width modulation) to vary the brightness of the LED.
Enter the following Python code into nano and save it as HelloWorld2.py
. Note the PWMLED(12)
function, which connects the LED on GPIO pin 12 to the PWM software:
from gpiozero import PWMLED
from time import sleep
led = PWMLED(12)
while True:
led.value = 0 # off
sleep(1)
led.value = 0.5 # half brightness
sleep(1)
led.value = 1 # full brightness
sleep(1)
Run the code, and the brightness will change every second:
sudo python3 HelloWorld2.py
And use the following code to see a smooth continuous brightening and darkening of the LED:
from gpiozero import PWMLED
from signal import pause
led = PWMLED(12)
led.pulse()
pause()
Boy, you accomplished a lot in this chapter. You have now started to see the possibilities of physical computing. And you have a blue LED!
3.144.252.201