About the Author
Dr. Simon Monk has a bachelors degree in cybernetics and computer science and a Ph.D. in software engineering. He is now a full-time writer and has authored numerous books, including Programming Arduino, 30 Arduino Projects for the Evil Genius, Hacking Electronics, and Fritzing for Inventors. Dr. Monk also runs the website MonkMakes.com, which features his own products. You can follow him on Twitter, where he is @simonmonk2.
Copyright 2016, 2013 by McGraw-Hill Education. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.
ISBN: 978-1-25-958741-2
MHID: 1-25-958741-X
The material in this eBook also appears in the print version of this title: ISBN: 978-1-25-958740-5, MHID: 1-25-958740-1.
eBook conversion by codeMantra
Version 1.0
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in corporate training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com.
McGraw-Hill Education, the McGraw-Hill Education logo, TAB, and related trade dress are trademarks or registered trademarks of McGraw-Hill Education and/or its affiliates in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. McGraw-Hill Education is not associated with any product or vendor mentioned in this book.
Raspberry Pi is a trademark of the Raspberry Pi Foundation.
Information has been obtained by McGraw-Hill Education from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill Education, or others, McGraw-Hill Education does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information.
TERMS OF USE
This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill Educations prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED AS IS. McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill Education and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill Education has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
To my brothers, Andrew and Tim Monk, for their love and wisdom.
CONTENTS AT A GLANCE
CONTENTS
PREFACE
The Raspberry Pi is rapidly becoming a worldwide phenomenon. People are waking up to the possibility of a $35 (U.S.) computer that can be put to use in all sorts of settingsfrom a desktop workstation to a media center to a controller for a home automation system.
This book explains in simple terms, to both nonprogrammers and programmers new to the Raspberry Pi, how to start writing programs for the Pi in the popular Python programming language. It then goes on to give you the basics of creating graphical user interfaces and simple games using the pygame
module.
The software in the book mostly uses Python 3, with the occasional use of Python 2 where necessary for module availability. The Raspbian Wheezy distribution recommended by the Raspberry Pi Foundation is used throughout the book.
The book starts with an introduction to the Raspberry Pi and covers the topics of buying the necessary accessories and setting everything up. You then get an introduction to programming while you gradually work your way through the next few chapters. Concepts are illustrated with sample applications that will get you started programming your Raspberry Pi.
Four chapters are devoted to programming and using the Raspberry Pis GPIO connector, which allows the device to be attached to external electronics. These chapters include three sample projectsa LED lighting controller, a LED clock, and a Raspberry Picontrolled robot, complete with ultrasonic rangefinder.
Here are the key topics covered in the book:
Python numbers, variables, and other basic concepts
Strings, lists, dictionaries, and other Python data structures
Modules and object orientation
Files and the Internet
Graphical user interfaces using Tkinter
Game programming using pygame
Interfacing with hardware via the GPIO connector
Sample hardware projects
All the code listings in the book are available for download from the books website at www.raspberrypibook.com, where you can also find other useful material relating to the book, including errata.