Book Image

Raspberry Pi cookbook for Python programmers

Book Image

Raspberry Pi cookbook for Python programmers

Overview of this book

Table of Contents (18 chapters)
Raspberry Pi Cookbook for Python Programmers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Since the release of the Raspberry Pi computer in February 2012, hundreds of thousands of people have been introduced to a new way of computing. Modern home computers, tablets, and phones are typically focused on providing content to the user to consume, either as a passive viewer or through basic interaction via games and activities.

However, the Raspberry Pi turns this concept on its head. The idea is that the user provides the input and the imagination, and the Raspberry Pi becomes an extension of their creativity. The Raspberry Pi provides a simple, low-cost platform that you can use to experiment with and play with your own ideas. It won't feed you information; it will let you discover it firsthand.

This book takes everything I have found exciting and interesting with the Raspberry Pi and puts it in an easy-to-follow format.

I hope that people will read this book and start their own Raspberry Pi journey; it has so much to offer, and the book is aimed squarely at showing off what you can achieve with it.

Like any good cookbook, the pages should be worn and used, and it should be something that is always being pulled off the shelf to refer to. I hope it will become your own, personal, go-to reference.

What this book covers

Chapter 1, Getting Started with a Raspberry Pi Computer, introduces the Raspberry Pi and explores the various ways that it can be set up and used, including how it can be used on a network and connected to remotely with another computer.

Chapter 2, Starting with Python Strings, Files, and Menus, guides us on how to take our first steps using Python 3, start with the basics, manipulate text, use files, and create menus to run our programs.

Chapter 3, Using Python for Automation and Productivity, explains the use of graphical user interfaces to create our own applications and utilities.

Chapter 4, Creating Games and Graphics, explains how to create a drawing application and graphical games using the Tkinter Canvas.

Chapter 5, Creating 3D Graphics, discusses how we can use the hidden power of the Raspberry Pi's graphical processing unit to learn about 3D graphics and landscapes and produce our very own 3D maze for exploration.

Chapter 6, Using Python to Drive Hardware, establishes the fact that to experience the Raspberry Pi at its best, we really have to use it with our own electronics. It discusses how to create circuits with LEDs and switches, and use them to indicate the system status and provide control. Finally, it shows us how to create our own game controller and light display.

Chapter 7, Sense and Display Real-world Data, explains the use of an analog-to-digital convertor to provide sensor readings to the Raspberry Pi. We discover how to store and graph the data in real time as well as display it on an LCD text display. Finally, we transfer the data to the Internet, which will allow us to view and share the captured data anywhere in the world.

Chapter 8, Creating Projects with the Raspberry Pi Camera Module, teaches us how to use the Raspberry Pi camera module, creating our own applications to produce time-lapse videos, stop-frame animations, and a bedtime book reader controlled with QR codes.

Chapter 9, Building Robots, takes you through building two different types of robots (a Rover-Pi and a Pi-Bug). We look at motor and servo control, using sensors, and adding a compass sensor for navigation.

Chapter 10, Interfacing with Technology, teaches us how to use the Raspberry Pi to trigger remote mains sockets, with which we can control household appliances. We learn how to communicate with the Raspberry Pi over a serial interface and use a smartphone to control everything using Bluetooth. Finally, we look at creating our own applications to control USB devices.

Appendix, Hardware and Software List, provides us with the full list of the hardware components and modules used in the book, along with suitable places to purchase them from. A full list of the software used is also provided, along with links to documentation.

What you need for this book

This book focuses on using the Raspberry Pi with Python 3; therefore, a basic Raspberry Pi setup is required. Chapters 1 to 5 of this book make use of the Raspberry Pi only; no additional hardware is required beyond a standard setup.

The standard setup will consist of the Raspberry Pi (Model A or Model B); an SD card installed with Raspbian; suitable micro USB power supply; and an HDMI-compatible screen, keyboard, and mouse. You will also be required to download and install various software packages; therefore, the Raspberry Pi should have a working Internet connection.

Chapter 1, Getting Started with a Raspberry Pi Computer, also describes how to use the screen/keyboard/mouse of a laptop or another computer to access the Raspberry Pi (you just need a network cable and power).

Chapter 6, Using Python to Drive Hardware, and Chapter 7, Sense and Display Real-world Data, show how electronic components can be connected to the Raspberry Pi's interfaces. These components will be needed in order to complete these chapters.

Chapter 8, Creating Projects with the Raspberry Pi Camera Module, requires the Raspberry Pi camera module for each of the projects (although a compatible USB webcam could be substituted by adjusting the code).

Chapter 9, Building Robots, uses a range of hardware and electronics to build your own robots. You can either use your own parts or a suitable kit for this.

Chapter 10, Interfacing with Technology, shows how additional hardware can be connected to the interfaces of the Raspberry Pi using various modules and kits.

A full list of the hardware used (and the possible places to purchase it from) has been provided in the Appendix, Hardware and Software List.

Who this book is for

This book is intended for anyone who wants to make the most of the Raspberry Pi experience. The book gradually introduces Python, starting with the basics and moving towards more advanced topics, such as using 3D graphics and interfacing with hardware.

Although you do not need to be familiar with Python, the Raspberry Pi, or electronics, this book touches on a wide range of topics. Ideally, you should give each chapter a try, see what you enjoy, and use that as a starting point to discover and learn more.

Each example in the book consists of full setup instructions, complete code listings, and a walk-through of what you did and why. This will allow you to get results quickly, and most importantly, understand how you achieved them.

All the examples are written using Python 3, with clear and detailed explanations of how everything works so that you can adapt and use all the information in your own projects.

As you progress through the book, it will explain how to structure and develop your code efficiently, building on the various techniques that can be applied as you progress. By the end, you will have a toolset of skills that you can apply on whatever your imagination inspires you to do.

Safety and using electronics

This book encourages you to experiment and connect your own circuits to the general-purpose input/output Raspberry Pi GPIO pins. This is an excellent way to learn about electronics and software at the same time. However, it is important to remember that the GPIO pins are unprotected, and if wired incorrectly, can easily be damaged or even cause the Raspberry Pi to stop working altogether. Therefore, care should be taken to correctly follow the instructions and wiring diagrams and check everything carefully before switching the Raspberry Pi on.

All the circuits, modules, and components described in this book are intended as demonstration examples only. They have not been tested for extended use and should not be left unattended or should not be used in safety-critical applications without adequate safeguards in place. Remember that all electronics must undergo rigorous safety testing to ensure that in the event of failure, there will be no risk of harm to people or property.

You should never attempt to modify or alter devices that are connected to mains electricity without proper training, and you must never directly connect any homemade devices to the mains supply.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "On a freshly formatted or new SD card, copy the contents of the NOOBS_vX.zip file."

A block of code is set as follows:

network={
  ssid="theSSID"
  key_mgmt=NONE
}

Any command-line input or output is written as follows:

sudo mount –t vfat /dev/mmcblk0p1 ~/recovery

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "For OS X or Linux, click on Terminal to open a connection to the Raspberry Pi."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots used in this book. You can download this file from https://www.packtpub.com/sites/default/files/downloads/6623OT_ColorGraphics.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.