Fragment 4037137
Fragment 4037137
Tim Cox
BIRMINGHAM - MUMBAI
Raspberry Pi Cookbook for
Python Programmers
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.
Raspberry Pi and the Raspberry Pi logo are registered trademarks of the Raspberry Pi
Foundation.
ISBN 978-1-84969-662-3
Proofreaders
Reviewers
Simran Bhogal
Colin Deady
Kirsty Cox
Ian McAlpine
Maria Gould
Acquisition Editors Ameesha Green
Pramila Balan
Paul Hindle
Luke Presland
Rebecca Pedley Indexer
Tejal Soni
Content Development Editor
Arvind Koul Graphics
Ronak Dhruv
Technical Editors
Yuvraj Mannari
Shubhangi H. Dhamgaye
Abhinash Sahu
Novina Kewalramani
Rohit Kumar Singh Production Coordinator
Pratish Soman Aparna Bhagat
Cover Work
Copy Editors
Aparna Bhagat
Janbal Dharmaraj
Insiya Morbiwala
Aditya Nair
Karuna Narayanan
About the Author
Tim Cox lives in England with his wife and two young daughters and works as a software
engineer. His passion for programming can be traced back to one Christmas in the mid 1980s
when he received a Sinclair Spectrum 48k+ home computer (a joint present with his two elder
brothers). By typing out and modifying BASIC programs, while dreaming about building robots,
an interest in computers and electronics was sparked, which has never faded. This interest
saw him through university, where he earned a BEng in Electronics and Electrical Engineering,
and into a career in developing embedded software for a wide range of applications, for
automotive, aerospace, and the oil industry, among others.
Keen to support the vision behind the Raspberry Pi, reignite engineering in schools, and
encourage a new generation of engineers, Tim co-founded the MagPi magazine. Thanks to the
dedication and time of the volunteers who contribute to it every month, it continues to have
monthly issues and attract an ever-increasing number of readers (and writers) worldwide.
Through his site PiHardware.com, Tim produces electronic kits and helps people learn
about the Raspberry Pi and hardware interfacing; each of them is supported with detailed
instructions and tutorials to help novices build the knowledge and skills for their projects.
This is Tim's Þrst ever book; it mirrors his own experience of discovering the Raspberry Pi and
showcasing its fantastic abilities.
Writing a book about the Raspberry Pi wouldn't have been possible without
the creation itself, so thanks to the Raspberry Pi foundation for their hard
work (and good humor) in making it a huge success. The Raspberry Pi
community consists of an excellent group of exceptionally helpful people
from all over the world, and it has been a pleasure to be involved with it
from the start. In particular, I would like to thank The MagPi team that has
supported me by reviewing the chapters and helping me achieve the best
possible standard. Also thanks to the Pi3D team who worked hard to get
their library running with Python 3 for the book.
Colin Deady started his career in IT in the late 1990s when he discovered software
testing. By now he had already fallen in love with computers, thanks to his parents buying
him and his brother ZX81 and ZX Spectrum+ home computers in the 1980s. He graduated
to the Amiga 1200 in the early 1990s and spent countless hours learning the insides of the
operating system. Now with 14 years' experience in testing, he works as a test manager with
an emphasis on test automation and extolls the virtues of Agile using Kanban and behavior-
driven development to great effect. (Test early, test often; Þx early, Þx often.)
In his spare time, Colin is part of the editorial team for The MagPi (www.themagpi.com), a
community-written magazine for the Raspberry Pi. With several published articles and having
reviewed and edited many more, he has built up extensive knowledge of this tiny platform. He
can also be found jointly running The MagPi stand at regular Bristol DigiMakers events in the
UK, demonstrating projects such as a remote control robot arm, a roverbot, and LED display
boards, all of which he has programmed in Python.
Ian McAlpine was Þrst introduced to computers with his school's Research Machines
RML-380Z and his Physics teacher's Compukit UK101 microcomputer. This was followed
by a Sinclair ZX81 home computer and then a BBC Micro Model A microcomputer, which he
still has to this day. His interest in computers resulted in him acquiring an MEng in Electronic
Systems Engineering from Aston University and an MSc in Information Technology from the
University of Liverpool. Ian currently works as a senior product owner at SAP Canada.
Being introduced to the Raspberry Pi not only rekindled his desire to "tinker", but also
provided him with an opportunity to give back to the community. Consequently, Ian is a very
active member of the editorial team for The MagPi, a monthly magazine for the Raspberry Pi,
which you can read online or download for free from www.themagpi.com.
I would like to thank my darling wife, Louise, and my awesome kids, Emily
and Molly, for their patience and support.
www.PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
Þles available? You can upgrade to the eBook version at www.PacktPub.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
[email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up
for a range of free newsletters and receive exclusive discounts and offers on Packt books
and eBooks.
TM
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
f Fully searchable across every book published by Packt
f Copy and paste, print and bookmark content
f On demand and accessible via web browser
ii
Table of Contents
iii