Appendix D. Raspberry Pi projects

In this appendix, you’ll find short discussions and descriptions of projects you can do with your Raspberry Pi. The goal is to launch you on your way. This isn’t a detailed set of instructions, but rather hints and basic steps for how you can make some of these projects.

Halloween heads

Halloween can be an inspiring time to use your Raspberry Pi to create a fun or scary display for your home. Let’s face it—it’s fun to scare people on Halloween. This project is about building a system for surprising trick-or-treaters who come to your door. When they approach, their movement will trigger a motion sensor that will display a video of a face talking or singing. The video is projected onto a Styrofoam head that is placed next to the door.

Here is what you’ll need for this project:

  • Raspberry Pi with a breadboard, a breakout board, and a ribbon cable
  • Passive infrared (PIR) motion sensor
  • Projector
  • Powered computer speakers
  • Styrofoam heads (one or more)
  • Small tables: one for the Pi and projector, and another for the Styrofoam head
  • Extension cord and power strip
  • Video of a singing or talking head

To construct this project, here are the steps:

1.  Connect your Raspberry Pi to the breadboard, and add the PIR sensor. This is similar to how you added the mini pushbutton in chapter 8.

2.  Download a video with a talking or singing head, or record your own. Write a Python program to play the video when the PIR sensor is triggered. This is similar to the DJ Raspi program, which plays a sound when the button is pressed.

3.  Test your program with the sensor and video working together.

4.  Set up a small table about 10 feet from your front door. On the table, set up your Raspberry Pi, breadboard with PIR sensor, speakers, and projector. Place the PIR sensor so that it will detect motion as someone approaches the door. Use an extension cord to provide the electrical power needed. (Only set this up if no rain is predicted!)

5.  Set up another small table or box next to your door. Place the Styrofoam head on it. Position the head so that the projector’s video displays the face on the head. Test and adjust the projector and the positioning of the head so that everything is aligned. When the video plays, the head will appear to come alive!

Here are a couple of key resources that may help you with this project:

  • Visit the SparkFun website at www.sparkfun.com and search for PIR sensors. This company has lots of great components that can help you make almost any electronics project you can imagine.
  • You can make the whole screen blank (all black) by using OMXPlayer with the blank option like so:
    pi@raspberrypi ~ $ omxplayer –b singheads.mp4

Time-lapse photography

You can easily connect a high-definition camera to your Raspberry Pi that is capable of taking digital photographs or videos (see appendix B for more information). In this project, you explore how you can set up your Pi to take time-lapse photographs.

Time-lapse photography typically involves taking a series of photographs and then stitching them together into a video. The individual photographs may be taken seconds, minutes, hours, or days apart. This technique is commonly used to show an accelerated view of something happening. Here are some examples of time-lapse scenes:

  • A glacier slowly retreating over the course of a year
  • The sun rising and setting, and the moon rising and setting
  • A plant growing

Here are some simple steps to get started with a time-lapse photography project:

1.  Set up your Raspberry Pi with the Pi camera kit, and test that it’s working.

2.  The subject of your time lapse determines how you need to mount the Pi camera. The camera doesn’t come with a case or any way to hold it up, so you’ll need to engineer a mount of some kind. Cardboard, hot glue, craft sticks, and duct tape are all great materials for fabricating something to hold up the camera. LEGO blocks can also be a useful material.

If you’re going to leave the camera outside for a long time, consider whether you’ll need to waterproof your Raspberry Pi. Plastic containers left over from takeout food can make a great case; you’ll just need to make holes in the container for wires and seal any gaps with hot glue.

3.  Plan how to get electrical power to your Pi. That may determine where you set up the Pi and camera.

4.  Program your Raspberry Pi to take the photographs and store them in a folder. Open LXTerminal, and install the picamera module for Python 3.X:

pi@raspberrypi ~ $ sudo apt-get install python3-picamera

To get you started, you can use a program like this to capture a series of photographs. This example takes a photograph every 3 minutes:

When the camera has finished taking images, you can press Ctrl-C to end the program.

Note

This program saves the images in the folder where the program is being run. You should make a folder for your time-lapse project and run the program from that folder.

Next you’ll need to combine the images into a video. You can use an application called mencoder to turn images into a movie. Install it like this:

pi@raspberrypi ~ $ sudo apt-get install mencoder

Then you’ll create a simple text file that contains all the names of the images you want to combine. You can use the list command (ls), select all the files ending in .jpg, and output the list to a text file:

pi@raspberrypi ~ $ ls image*.jpg > list.txt

Next use mencoder to combine all the individual images into a time-lapse movie. This example makes a movie called TimeLapseMovie.avi:

pi@raspberrypi ~ $ mencoder -nosound -ovc lavc -lavcopts
 vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o
 TimeLapseMovie.avi -mf type=jpeg:fps=24 mf://@list.txt

When it’s done, you can watch the movie using OMXPlayer:

pi@raspberrypi ~ $ omxplayer TimeLapseMovie.avi

You can read more online about the mencoder options available.

Raspberry Pi robot

The Raspberry Pi can readily be turned into a robot by adding servomotors and sensors using the Pi’s GPIO capabilities. The Pi can be programmed in Python to make decisions, gather input from sensors, and control servomotors to interact with the world.

Although you could assemble you own robot from scratch, there are some Raspberry Pi robot kits that can make it a lot easier. For this project, we’ll discuss using the GoPiGo kit from Dexter Industries. It’s an affordable, well-engineered kit that within a few hours will let you have your Pi moving around under your control. You can add an ultrasonic sensor (detects objects in front of it) and write a Python program to make your Pi GoPiGo robot navigate the room autonomously (on its own) using the same if/else statements you learned earlier.

We’ll cover the basic steps for building the GoPiGo (you can read the full set of instructions online at www.dexterindustries.com/GoPiGo):

1.  Build your GoPiGo robot following the online instructions. Connect your Raspberry Pi to the robot: it fits upside down on top of the GoPiGo board. The Pi communicates to the GoPiGo board through the GPIO pins.

2.  Insert the GoPiGo SD card into your Pi. The SD card contains a custom distribution of Raspbian. Connect your Pi to a keyboard, mouse, USB Wi-Fi adapter, and TV or monitor. Later you’ll be able to connect to your GoPiGo remotely from another computer. Power it up using the provided battery pack. Boot up your Pi, and connect the GoPiGo to your wireless network.

3.  Set up your computer to remotely access your Pi from another computer. This means you’ll be able to see your Raspbian desktop from another Windows or Mac computer in your home. To do so, you use software called VNC. You need to install VNC Server on your Raspberry Pi and then install VNC Client on your computer. There are some great tutorials on how to do this, such as the one on the Adafruit website. Go to https://learn.adafruit.com, and search for “installing VNC.”

4.  Using VNC, connect to your Raspberry Pi from your home Windows or Mac computer. Once you’re sure VNC Server and Client are working properly, you can disconnect your Pi from the monitor, keyboard, and mouse, leaving only the USB Wi-Fi adapter plugged in. Your GoPiGo is ready to move!

5.  From the VNC Client on your Windows or Mac, open LXTerminal on your Pi. Change directories to the GoPiGo Python folder on the desktop using the cd command:

cd Desktop/GoPiGo/Software/Python/

Run the GoPiGo test controller Python program:

sudo python basic_test_all.py

After the program starts, you can use these keys to move your GoPiGo around the room:

w Move forward
a Turn left
s Move back
d Turn right
x Stop
t Increase speed
g Decrease speed

Excellent! You’ve made your Raspberry Pi into a robot. Add sensors and make programs to navigate around a room, or attach a Pi camera and stream video to another computer so you can see what your Raspberry Pi sees.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.36.141