21
Learn Raspberry Pi Programming with Python Wolfram Donat

Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

  • Upload
    dominh

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

Learn Raspberry Pi Programming with Python

Wolfram Donat

Page 2: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

Learn Raspberry Pi Programming with Python

Copyright © 2014 by Wolfram Donat

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4302-6424-8

ISBN-13 (electronic): 978-1-4302-6425-5

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

President and Publisher: Paul ManningLead Editor: Michelle LowmanTechnical Reviewer: Chaim KrauseDevelopment Editor: Anne Marie WalkerEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Jim DeWolf,

Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss

Coordinating Editor: Kevin SheaCopy Editor: Roger LeBlancCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com/9781430265627. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

Page 3: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

To Becky and Reed

Thank you for your patience and support when I disappear for hours, days, and weeks at a time, building all manner of off-the-wall things and then writing about them.

Page 4: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks
Page 5: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

v

Contents at a Glance

About the Author ���������������������������������������������������������������������������������������������������������������� xv

About the Technical Reviewer ������������������������������������������������������������������������������������������ xvii

Acknowledgments ������������������������������������������������������������������������������������������������������������� xix

Introduction ����������������������������������������������������������������������������������������������������������������������� xxi

Chapter 1: Introducing the Raspberry Pi ■ ���������������������������������������������������������������������������1

Chapter 2: Linux by the Seat of Your Pants ■ ���������������������������������������������������������������������15

Chapter 3: Introducing Python ■ ����������������������������������������������������������������������������������������31

Chapter 4: Electronics at 100 MPH ■ ����������������������������������������������������������������������������������51

Chapter 5: The Web Bot ■ ���������������������������������������������������������������������������������������������������67

Chapter 6: The Weather Station ■ ���������������������������������������������������������������������������������������81

Chapter 7: The Media Server ■ �����������������������������������������������������������������������������������������101

Chapter 8: The Home Security System ■ ��������������������������������������������������������������������������111

Chapter 9: The Cat Toy ■ ���������������������������������������������������������������������������������������������������127

Chapter 10: The Radio-Controlled Airplane ■ �������������������������������������������������������������������145

Chapter 11: The Weather Balloon ■ ����������������������������������������������������������������������������������161

Chapter 12: The Submersible ■ ����������������������������������������������������������������������������������������173

Chapter 13: The Gertboard ■ ��������������������������������������������������������������������������������������������201

Chapter 14: The Raspberry Pi and the Arduino ■ �������������������������������������������������������������215

Index ���������������������������������������������������������������������������������������������������������������������������������227

Page 6: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks
Page 7: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

vii

Contents

About the Author ���������������������������������������������������������������������������������������������������������������� xv

About the Technical Reviewer ������������������������������������������������������������������������������������������ xvii

Acknowledgments ������������������������������������������������������������������������������������������������������������� xix

Introduction ����������������������������������������������������������������������������������������������������������������������� xxi

Chapter 1: Introducing the Raspberry Pi ■ ���������������������������������������������������������������������������1

The History of Raspberry Pi �����������������������������������������������������������������������������������������������������������2

Exploring the Pi Board �������������������������������������������������������������������������������������������������������������������4

The SD Card ����������������������������������������������������������������������������������������������������������������������������������������������������������� 5

The Power Port ������������������������������������������������������������������������������������������������������������������������������������������������������ 5

The HDMI Port �������������������������������������������������������������������������������������������������������������������������������������������������������� 5

The Ethernet and USB Ports ���������������������������������������������������������������������������������������������������������������������������������� 6

The Audio and RCA Video Jacks ���������������������������������������������������������������������������������������������������������������������������� 6

The GPIO Pins �������������������������������������������������������������������������������������������������������������������������������������������������������� 6

The System on a Chip �������������������������������������������������������������������������������������������������������������������������������������������� 6

Comparing Raspberry Pi to Similar Devices ���������������������������������������������������������������������������������������������������������� 7

Hardware Requirements of the Pi �������������������������������������������������������������������������������������������������7

Connecting to Power ��������������������������������������������������������������������������������������������������������������������������������������������� 7

Adding a Monitor ��������������������������������������������������������������������������������������������������������������������������������������������������� 7

Adding a USB Hub �������������������������������������������������������������������������������������������������������������������������������������������������� 7

Using a Wireless USB Dongle �������������������������������������������������������������������������������������������������������������������������������� 8

The Pi Operating System ���������������������������������������������������������������������������������������������������������������9

Formatting the Card ��������������������������������������������������������������������������������������������������������������������������������������������� 10

Using NOOBS ������������������������������������������������������������������������������������������������������������������������������������������������������� 10

Page 8: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

viii

Connecting the Peripherals ���������������������������������������������������������������������������������������������������������10

Configuring the Pi ������������������������������������������������������������������������������������������������������������������������11

Shutting Down the Pi �������������������������������������������������������������������������������������������������������������������13

Summary �������������������������������������������������������������������������������������������������������������������������������������13

Chapter 2: Linux by the Seat of Your Pants ■ ���������������������������������������������������������������������15

Getting Started with Linux on the Pi ��������������������������������������������������������������������������������������������16

Linux Files and the File System ��������������������������������������������������������������������������������������������������������������������������� 17

Root User vs� sudo ����������������������������������������������������������������������������������������������������������������������������������������������� 17

Commands ����������������������������������������������������������������������������������������������������������������������������������18

Exercise: Navigating in the Linux File System �����������������������������������������������������������������������������21

Shells in Linux �����������������������������������������������������������������������������������������������������������������������������22

Package Managers ����������������������������������������������������������������������������������������������������������������������23

Text Editors ����������������������������������������������������������������������������������������������������������������������������������24

Vim vs� emacs vs� nano ��������������������������������������������������������������������������������������������������������������������������������������� 25

Summary �������������������������������������������������������������������������������������������������������������������������������������29

Chapter 3: Introducing Python ■ ����������������������������������������������������������������������������������������31

Scripting vs� a Programming Language ��������������������������������������������������������������������������������������31

The Python Philosophy ����������������������������������������������������������������������������������������������������������������33

Getting Started with Python ��������������������������������������������������������������������������������������������������������34

Running Python Using IDLE ��������������������������������������������������������������������������������������������������������������������������������� 34

Running Python Using the Terminal ��������������������������������������������������������������������������������������������������������������������� 37

Running Python Using Scripts ����������������������������������������������������������������������������������������������������������������������������� 38

Exploring Python’s Data Types ����������������������������������������������������������������������������������������������������������������������������� 39

Programming with Python �����������������������������������������������������������������������������������������������������������44

IF tests ����������������������������������������������������������������������������������������������������������������������������������������������������������������� 45

Loops ������������������������������������������������������������������������������������������������������������������������������������������������������������������� 46

Functions ������������������������������������������������������������������������������������������������������������������������������������������������������������� 48

Objects and Object-Oriented Programming ��������������������������������������������������������������������������������������������������������� 49

Summary �������������������������������������������������������������������������������������������������������������������������������������50

Page 9: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

ix

Chapter 4: Electronics at 100 MPH ■ ����������������������������������������������������������������������������������51

Basic Electricity Concepts �����������������������������������������������������������������������������������������������������������52

Required Tools for Robotics ���������������������������������������������������������������������������������������������������������54

Screwdrivers ������������������������������������������������������������������������������������������������������������������������������������������������������� 54

Pliers and Wire Strippers ������������������������������������������������������������������������������������������������������������������������������������� 54

Wire Cutters ��������������������������������������������������������������������������������������������������������������������������������������������������������� 55

Files ��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 56

Magnifying Light �������������������������������������������������������������������������������������������������������������������������������������������������� 56

Hot Glue Gun �������������������������������������������������������������������������������������������������������������������������������������������������������� 57

Assorted Glues ����������������������������������������������������������������������������������������������������������������������������������������������������� 57

Multimeter ����������������������������������������������������������������������������������������������������������������������������������������������������������� 57

Power Supplies ���������������������������������������������������������������������������������������������������������������������������������������������������� 58

Breadboard ���������������������������������������������������������������������������������������������������������������������������������������������������������� 59

Power Strip ���������������������������������������������������������������������������������������������������������������������������������������������������������� 61

Soldering Iron ������������������������������������������������������������������������������������������������������������������������������������������������������ 61

General Safety Rules �������������������������������������������������������������������������������������������������������������������62

Working with Heat ����������������������������������������������������������������������������������������������������������������������������������������������� 62

Working With Sharp Objects �������������������������������������������������������������������������������������������������������������������������������� 63

Wear Safety Glasses �������������������������������������������������������������������������������������������������������������������������������������������� 63

Fire Extinguishers at the Ready ��������������������������������������������������������������������������������������������������������������������������� 63

Keep a First-Aid Kit Handy ����������������������������������������������������������������������������������������������������������������������������������� 63

Work in a Ventilated Area ������������������������������������������������������������������������������������������������������������������������������������� 64

Organizing Your Workplace ����������������������������������������������������������������������������������������������������������64

Bonus: Soldering Techniques ������������������������������������������������������������������������������������������������������65

Summary �������������������������������������������������������������������������������������������������������������������������������������66

Chapter 5: The Web Bot ■ ���������������������������������������������������������������������������������������������������67

Bot Etiquette ��������������������������������������������������������������������������������������������������������������������������������68

The Connections of the Web ��������������������������������������������������������������������������������������������������������69

Web Communication Protocols ���������������������������������������������������������������������������������������������������������������������������� 69

Web Page Formats ���������������������������������������������������������������������������������������������������������������������������������������������� 69

A Request Example ���������������������������������������������������������������������������������������������������������������������������������������������� 70

Page 10: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

x

Our Web Bot Concept ������������������������������������������������������������������������������������������������������������������70

Parsing Web Pages ����������������������������������������������������������������������������������������������������������������������71

Coding with Python Modules �������������������������������������������������������������������������������������������������������72

Using the Mechanize Module ������������������������������������������������������������������������������������������������������������������������������ 72

Parsing with Beautiful Soup �������������������������������������������������������������������������������������������������������������������������������� 73

Downloading with the urllib Library ���������������������������������������������������������������������������������������������������������������� 73

Deciding What to Download ��������������������������������������������������������������������������������������������������������73

Choosing a Starting Point ������������������������������������������������������������������������������������������������������������������������������������ 74

Storing Your Files ������������������������������������������������������������������������������������������������������������������������������������������������� 74

Writing the Python Bot ����������������������������������������������������������������������������������������������������������������74

Reading a String and Extracting All the Links ������������������������������������������������������������������������������������������������������ 75

Looking For and Downloading Files ��������������������������������������������������������������������������������������������������������������������� 75

Testing the Bot ����������������������������������������������������������������������������������������������������������������������������������������������������� 76

Creating Directories and Instantiating a List ������������������������������������������������������������������������������������������������������� 77

The Final Code �����������������������������������������������������������������������������������������������������������������������������79

Summary �������������������������������������������������������������������������������������������������������������������������������������80

Chapter 6: The Weather Station ■ ���������������������������������������������������������������������������������������81

A Shopping List of Parts ��������������������������������������������������������������������������������������������������������������82

Using the I2C Protocol �����������������������������������������������������������������������������������������������������������������82

Using an Anemometer �����������������������������������������������������������������������������������������������������������������84

Building the Anemometer ������������������������������������������������������������������������������������������������������������������������������������ 84

Connecting the Anemometer to the Pi ����������������������������������������������������������������������������������������������������������������� 87

Correlating Revolutions per Second with Wind Speed ���������������������������������������������������������������������������������������� 88

Connecting the Digital Compass �������������������������������������������������������������������������������������������������90

Connecting the Temperature/Humidity Sensor ����������������������������������������������������������������������������92

Connecting the Barometer ����������������������������������������������������������������������������������������������������������93

Connecting the Bits ���������������������������������������������������������������������������������������������������������������������95

The Final Code �����������������������������������������������������������������������������������������������������������������������������97

Summary �����������������������������������������������������������������������������������������������������������������������������������100

Page 11: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

xi

Chapter 7: The Media Server ■ �����������������������������������������������������������������������������������������101

A Shopping List of Parts ������������������������������������������������������������������������������������������������������������101

Using an NTFS Drive ������������������������������������������������������������������������������������������������������������������101

Installing Samba ������������������������������������������������������������������������������������������������������������������������104

Configuring Samba �������������������������������������������������������������������������������������������������������������������������������������������� 104

Setting Linux Permissions ��������������������������������������������������������������������������������������������������������������������������������� 106

Fixing the Apostrophe Bug ��������������������������������������������������������������������������������������������������������������������������������� 107

Restarting the Samba Service ��������������������������������������������������������������������������������������������������������������������������� 107

Connecting with Linux/OS X ������������������������������������������������������������������������������������������������������108

Where’s Python? �����������������������������������������������������������������������������������������������������������������������110

Summary �����������������������������������������������������������������������������������������������������������������������������������110

Chapter 8: The Home Security System ■ ��������������������������������������������������������������������������111

Dogs as Security �����������������������������������������������������������������������������������������������������������������������111

Raspberry Pi as Security �����������������������������������������������������������������������������������������������������������112

Using a Sensor Network ������������������������������������������������������������������������������������������������������������112

Understanding a Pulldown Resistor ������������������������������������������������������������������������������������������������������������������� 112

A Shopping List of Parts ������������������������������������������������������������������������������������������������������������������������������������ 113

Connecting to Your Network Wirelessly ������������������������������������������������������������������������������������������������������������� 113

Accessing the GPIO Pins ������������������������������������������������������������������������������������������������������������������������������������ 116

Setting Up the Motion Sensor ���������������������������������������������������������������������������������������������������������������������������� 118

Setting Up the Reed Switch ������������������������������������������������������������������������������������������������������������������������������� 120

Setting Up the Pressure Switch ������������������������������������������������������������������������������������������������������������������������� 121

Connecting the Magnetic Sensor ����������������������������������������������������������������������������������������������������������������������� 122

Setting Up Pi’s Camera �������������������������������������������������������������������������������������������������������������������������������������� 123

Sending a Text Message from the Pi ����������������������������������������������������������������������������������������������������������������� 123

Implementing the Callback �������������������������������������������������������������������������������������������������������������������������������� 124

Connecting All of the Bits ����������������������������������������������������������������������������������������������������������124

The Final Code ���������������������������������������������������������������������������������������������������������������������������125

Summary �����������������������������������������������������������������������������������������������������������������������������������126

Page 12: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

xii

Chapter 9: The Cat Toy ■ ���������������������������������������������������������������������������������������������������127

A Shopping List of Parts ������������������������������������������������������������������������������������������������������������127

The Concept Behind the Toy ������������������������������������������������������������������������������������������������������128

Creating and Using Random Numbers ��������������������������������������������������������������������������������������129

Using the GPIO Library ��������������������������������������������������������������������������������������������������������������130

Controlling the Servo �����������������������������������������������������������������������������������������������������������������132

Constructing the Servo Mechanism ������������������������������������������������������������������������������������������134

Constructing the Laser Mechanism �������������������������������������������������������������������������������������������135

Connecting the Laser to the Servo ��������������������������������������������������������������������������������������������136

Connecting the Motion Sensor ��������������������������������������������������������������������������������������������������137

Connecting All the Bits ��������������������������������������������������������������������������������������������������������������140

The Final Code ���������������������������������������������������������������������������������������������������������������������������142

Summary �����������������������������������������������������������������������������������������������������������������������������������143

Chapter 10: The Radio-Controlled Airplane ■ �������������������������������������������������������������������145

A Shopping List of Parts ������������������������������������������������������������������������������������������������������������145

Connecting the GPS Receiver to the Pi ��������������������������������������������������������������������������������������146

Setting Up a Log File �����������������������������������������������������������������������������������������������������������������148

Formatting a KML File ���������������������������������������������������������������������������������������������������������������149

Using Threading and Objects�����������������������������������������������������������������������������������������������������151

Setting Up Automatic Startup ����������������������������������������������������������������������������������������������������152

Connecting the Bits �������������������������������������������������������������������������������������������������������������������153

The Final Code ���������������������������������������������������������������������������������������������������������������������������157

The Plane Program �������������������������������������������������������������������������������������������������������������������������������������������� 158

KML Conversion Program ���������������������������������������������������������������������������������������������������������������������������������� 159

Summary �����������������������������������������������������������������������������������������������������������������������������������159

Chapter 11: The Weather Balloon ■ ����������������������������������������������������������������������������������161

A Shopping List of Parts ������������������������������������������������������������������������������������������������������������161

Setting Up the GPS Receiver �����������������������������������������������������������������������������������������������������162

Storing the GPS Data �����������������������������������������������������������������������������������������������������������������164

Page 13: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

xiii

Installing PiFM ���������������������������������������������������������������������������������������������������������������������������164

Installing festival �����������������������������������������������������������������������������������������������������������������������165

Installing FFMPEG ���������������������������������������������������������������������������������������������������������������������165

Preparing the Pi �������������������������������������������������������������������������������������������������������������������������166

Using Threading and Objects�����������������������������������������������������������������������������������������������������167

Connecting the Bits �������������������������������������������������������������������������������������������������������������������167

Reviewing the Photo Results �����������������������������������������������������������������������������������������������������170

The Final Code ���������������������������������������������������������������������������������������������������������������������������171

Summary �����������������������������������������������������������������������������������������������������������������������������������172

Chapter 12: The Submersible ■ ����������������������������������������������������������������������������������������173

A Shopping List of Parts ������������������������������������������������������������������������������������������������������������174

Accessing the Raspberry Pi’s GPIO pins ������������������������������������������������������������������������������������174

Installing the Raspberry Pi Camera Board ��������������������������������������������������������������������������������176

Controlling the Sub ��������������������������������������������������������������������������������������������������������������������178

Attaching the Wiichuck Adapter ������������������������������������������������������������������������������������������������������������������������� 179

Activating the Pi’s I2C ���������������������������������������������������������������������������������������������������������������������������������������� 180

Testing the Nunchuk ������������������������������������������������������������������������������������������������������������������������������������������ 181

Reading from the Nunchuk �������������������������������������������������������������������������������������������������������������������������������� 182

Controlling the Sub Motors and Camera with the Nunchuk ������������������������������������������������������������������������������ 185

Starting the Program Remotely ������������������������������������������������������������������������������������������������187

The Final Control Program ���������������������������������������������������������������������������������������������������������189

The Final Code ���������������������������������������������������������������������������������������������������������������������������189

Constructing the Sub �����������������������������������������������������������������������������������������������������������������191

Building the Frame �������������������������������������������������������������������������������������������������������������������������������������������� 192

Creating the Pi’s Enclosure �������������������������������������������������������������������������������������������������������������������������������� 192

Waterproofing the Motor Enclosures ����������������������������������������������������������������������������������������������������������������� 193

Connecting the Nunchuck ��������������������������������������������������������������������������������������������������������������������������������� 196

Assembling the Final Product ���������������������������������������������������������������������������������������������������������������������������� 196

Summary �����������������������������������������������������������������������������������������������������������������������������������200

Page 14: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

■ Contents

xiv

Chapter 13: The Gertboard ■ ��������������������������������������������������������������������������������������������201

Examining the Board �����������������������������������������������������������������������������������������������������������������201

The GPIO Pins ���������������������������������������������������������������������������������������������������������������������������������������������������� 202

Atmega Chip ������������������������������������������������������������������������������������������������������������������������������������������������������ 203

A-to-D and D-to-A Converters ��������������������������������������������������������������������������������������������������������������������������� 204

I/O Section ��������������������������������������������������������������������������������������������������������������������������������������������������������� 205

The Motor Controller ������������������������������������������������������������������������������������������������������������������������������������������ 206

Open Collector Driver ����������������������������������������������������������������������������������������������������������������������������������������� 207

Jumpers ������������������������������������������������������������������������������������������������������������������������������������������������������������� 208

Some Example Projects�������������������������������������������������������������������������������������������������������������209

Configuring the Preliminary Jumper Setup ������������������������������������������������������������������������������������������������������� 209

Making Some LEDs Blink ����������������������������������������������������������������������������������������������������������������������������������� 210

Experimenting with Motor Controllers ��������������������������������������������������������������������������������������������������������������� 212

Using the Open Collector Drivers ����������������������������������������������������������������������������������������������������������������������� 213

Using the Digital/Analogue Converters �������������������������������������������������������������������������������������������������������������� 213

Summary �����������������������������������������������������������������������������������������������������������������������������������214

Chapter 14: The Raspberry Pi and the Arduino ■ �������������������������������������������������������������215

Exploring the Arduino ����������������������������������������������������������������������������������������������������������������215

Installing the Arduino IDE on the Pi �������������������������������������������������������������������������������������������218

Running a Servo ������������������������������������������������������������������������������������������������������������������������223

The Arduino and the Gertboard �������������������������������������������������������������������������������������������������224

Summary �����������������������������������������������������������������������������������������������������������������������������������225

Index ���������������������������������������������������������������������������������������������������������������������������������227

Page 15: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

xv

About the Author

Wolfram Donat is tallish, with hair and four limbs attached in approximately the correct locations. He is a Computer Engineer, author and programmer, with interests in robotics, animatronics, automation and embedded systems. He wrote his first BASIC program on a Commodore VIC-20, more years ago than he cares to admit.

He currently lives in Anchorage, Alaska, with his wife, son, and menagerie of animals.

Page 16: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks
Page 17: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

xvii

About the Technical Reviewer

Chaim Krause presently lives in Leavenworth, Kansas, where the U.S. Army employs him as a Simulation Specialist. In his spare time, he likes to play PC games and occasionally develops his own. He has recently taken up the sport of golf to spend more time with his significant other, Ivana. Although he holds a BA in Political Science from the University of Chicago, Chaim is an autodidact when it comes to computers, programming, and electronics. He wrote his first computer game in BASIC on a Tandy Model I Level I and stored the program on a cassette tape. Amateur radio introduced him to electronics, while the Arduino and the Raspberry Pi provided a medium to combine computing, programming, and electronics into one hobby.

Page 18: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks
Page 19: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

xix

Acknowledgments

Writing a book may be a solitary procedure, but it certainly can’t be done without help. A lot of work by a lot of people went into this enterprise, and they all have my deepest appreciation. My thanks to Kevin for keeping things moving smoothly, to Anne Marie for figuring out what I was trying to say, and to Roger LeBlanc for making it look like I know what I’m doing. Thanks to Chaim, the tech reviewer, for making sure the stuff I wrote actually runs and does what it’s supposed to. And thanks to Michelle, for taking notice of me in the first place.

Last but definitely not least, thanks to Becky and Reed. This one’s for you.

Page 20: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks
Page 21: Learn Raspberry Pi Programming with Python - Springer978-1-4302-6425-5/1.pdf · To Becky and Reed Thank you for your patience and support when I disappear for hours, days, and weeks

xxi

Introduction

In 2006, when Eben Upton and the other founders of the Raspberry Pi Foundation looked at the state of Computer Science (CS) programs in universities, they were dismayed. Computer science programs were being reduced to “CS 101: How To Operate Microsoft Word” and “CS 203: Optimize Your Facebook Page.” Nobody, they realized, was learning how to program any more, least of all before they entered college. So they hatched a plan—create a small, cheap computer that kids could learn to program on, like the Amigas, Spectrums, and Commodore 64s of yesteryear. They put an ARM processor on a board, gave it (eventually) 512 MB of RAM and a VideoCore GPU, and allowed users to interface with it using a USB keyboard, mouse, and an HDMI output port. To make it easy to program, they designed it so that its main programming language would be Python—a powerful, easy-to-learn scripting language. And thus the Raspberry Pi was born.

I wrote my first program in BASIC on a Commodore VIC 20, lo these many years ago. At 5 KB of RAM, it had less computing power than many of today’s microcontrollers, but I was still able to write a simple maze game on it, saving my progress as I went on a cassette-tape drive. In the years since, I’ve traversed my way through the different computing platforms, from Windows 3.1, to Macintosh OS 8, to a little bit of Linux. It had been a long time since I was truly excited by a computer; the Pi was a breath of fresh air in a somewhat stale computing environment. Not only was it small and cheap, but it was easy to get it to interact with the physical world—a real boon for anybody interested in designing physical systems. So when I heard about its release, I signed up like about a trillion other hobbyists/hackers/engineers and waited impatiently for mine to be delivered. Then I started building stuff with it and never looked back.

If you bought a Pi but aren’t sure how to get started with it, this book is for you.If you bought a Pi but aren’t sure what to do with it, this book is for you.If you’re considering buying a Pi but haven’t yet because you keep thinking, “Why should I? It’s not like I can do

anything cool with it, right?”, then this book is definitely for you.This book isn’t meant to be a textbook on Python, nor is it an exhaustive exploration of the Raspberry Pi and

everything it can do. But it is meant to be a fun, getting-started guide to this neat little computer. I hope that after you work your way through the book, you’ll get a sense of all the things that are possible with the Pi when you combine it with a little ingenuity and creativity on your part.

If you want to work through the projects here in order, feel free. If you’d rather skip around, doing those that interest you, you’re welcome to do that as well. Along the way, I hope you’ll develop a familiarity with both Python and the Pi that will enable you to continue on, building projects as you go, and perhaps inspiring others along the way. Above all, I hope you enjoy the book and its projects. It was truly a blast to write.

Happy computing!