Raspberry Pi Cookbook
Simon Monk
Preface
Since its launch in 2011, the Raspberry Pi has found a role both as a very low-cost Linux-based computer and as a platform for embedded computing. It has proven popular with educators and hobbyists alike, with over 2 million units sold since its release.
In this book, you will find a wide range of recipes using the Raspberry Pi, including recipes for getting started and setting up your Pi; recipes for using the Python programming language; and a large number of recipes about using the Raspberry Pi with sensors, displays, motors, and so on. The book also includes a chapter on using the Raspberry Pi with Arduino boards.
This book is designed in such a way that you can read it linearly, as you would a regular book, or access recipes at random. You can search the table of contents or index for the recipe that you want and then jump right to it. If the recipe requires you to know about other things, then it will refer you to other recipes, rather like a cookbook might refer you to base sauces before showing you how to cook something fancier.
The world of Raspberry Pi is one that moves quickly. With a large active community, new interface boards and software libraries are being developed all the time. So, besides many examples that use specific interface boards or pieces of software, the book also covers basic principles so that you can have a better understanding of how to use new technologies that come along as the Raspberry Pi ecosystem develops.
As you would expect, there is a large body of code (mostly Python programs) that accompanies the book. These programs are all open source and available on GitHub. Youll find a link to them at the Raspberry Pi Cookbook website.
For most of the software-based recipes, all you need is a Raspberry Pi. I recommend a Raspberry Pi model B. For recipes that involve making your own hardware to interface with the Raspberry Pi, I have tried to make good use of ready-made modules, as well as solderless breadboard and jumper wires to avoid the need for soldering.
For those wishing to make breadboard-based projects more durable, I suggest using protoyping boards with the same layout as a half-sized breadboard, such as those sold by Adafruit, so that the design can easily be transferred to a soldered solution.
1. Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. 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, 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.
Tip
This icon signifies a tip, suggestion, or general note.
Warning
This icon indicates a warning or caution.
Note
This icon points you to the related video for that section.
2. Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at http://www.raspberrypicookbook.com.
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 youre 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 OReilly 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 products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: Raspberry Pi Cookbook by Simon Monk (OReilly). Copyright 2014 Simon Monk, 978-1-449-36522-6.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .
3. Safari Books Online
Note
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the worlds 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 OReilly 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.
4. How to Contact Us
Please address comments and questions concerning this book to the publisher:
OReilly 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-cb.
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
5. Acknowledgments
As always, I thank Linda for her patience and support.
I also thank the technical reviewers Duncan Amos, Chaim Krause, and Steve Suehring; their comments were very helpful.
Rachel Roumeliotis has done a great job as editor on this project. Her pragmatic approach is very easy to work with and has made this a very interesting project to work on.
Thanks also to all the OReilly team, especially those I met at the Cambridge office, who were very welcoming when I visited, and of course Nan Reinhardt for her diligent copyediting.
Chapter 1. Setup and Management
1.0. Introduction
When you buy a Raspberry Pi, you are essentially buying an assembled printed circuit board. It does not even include a power supply or operating system.
The recipes in this chapter are concerned with getting your Raspberry Pi set up and ready for use.
Because the Raspberry Pi just uses standard USB keyboards and mice, most of the setup is pretty straightforward, so we will concentrate on those tasks that are specific to the Raspberry Pi.
1.1. Selecting a Model of Raspberry Pi