Additional Accessories
In This Chapter
As you’ve seen in preceding chapters, the Raspberry Pi is capable of doing all kinds of things on its own. However, if you really want to make some interesting projects, you can branch out a bit more and take advantage of some of the wide variety of accessories the Raspberry Pi supports.
With the right combination of add-ons, you can turn your Raspberry Pi into a full desktop computer, a camera, and more. You also can connect your Raspberry Pi to additional electronics with breakouts, pinouts, and other accessories. Essentially, you can attach your Pi to just about whatever you want if you have the right accessories.
In this chapter, you discover different pieces you can use with your Raspberry Pi, learn what they do, and take a look at a few examples of how you might use them.
Turning Your Pi Into a Mini Computer
Back in Chapter 10, you set up your Raspberry Pi as a PC, downloaded necessary software, and even gave it a more Windows-like appearance. If you want to really use your Raspberry Pi as your main computer, though, you’ll need to add a few accessories.
At this point, let’s assume you already have a keyboard, mouse, and Wi-Fi adapter set up on your Raspberry Pi. Now let’s take a look at some of the accessories you can add to turn your Raspberry Pi into a self-contained little computer.
Display Adapters and LCD Displays
We’ve already covered finding a monitor or TV for your Raspberry Pi, but there’s also a subset of specialized displays made just for the Raspberry Pi.
These displays tend to fall into two different categories: tiny, text-only LCD screens or small TFT displays.
Alphanumeric LCD displays: LCD displays are small LCD screens that look a lot like a calculator screen. They only have one color and a little bit of text. They connect to the Raspberry Pi’s display adapter so you don’t need to worry about any crazy wiring. Typically, these are used for projects where you’re only displaying a bit of text output, like if you create a weather monitor or a music player.
Small LCD displays: You don’t have to go out and buy a full-size monitor for your Raspberry Pi. Retailers like Adafruit and SparkFun have smaller, 7- or 10-inch displays you can use for your Raspberry Pi projects. Likewise, if you’re building a computer all your own, you can purchase a display without the casing so you can stuff the display into whatever kind of case you build yourself.
Regardless of what you’re looking for, there’s probably a display that works for your project, so look online. When you’re building something with a Raspberry Pi, think outside the box and consider alternatives to standard monitors. For example, if you want to build one of the retro game arcade machines mentioned in Chapter 20, you can buy a display without a case and then build it into the cabinet yourself. If you’re building a little computer, get a little display to go along with it.
HARDWARE HELPER
When you’re shopping for displays, look for ones that are compatible with the Raspberry Pi. This will make hooking up the display a lot easier, and you’ll likely be able to find technical support online if you need it. You can almost always alter a display to work with your Raspberry Pi, but it might take some technical troubleshooting to get it working.
Beyond regular displays, you also can add a touchscreen display pretty easily. Retailers like Adafruit make special touchscreen kits that work with the Raspberry Pi so you can connect a touchscreen to any project with very little effort.
One such touchscreen kit, the PiTFT, retails for just $35. It’s a tiny display, measuring in at just 2.8 inches with a resolution of 320×240, but it’s plenty capable of displaying what you need out of Raspbian.
Of course, if you need something a bit bigger, you can go up from there. Tontec makes a 7-inch display for just $48 as well as a 9-inch display for $55. These larger displays typically come with a driver board controller that handles power and any other settings you might need.
With any of these displays, you’ll need to know a little about soldering, but it’s nothing too complicated. If you’re not comfortable with soldering, you can usually find prebuilt kits online.
Why would you need a touchscreen for a Raspberry Pi? If you don’t feel like dealing with a mouse, touchscreens are a great alternative. They’re also ideal when you want to make a small-scale project that needs a screen but doesn’t need a whole computer backing it, such as a miniature tablet. But just about anything you make could benefit from a small screen. If you want your Raspberry Pi to act like a self-contained unit, this is a perfect way to do it.
And if you did get one of these touchscreens, you can skip the keyboard altogether and install a touchscreen keyboard. It takes a little bit of work, but it’s nothing too difficult. Here’s how to do it:
sudo apt-get install libfakekey-dev libpng-dev libxft-dev autoconf libtool -y
, and press Enter. This downloads some required files.git clone https://github.com/mwilliams03/matchbox-keyboard.git
, and press Enter.cd matchbox-keyboard
, and press Enter../autogen.sh
, and press Enter.make
, and press Enter.sudo make install
, and press Enter.sudo apt-get install libmatchbox1 -y
, and press Enter.Now, your Raspberry Pi has the required software to power the keyboard. You also can make things a little easier on yourself by adding a toggle to turn the keyboard on and off. Here’s how:
sudo nano /usr/bin/
toggle-matchbox.sh
, and press Enter to get into the text editor.#!/bin/bash
PID=–pidof matchbox-keyboard’
if [ ! -e $PID ]; then
killall matchbox-keyboard
else
matchbox-keyboard&
fi
When you’re done, press Ctrl+X to save and exit the text editor.
sudo chmod +x
/usr/bin/toggle-matchbox.sh
, and press Enter.sudo nano
/usr/local/share/applications/toggle-matchbox.desktop
, and press Enter.[Desktop Entry]
Name=Toggle Matchbox Keyboard
Comment=Toggle Matchbox
Keyboard Exec=toggle-matchbox.sh
Type=Application
Icon=matchbox-keyboard.png
Categories=Panel;Utility;MB
X-MB-INPUT-MECHANSIM=True
When you’re done, press Ctrl+X to save and exit.
nano ~/.config/lxpanel/LXDE/panels/panel
, and press Enter.Plugin {
type = launchbar
Config {
Button {
id=lxde-screenlock.desktop
}
Button {
id=lxde-logout.desktop
}
}
Change it to read:
Plugin {
type = launchbar
Config {
Button {
id=toggle-matchbox.desktop
}
Button {
id=lxde-screenlock.desktop
}
Button {
id=lxde-logout.desktop
}
}
When you’re done, press Ctrl+X to save and exit.
You now have a touchscreen on your Raspberry Pi and a touchscreen keyboard upon which to do all your typing. It’s not as easy to type on as a real keyboard, but it’ll certainly get you by for shorter tasks.
PI POINTER
If you’ve never soldered before, adding a touchscreen can be a bit intimidating. But don’t worry. Even if you’ve never soldered anything, what you need to do here is very simple and only takes a couple seconds. You can find a full guide to attaching a screen to your Raspberry Pi on Adafruit at learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi.
Miniature Keyboard and Mouse Combos
If you need to do a bit more typing than a touchscreen keyboard will allow but you still don’t want a full-size keyboard, a mini keyboard and mouse combination accessory might be what you need.
You have a few different options for these types of keyboard-mouse combos. If you’re looking for something very small, Adafruit sells a 6-inch keyboard and touchpad for $50 you can order from adafruit.com/product/922. It uses USB, so it’s fully compatible with the Raspberry Pi. You also can find less-expensive models made by FAVI and Rii on Amazon. These are great for projects where you need a keyboard but don’t want something full-size, like if you’re building a media center or a tiny portable Raspberry Pi.
If you need something a little bigger, any USB keyboard/touchpad combo will work. Logitech and Rosewill both make full-size keyboards that work with the Raspberry Pi out of the box.
Powered USB Hubs
Depending on which model of the Raspberry Pi you have, you have a set number of USB ports. If you need more, a powered USB hub is an easy way to add them.
Any powered USB hub will work with the Raspberry Pi, so you can expand the ports as much as you want. Powered USB hubs slot right into a USB port and easily expand your ports. They don’t need special drivers or anything else.
External Hard Drives
One of the big downsides with the Raspberry Pi is the lack of a hard drive. SD cards can only hold so much data, and if you want to do something like run a server or store media, you’re kind of out of luck. Fortunately, external hard drives are easy to add to your Raspberry Pi.
You can format a hard drive on your Raspberry Pi in a number of ways, including NTFS (Windows NT File System) and FAT (File Allocation Table). NTFS is typically the best for the Raspberry Pi because it supports large file size and volume size.
HARDWARE HELPER
An external hard drive is useful for a number of projects in this book. If you do any project that needs a lot of extra storage, an external hard drive is helpful. You can use one hard drive for multiple projects though, so don’t feel like you have to buy a new one every time.
Any external hard drive will work, but most require a USB hub to run because they need the extra power. When you have everything in hand, setting it up takes just a few steps:
sudo blkid
, and press Enter. This checks to see if the hard drive is recognized. You should see your hard drive make and model listed on the screen.sudo fdisk -l
, and press Enter. You’ll see your hard drive listed with information that reads something like /dev/sda1. Make a note of the name.sudo mount /devicenamefromstep2 /mount
, and press Enter. It should read something like sudo mount /dev/sda1 /mount. Your drive is now mounted.sudo chmod 775 /mnt
, and press Enter. This enables you to read and write to the drive.That’s all it takes to mount an external hard drive. If you want to mount it automatically on boot, you’ll need to take an additional step:
sudo nano /etc/fstab
, and press Enter. This opens the text editor./dev/sda1 /mnt ntfs defaults
0
0
. When you’re done, press Ctrl+X to save and exit.Now, your external hard drive will mount every time you plug in your Raspberry Pi. If you ever need to unmount it, just type in sudo unmounts /mnt
and press Enter.
Adding a Camera to Your Raspberry Pi
We’ve already talked a little bit about the camera module in Chapters 16 and 20, but let’s talk a little bit more about it alongside other camera solutions like webcams.
What the Camera Module Can Do
As the name implies, the Raspberry Pi Camera Module adds a camera to the Raspberry Pi. You can use it to take pictures or record video. Two different versions of the Raspberry Pi Camera Module exist: the standard one that takes pictures in normal light, and the Pi NoIR, which is meant for nighttime photography.
The camera is pretty simple. It’s only a fixed focus lens, so you can’t zoom. It’s also not particularly powerful, so although it takes decent pictures, it doesn’t have a ton of features for exposure control, white balance, or anything similar.
Support for Webcams and External Video Cameras
Beyond the officially supported camera module, the Raspberry Pi works with pretty much any USB webcam or external video camera. You can find a full list of compatible webcams at
elinux.org/RPi_USB_Webcams. To use one, you need to install some software. Here’s what to do:
sudo apt-get install fswebcam
, and press Enter. This installs the fswebcam package.fswebcam test.jpg
, and pressing Enter. You should see a still image from your webcam.fswebcam -r 128
0
x72
0
test.jpg
, and press Enter to take a picture with a 1280×720 resolution.Sure, this works, but it’s kind of a pain to remember all the commands to type in just to take a single picture. It’s easier to make a bash script that automatically saves the image with the current date. Here’s how:
sudo nano webcam.sh
, and press Enter to get into the text editor.#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
fswebcam -r 1280x720-no-banner /home/pi/webcam/$DATE.jpg
Press Ctrl+X to save and exit.
chmod +x webcam.sh
, and press Enter../webcam.sh
, and pressing Enter. This should snap a picture from your webcam and save it with the current date.Once your webcam software is installed, you can manipulate it from the command line. It also should show up in Raspbian when you need it.
If you wanted, you could follow the project in Chapter 16 for creating a surveillance camera using a webcam instead of the Raspberry Pi Camera Module. It doesn’t really matter what type of camera you use; you just need something with a lens. The camera module is the easiest and least expensive solution.
Breakouts and Pinouts for Embedded/Advanced Projects
You’ve likely noticed that your Raspberry Pi has numerous places to connect peripherals through the GPIO. To use those, you can always connect your accessories straight to the GPIO, or you can use kits and cables to make things a bit easier on yourself (and avoid soldering directly onto your Raspberry Pi).
Cobbler Kits
A cobbler kit is an easy way to plug your GPIO into a solderless breadboard. Basically, it uses a ribbon cable to connect the pins on the GPIO to a breakout that attaches to a solderless breadboard. From there, you can cobble together different wires without having to solder anything.
These aren’t meant for a permanent solution; they’re meant for testing. But such kits make things a little easier during that testing phase. Most electronics stores sell cobbler kits for les than $10.
Pinouts
Pinouts are overlays that sit on top of your Raspberry Pi’s GPIO pins so you don’t have to remember what each and every one does. You can typically find printable versions online pretty easily. Here are a few you can print yourself:
Raspberry Pi pinout for Model B: Find a simple little printable pinout that works with the Raspberry Pi Model B at doctormonk.com/2013/02/raspberry-pi-and-breadboard-raspberry.html.
Raspberry Pi pinout for Models A+, B+, and 2: Find a printable pinout for Models A+, B+, and 2 at sander.grids.be/raspberry-pi-b-printable-pinout.
Interactive Pinout Site: If you want a little extra info, Pinout is a simple site that works on computers and smartphones and provides a little more interaction. Just click the pin you’re interested in, and you’ll get a bunch more info about it. Find it at pi.gadgetoid.com/pinout.
After you printed your guide, place it on top of the GPIO pins and push them through. Then you’ll have an at-a-glance guide to what’s what.
Breadboards and Prototyping Kits
To prototype your electronics projects, you need a system that doesn’t require soldering. This reduces the chances of you getting something wrong, especially if you’re not used to soldering. If you’re connecting the Raspberry Pi to other devices through the GPIO pins, you can run them through a breadboard or a prototyping board.
A breadboard is pretty straightforward. It features terminals that connect one row to another. So if you want to connect two wires, you simply push them into the breadboard at select points to connect them instead of soldering them together. For example, if you hook a light to one end of the breadboard and a power cable to another, the light will glow.
A breadboard enables you to build complex electronics but still maintain the mobility of moving things around for troubleshooting. Breadboards are inexpensive, usually coming in at less than $5 at your electronics shop of choice.
Prototyping kits operate on a principle similar to breadboards, except they’re slightly more permanent. Typically, you’ll solder the prototyping kit on top of your Raspberry Pi. From there, you get access to a breadboardlike prototyping area where you can plug in whatever you need. Prototyping boards are modular and easy to mess around with, but they’re more permanently attached to the Raspberry Pi.
PI POINTER
Breadboards are one of those things that are a lot easier to understand once you’re actually using one. If you’re curious, go to an electronics shop and play around with one. It’ll make sense pretty quickly when you see it in action.
Which of these you prefer is really up to you and the project you’re working on. For especially intense projects, you can use both, so keep that in mind.
Other Accessories for the Raspberry Pi
A variety of accessories are available to attach to your Raspberry Pi. Let’s take a look at just a few more popular ones you might find useful in your upcoming projects.
You can add a GPS module, much like the one you have in your phone, to your Raspberry Pi. These usually plug in to one of the GPIO ports or a USB port. The initial setup depends on what type of device you have (it should come with instructions), but after that, you’ll just need to type in a few commands to get the GPS module working.
Here’s what to do:
sudo apt-get install gpsd gpsd-clients python-gps
, and press Enter.sudo gpsd /dev/USB
0
-F /var/run/gpsd.sock
and press Enter. Substitute your device address in for /dev/USB0.cgps -s
and press Enter. You should see your location displayed in your command line window.Once it’s up and running, you’ve got GPS on your Raspberry Pi.
Accelerometer Modules
An accelerometer is a simple module that adds motion control to your Raspberry Pi. This is the same type of thing that’s in your smartphone that enables it to know whether you’re holding it in landscape or portrait mode.
Accelerometers can tell if you’re speeding up or slowing down as well as sense tilting. It can sense any type of motion, so it’s also the crux of what’s inside Nintendo’s Wiimote.
PI POINTER
Accelerometers are handy in robotics as well as anything else you might want to create that requires a little balance. You could even use it in your home-automation setup to detect whether something gets moved around.
Most of us don’t have a lot of use for an accelerometer on our Raspberry Pi’s, but if you’re interested in building something like a robot, an accelerometer is a must. Setup for each is a little different and depends on what you want to do with it, so be sure to hold on to your manual when you order one. Adafruit and Sparkfun both carry accelerometers.
The Raspberry Pi doesn’t include an internal clock, which means it can’t keep track of time when it’s powered off. Oftentimes, this is totally fine, but it can be problematic when you need your Raspberry Pi to keep precise time and you don’t have an internet connection.
Real-time clocks add time-keeping functionality to your Raspberry Pi and keep track of that time using a battery. You need to connect your clock to your Raspberry Pi’s GPIO pins. After that, you just need to update a couple things on your Raspberry Pi.
This process might be slightly different for your clock, so double-check with the manufacturer before you run anything.
Here’s what to do:
sudo modprobe rtc-ds13
0
7
, and press Enter.sudo echo ds13
0
7
0
x68 > /sys/class/i2c-adapter/i2c-1/new_device
, and press Enter.sudo hwclock -r
, and press Enter to see the time. If you’re connected to the internet, you should see the correct time. If not, connect to the internet and run the command again.sudo nano /etc/modules
, and press Enter.rtc-ds13
0
7
to the end of the file, and press Ctrl+X to save and exit.sudo nano /etc/rc.local
, and press Enter. Add these lines to the file:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
Now your Raspberry Pi will load the hardware clock by default and you don’t have to worry about being connected to the internet all the time just get the right date and time.
The Raspberry Pi Computer Module was created by the Raspberry Pi Foundation and released in 2014. It’s not exactly an accessory so much as another Raspberry Pi.
On the module are all the guts of the Raspberry Pi, including the processor and RAM, as well as a flash device in place of the SD card. This is all slimmed down to fit onto a tiny, RAM-size board.
The Computer Module is designed for people who want to create their own circuit boards, so it’s not really meant for beginners, but it’s good to know that it exists if you want to check it out after you’ve gained some experience working with your Raspberry Pi.
The Least You Need to Know