Preface

The inspiration for the Raspberry Pi was born when Eben Upton was working with computer science students at Cambridge University (see the Foreword for his own account). He saw a need for incoming students to have greater opportunities to obtain programming experience before they got to the university level. The first concept designs for what would become the Pi we know now were born in 2006. Alpha boards were demonstrated in late 2011, and the first 10 boards were auctioned off at the beginning of 2012, raising £16,000.

The first batch of 10,000 Raspberry Pis went on sale February 29, 2012. Toward the end of 2011, the SD card image for it had already been downloaded more than 50,000 times, hinting at its impending popularity. The two UK sellers at the time, Premier Farnell and RS Components, sold out within minutes, with the latter reporting more than 100,000 orders that day. Upton designed them for education—specifically Python, hence the “Pi” part of the name. But the tiny board caught the eye of already-experienced programmers and electronics hackers. As of this writing, a year and a half after that first day of sale, more than two million have been sold.

And then roughly 1.95 million of them got stuck in an office drawer while their owners gathered with hackerspace friends over beer and collectively lamented, “Yeah, I bought a Pi, but I haven’t figured out what to do with it yet. I was thinking I might use it to build a time machine and try to study the K-Pg event in person, but I’ll probably just put XBMC on it.”

We wrote this book for you, the ones who haven’t decided what to do with your languishing Pis yet. Of course, if you do just want to install XBMC, you can refer to [Hack #54] and then read the rest of the book to see the fun you’re missing. Alas, we haven’t perfected the time machine hack yet, but follow @suehle and @spotrh on Twitter, and we’ll let you know if we find a good source for flux capacitors.

We’re in what we hope is still the early stages of a return to a DIY culture. Those of you deeply embroiled in it already, who have been to every Maker Faire and joined your local hackerspace the day it opened, might insist that, on the contrary, we are deep in the midst of said return. But it hasn’t gone far enough yet. Beyond our little maker/hacker/builder/doer niche is still a wide world of disposable goods and electronics consumption and dump-tion. Our devices are increasingly designed to do what the designer intended without the flexibility to do what the owner intends, needs, or wants. Further, they often are sealed up boxes, keeping prying fingers from ripping them apart and rebuilding them to suit new visions.

The acceptance of closed, unhackable, unfixable goods is relatively new in the course of human culture. It’s not so long ago—perhaps even your own childhood if you’re over 30 or so—in which we were happily building our computers from kits and taking the TV to the repair shop instead of buying a new one. Devices like the Raspberry Pi help bring us back to that better time when we knew (or could find out) what was happening inside the things that we owned, when we could change them for the better and give them new life when they broke down.

The first chapter of this book is for everyone with a Raspberry Pi; it gives you a basis on which to build all of the hacks. From there, we move on to the larger projects that implement all of those smaller hack needs. And in the spirit of the Pi’s original purpose, we hope you learn a lot.

Who This Book Is For

Despite the potentially intimdating word “hacks” in the title, we don’t expect you to be a Linux kernel developer or electrical engineer to be able to use this book. Hacks and hacking—not in the sense you hear those words used on the six o’clock news—are how many of us learn best. Hands on, trying something new, possibly frying electronics in the process.

We’ve tried to write these hacks so that even the novice can follow along and become a Raspberry Pi hacker. It will help greatly if you have at least a rudimentary understanding of how to use the Linux command line. For the most part, we walk you through those steps, too, but in places where we haven’t, a quick look to Google or to the man pages of a command should catch you up.

As to the electronics half of the hacking, we’ve tried to spell as much out in detail as possible. For those who already have a workroom filled with jumper wires and strange parts you picked up out of the electronics store clearance bin “because they might be handy someday,” this level of detail might feel belabored. Just skip ahead to the parts that are useful to you and be thankful that your less-knowledgeable friends will be getting help from the book instead of calling you to ask if “GND” is really that important, based on the assumption that it’s an amusing nod to their childhood and stands for “Goonies Never (say) Die.”

How to Use This Book

Although you can read this book cover to cover, each hack should stand alone, so feel free to browse and jump to the different sections that interest you most. If there’s a prerequisite you need to know about, a cross-reference will guide you to the right hack.

The hacks in the book are organized into the following chapters:

Chapter 1, Configuration Hacks
The first chapter introduces you to the common needs of Raspberry Pi users, like making sure you have the right SD card for the project. It deals with the assorted parts and issues you’re likely to encounter with any Raspberry Pi project, such as power problems and getting aquainted with the GPIO pins. You’ll also find tips and tricks for dealing with some of its more finicky aspects.
Chapter 2, Hacking Linux for the Raspberry Pi
The most basic description you can give of the Raspberry Pi is “small Linux computer,” which means a lot of the things you’ll want to do will require some knowledge of Linux, working from the command line, and getting into the system. Even if you’re relatively new to Linux, we’ll help you walk through processes—like building a custom kernel and updating the firmware—that might sound challenging to the novice but don’t have to be.
Chapter 3, Raspberry Pi Around the House
Now that you’ve gotten the hardware and operating system under control, you can start looking for bigger projects. In this chapter, you’ll find some ways to use it around the house, including in really useful projects that make your home computing setup a little smoother.
Chapter 4, Hacking the Outdoors
Why should all the Pi fun be inside your house? You can use it to help out in the garden, take it geocaching, or run an animated holiday lights display from it. And that’s just the beginning. What if you could (nearly) waterproof it?
Chapter 5, Multimedia Hacks
Back inside (mostly), this chapter presents the many ways you can use your Pi for entertainment. You can play music in a few ways, including by turning the Pi itself into an FM radio or by using it to set up a home theater. Then you can take it on the road and let the kids watch movies from the backseat by integrating a touchscreen.
Chapter 6, Extend Your Pi
This final chapter addresses some larger projects (not that launching an aerial photography rig isn’t big!). Use the hacks in this chapter to attach more things to your Pi, get more control over the GPIO, and even build your own cluster of Raspberry Pis with up to 64 nodes.

Hardware Requirements

Most of the hacks in this book use the Raspberry Pi Model B, version 2. Here is a little information for you to consult and compare to the needs of your intended project before deciding whether the board you have will serve your purpose.

Why “Model A” and “Model B”?

In keeping with the educational goals, these names are a nod to the inspiration for the Raspberry Pi, the BBC Micro computer released with the same model names in 1981.

The Broadcom BCM2835 chip, 700 MHz CPU, and GPU are the same across the boards. All of them offer HDMI, composite RAC (PAL and NTSC), and a 3.5 mm audio jack. They all also have the same SD card slot. The Model A sells for $25, while the Model B sells for $35. See Table 1 for a description of notable differences.

Table 1. Differences between Model A and Model B
FeatureModel AModel B

Memory

256 MB SDRAM

512 MB SDRAM

USB ports

1

2

Ethernet

none

10/100 Ehternet RJ45

There are also two revisions of the Model B board, as shown in Figure 1.

For visual comparison, the two Model B boards: revision 1 (top) and revision 2 (bottom)
Figure 1. For visual comparison, the two Model B boards: revision 1 (top) and revision 2 (bottom)

Changes made for rev 2 include:

  • 2 2.5 mm nonplated mounting holes added
  • LED labeling: D9 (yellow) corrects 10 label to 100, and D5 (green) says ACT for “Activity” instead of OK.
  • Reset circuit added (see [Hack #17] for how to use it)
  • USB fuses removed (this helps with USB hubs that backfeed power, as long it’s no more than 2.5A under fault conditions)
  • GPIO pin changes:

    • CAM_GPIO: GPIO27 now GPIO21 routed to S5 pin 11 (S5 is the camera interface)
    • GPIO_GEN2: GPIO21 now GPIO27 routed to P1 pin 13 (P1 is the main, long strip of GPIO pins)
    • Additional GPIO added on P5 (see Figure 2), which maps as follows:

      • P1 - 5V0
      • P2 - 3.3V
      • P3 - GPIO28
      • P4 - GPIO29
      • P5 - GPIO30
      • P6 - GPIO31
      • P7 - GND
      • P8 - GND
  • Primary and secondary I2C channels switched (see Table 2)
P5 pin order (Note that “P5” label is on bottom of the board)
Figure 2. P5 pin order (Note that “P5” label is on bottom of the board)
Table 2. I2C channels in Rev 1 and Rev 2
Rev 1 Rev 2

SCL0 (GPIO 1)

routed to P1 pin 5

routed to S5 pin 13

SDA0 (GPIO 0)

routed to P1 pin 3

routed to S5 pin 14

SCL1 (GPIO 3)

routed to S5 pin 13

routed to P1 pin 5

This information is applicable for most of the hacks in the book. But, of course, just because they were tested (and possibly designed for) the Model B, it doesn’t mean that the hacks won’t work with a different model or revision (unless otherwise specified). Feel free to use what you have, but know you might need to refer to additional information online about older boards.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic
Indicates new terms, URLs, and email addresses.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, filenames, file extensions, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.

This element signifies a tip, suggestion, warning, caution or general note.

Using Code Examples

All code listings are also available at the book’s GitHub repository.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: For example: "Raspberry Pi Hacks by Ruth Suehle and Tom Callaway (O’Reilly). Copyright 2014 Ruth Suehle and Tom Callaway, 978-1-449-36234-8.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/Raspberry-Pi-Hacks.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Ruth and Tom would like to thank the following people for their inspiration, ideas, help, and support:

  • Le Dernier Bar Avant Le Fin Du Monde, which we affectionately refer to as “the greatest bar in the world,” where this book’s initial hack list was born with the help of Josh Boyer, Dennis Gilmore, a few pitchers of French beer, and a Pan Galactic Gargle Blaster
  • Every Fedora contributor, as one of the four foundations is friends and so many of you friends have helped us along the way
  • Rodney Radford, both contributor and new friend, for tirelessly answering what he no doubt thought were absurd questions, driving many miles to tinker in person, and for trying to help moderately dangerous X-Men costume ideas come true without harm to the wearer or to the book’s deadline

Ruth thanks Scott, for being the kind of husband who understands when she runs off to places with nicknames like “the greatest bar in the world” and events like Maker Faire while he stays home with two awesome kids, who, while awesome, neither serve Pan Galactic Gargle Blasters nor build massive fire-based art installations (yet). Those awesome kids are the ones this is really for—the next generation of hackers and makers—so even greater thanks to Hannah, who QAed the XBMC instructions in this book (proving they’re easy enough for a seven-year-old) and Ian, who can always say he learned to spell his first word during the writing of this book: a 12-character login password.

Tom would like to thank his wife Pam for putting up with him. He was writing this book in the midst of traveling the world, presenting at conferences, having a second child, and moving across the United States. He’d also like to dedicate his parts of this book to his sons, Jimmy (3) and Danny (0.5), who will hopefully read this book one day and think, “Dad, this stuff is so old, it doesn’t even respond to my brain WiFi signals.”

Contributors

Emmanuel Ackerman is a retired computer programmer who now spends his time doing Sufi meditation and exercises, making pottery, and doing volunteer work for Powering Potential and related groups. Ackerman contributed [Hack #63].

David Bordonada works for Libelium, which runs Cooking Hacks, a site full of both tutorials and parts for your Raspberry Pi and other hardware projects. Bordonada contributed [Hack #58].

Simon Cox is professor of computational methods and head of the Computational Engineering Design Research Group (CED) Group within the Faculty of Engineering and the Environment (FEE) at the University of Southampton. He is also the associate dean for enterprise in engineering and the environment. Cox contributed [Hack #65].

Lori Easterly is currently an information security engineer with a background in Linux and systems administration, currently residing in central Florida. Among numerous hacky interests, she is a shortwave and radio-listening enthusiast with a passion for technology, tinkering, and discovery. Easterly contributed [Hack #43].

Oliver Mattos is a search engine mechanic at Google by day and a Pi hacker by night who loves to make anything do something it wasn’t made for. Mattos contributed [Hack #49] with Oskar Weigl.

Joe Ottinger is a principal engineer on the Open Source and Standards team at Red Hat. Ottinger contributed [Hack #64].

Rodney Radford is an embedded software developer and collector of hobbies, bouncing between hardware tinkering, nearspace telemetry, high altitude rocketry, geocaching, SCUBA diving, woodworking, time travel, and robotics as time (and time travel) permits. Radford contributed [Hack #44] and [Hack #45].

Jared Smith is an open source enthusiast. He enjoys programming, systems administration, documentation, VoIP, and teaching others how to use open source software. Smith contributed [Hack #32].

Wouter van Wijk (www.woutervanwijk.nl) is the creator of Pi MusicBox. He combines being a journalist for the second biggest newspaper in the Netherlands with being a techie who is fascinated by the interaction of people and computers. He enjoys designing user interfaces and experimenting, and likes to make computers and services easier to use. van Wijk contributed [Hack #48].

Oskar Weigl is a master’s student at Imperial College and ARM engineer whose greatest passions are playing with hardware and software (and building robots)—a true forward- and reverse-engineer. Weigl contributed [Hack #49] with Oliver Mattos.

Get Raspberry Pi Hacks now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.