Raspberry Pi A User Manual - 172

Other manuals for A: Getting Started Manual

Summary of A

  • Page 3: Raspberry Pi

    Raspberry pi ® user guide 2nd edition.

  • Page 5: Raspberry Pi

    Raspberry pi ® user guide 2nd edition eben upton and gareth halfacree.

  • Page 6

    This edition first published 2014 © 2014 eben upton and gareth halfacree registered office john wiley & sons ltd., the atrium, southern gate, chichester, west sussex, po19 8sq, united kingdom for details of our global editorial offices, for customer services and for information about how to apply fo...

  • Page 7

    Editorial and production vp consumer and technology publishing director michelle leete associate director–book content management martin tribe associate publisher chris webb executive commissioning editor craig smith senior project editor sara shlaer copy editor kathryn duggan, grace fairley technic...

  • Page 8

    For liz, who made it all possible. —eben for my father, the enthusiastic past, and my daughter, the exciting future. —gareth.

  • Page 9: About The Authors

    About the authors eben upton is a founder and trustee of the raspberry pi foundation, and serves as its executive director. He is responsible for the overall software and hardware architecture of the raspberry pi, and for the foundation’s relationships with its key suppliers and custom- ers. In an e...

  • Page 11: Table of Contents

    Table of contents introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 programming is fun! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 a bit of history . . . . . . . . . . . . . . . . ...

  • Page 12

    X r a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n chapter 3 linux system administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 linux: an overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 l...

  • Page 13

    Xi t a b l e o f c o n t e n t s chapter 6 the raspberry pi software configuration tool . . . . . . . . . . . . . . . . 93 running the tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 the setup options screen . . . . . . . ...

  • Page 14: Xii

    Xii r a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n chapter 9 the pi as a productivity machine . . . . . . . . . . . . . . . . . . . . . . . . . 135 using cloud-based apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136 using l...

  • Page 15: Xiii

    Xiii t a b l e o f c o n t e n t s part iv: hardware hacking chapter 13 learning to hack hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 electronic equipment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 reading ...

  • Page 16: Xiv

    Xiv r a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n chapter 16 add-on boards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ciseco slice of pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

  • Page 17: Programming Is Fun!

    Introduction “children today are digital natives”, said a man i got talking to at a fireworks party last year. “i don’t understand why you’re making this thing. My kids know more about set- ting up our pc than i do.” i asked him if they could program, to which he replied: “why would they want to? Th...

  • Page 18

    R a s p b e r r y p i u s e r g u i d e , s e c o n d e d i t i o n 2 it’s not even as if we’re coming from a position where children don’t want to get involved in the computer industry. A big kick up the backside came a few years ago, when we were mov- ing quite slowly on the raspberry pi project. ...

  • Page 19: A Bit of History

    I n t r o d u c t i o n 3 too many of the computing devices a child will interact with daily are so locked down that they can’t be used creatively as a tool—even though computing is a creative subject. Try using your iphone to act as the brains of a robot, or getting your ps3 to play a game you’ve w...

  • Page 20: Why “Raspberry Pi”?

    R a s p b e r r y p i u s e r g u i d e , s e c o n d e d i t i o n 4 in their spare time. So the initial idea behind the raspberry pi was a very parochial one with a very tight (and pretty unambitious) focus: i wanted to make a tool to get the small number of applicants to this small university cou...

  • Page 21

    I n t r o d u c t i o n 5 in my new role as a chip architect at broadcom, a big semiconductor company, i had access to inexpensive but high-performing hardware produced by the company with the intention of being used in very high-end mobile phones—the sort with the hd video and the 14-mega- pixel ca...

  • Page 22

    R a s p b e r r y p i u s e r g u i d e , s e c o n d e d i t i o n 6 the next morning, rory’s video had gone viral, and i realised that we had accidentally prom- ised the world that we’d make everybody a $25 computer. While rory went off to write another blog post on exactly what it is that makes a...

  • Page 23: Our Community

    I n t r o d u c t i o n 7 we blog something interesting about the device at www.Raspberrypi.Org at least once every day. Come and join in the conversation! There were 100,000 people on our mailing list wanting a raspberry pi—and they all put an order in on day one! Not surprisingly, this brought up ...

  • Page 24

    R a s p b e r r y p i u s e r g u i d e , s e c o n d e d i t i o n 8 hand over to somebody else who already had the infrastructure and capital to do that, so we got in touch with element14 and rs components, both uk microelectronics suppliers with worldwide businesses, and contracted with them to d...

  • Page 25

    I n t r o d u c t i o n 9 we think physical computing—building systems using sensors, motors, lights and micro- controllers—is something that gets overlooked in favour of pure software projects in a lot of instances, and it’s a shame, because physical computing is massive fun. To the extent that the...

  • Page 26

    R a s p b e r r y p i u s e r g u i d e , s e c o n d e d i t i o n 10

  • Page 27: Connecting The

    Part i connecting the board chapter 1 meet the raspberry pi chapter 2 getting started with raspberry pi chapter 3 linux system administration chapter 4 troubleshooting chapter 5 network configuration chapter 6 the raspberry pi software configuration tool chapter 7 advanced raspberry pi configuration.

  • Page 29: Chapter

    Chapter 1 meet the raspberry pi.

  • Page 30: A Trip Around The Board

    P a r t i c o n n e c t i n g t h e b o a r d 14 your raspberry pi board is a miniature marvel, packing considerable computing power into a footprint no larger than a credit card. It’s capable of some amazing feats, but there are a few things you need to know before you plunge head-first into the br...

  • Page 31

    C h a p t e r 1 m e e t t h e r a s p b e r r y p i 15 in the centre of all raspberry pi boards is a square semiconductor, more commonly known as an integrated circuit or chip. This is the broadcom bcm2835 system-on-chip (soc) module, which provides the pi with its general-purpose processing, graphi...

  • Page 32: Model A

    P a r t i c o n n e c t i n g t h e b o a r d 16 is lost. In chapter 2, “getting started with the raspberry pi”, you’ll learn how to prepare an sd card for use with the pi, including installing an operating system in a process known as flashing. The right-hand edge of the pi will have different conn...

  • Page 33: Model B

    C h a p t e r 1 m e e t t h e r a s p b e r r y p i 17 the model a has only a single port on its right-hand edge, a universal serial bus (usb) port. This is the same type of port found on desktop and laptop computers, and allows the pi to be connected to almost any usb-compatible peripheral. Most co...

  • Page 34: Revision 1

    P a r t i c o n n e c t i n g t h e b o a r d 18 the model b has two usb ports on the right-hand edge of the board, providing connectivity for a keyboard and mouse, and still leaving two spare ports for additional accessories such as external storage devices or hardware interfaces. Additionally, it ...

  • Page 35: Arm Versus X86

    C h a p t e r 1 m e e t t h e r a s p b e r r y p i 19 arm versus x86 the processor at the heart of the raspberry pi system is the broadcom bcm2835 soc multi- media processor. This means that the vast majority of the system’s components, including its central and graphics processing units along with...

  • Page 36: Windows Versus Linux

    P a r t i c o n n e c t i n g t h e b o a r d 20 windows versus linux another important difference between the raspberry pi and your desktop or laptop, other than the size and price, is the operating system—the software that allows you to control the computer. The majority of desktop and laptop comp...

  • Page 37: Chapter

    Chapter 2 getting started with the raspberry pi.

  • Page 38: Your Mileage May Vary

    P a r t i c o n n e c t i n g t h e b o a r d 22 now that you have a basic understanding of how the raspberry pi differs from other computing devices, it’s time to get started. If you’ve just received your pi, take it out of its protective anti-static bag and place it on a flat, non-conductive surfa...

  • Page 39: Hdmi Video

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 23 figure 2-1: the yellow rca phono connector, for composite video output hdmi video a better quality picture can be obtained using the high definition multimedia interface (hdmi) connector, the only port found on the bo...

  • Page 40: Dsi Video

    P a r t i c o n n e c t i n g t h e b o a r d 24 figure 2-2: the silver hdmi connector, for high-definition video output dsi video the final video output on the pi can be found above the sd card slot on the top of the printed circuit board—it’s a small ribbon connector protected by a layer of plasti...

  • Page 41

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 25 if you’re using the pi with a dvi-d monitor via an adapter or cable, audio will not be included. This highlights the main difference between hdmi and dvi: while hdmi can carry audio signals, dvi cannot and is instead ...

  • Page 42

    P a r t i c o n n e c t i n g t h e b o a r d 26 can connect the keyboard and mouse directly to these ports. If you’re using a model a, you’ll need to purchase an external usb hub in order to connect two usb devices simultaneously. Figure 2-3: the raspberry pi model b’s two usb ports a usb hub is a ...

  • Page 43: A Note On Storage

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 27 installing noobs on an sd card the raspberry pi foundation supplies a software tool for the pi known as new out-of-box software, or noobs. This tool, is intended to make it as easy as possible to get started with usin...

  • Page 44: Connecting External Storage

    P a r t i c o n n e c t i n g t h e b o a r d 28 enough to open it on most operating systems; if not, download an archive utility like 7zip ( www.7-zip.Org ) and try again. When you have opened the file, use your archive software’s extract or copy function to transfer the files from within the archi...

  • Page 45: Connecting The Network

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 29 mounted—a process you will learn in chapter 3, “linux system administration”. For now, it’s enough to connect the drives to the pi in readiness. Figure 2-5: two usb mass storage devices: a pen drive and an external ha...

  • Page 46: Wired Networking

    P a r t i c o n n e c t i n g t h e b o a r d 30 wired networking to get your raspberry pi on the network, you’ll need to connect an rj45 ethernet patch cable between the pi and a switch, router or hub. If you don’t have a router or hub, you can get your desktop or laptop talking to the pi by connec...

  • Page 47: Wireless Networking

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 31 if you do connect the pi directly to a pc or laptop, you won’t be able to connect out onto the internet by default. To do so, you’ll need to configure your pc to bridge the wired ethernet port and another (typically w...

  • Page 48: Connecting Power

    P a r t i c o n n e c t i n g t h e b o a r d 32 using such a device, the pi can connect to a wide range of wireless networks, including those running on the latest 802.11n high-speed standard. Before purchasing a usb wireless adapter, check the following: ❍ ensure that linux is listed as a supporte...

  • Page 49: Installing Using Noobs

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 33 required for the pi to work properly. Connect the micro-usb power supply only when you are ready to start using the pi. With no power button on the device, it will start working the instant power is connected. To safe...

  • Page 50

    P a r t i c o n n e c t i n g t h e b o a r d 34 figure 2-8: the noobs operating system menu tip if you see only a blank screen, but the pi’s act and pwr lights are on, you may need to choose a different display mode. Press 1 on the keyboard for standard hdmi mode, 2 for a ‘safe’ mode with a lower r...

  • Page 51: Installing Manually

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 35 to begin the installation process, click the install icon at the top-left of the menu and, when asked, confirm that the sd card can be overwritten when asked. As with installing noobs itself, this process can take a l...

  • Page 52

    P a r t i c o n n e c t i n g t h e b o a r d 36 first, you’ll need to decide which linux distribution you would like to use with your raspberry pi. Each has its advantages and disadvantages. Don’t worry if you change your mind later and want to try a different version of linux: an sd card can be fl...

  • Page 53

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 37 flashing from linux if your current pc is running a variant of linux already, you can use the dd command to write the contents of the image file out to the sd card. This is a text-interface program operated from the c...

  • Page 54

    P a r t i c o n n e c t i n g t h e b o a r d 38 flashing from os x if your current computer is a mac running apple os x, you’ll be pleased to hear that things are just as simple as with linux. Thanks to a similar ancestry, os x and linux both contain the dd utility, which you can use to flash the s...

  • Page 55

    C h a p t e r 2 g e t t i n g s t a r t e d w i t h t h e r a s p b e r r y p i 39 4. Browse to the imagefilename.Img file you extracted from the distribution archive, replacing imagefilename.Img with the actual name of the file extracted from the zip archive, and then click the open button. 5. Sele...

  • Page 57: Chapter

    Chapter 3 linux system administration.

  • Page 58: Linux: An Overview

    P a r t i c o n n e c t i n g t h e b o a r d 42 the majority of modern linux distributions are user-friendly, with a graphical user interface (gui) that provides an easy way to perform common tasks. It is, however, quite dif- ferent to both windows and os x, so if you’re going to get the most out o...

  • Page 59

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 43 table 3-1 the quick linux glossary term/concept definition bash the most popular shell choice, used in the majority of linux distributions. Bootloader software responsible for loading the linux kernel. The most common is grub. Cons...

  • Page 60: Linux Basics

    P a r t i c o n n e c t i n g t h e b o a r d 44 linux basics although there are hundreds of different linux distributions available, they all share a com- mon set of tools known as commands. These tools, which are operated via the terminal, are analogous to similar tools on windows and os x. To get...

  • Page 61: Introducing Raspbian

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 45 ❍ rmdir—by itself, rm cannot usually remove directories. As a result, rmdir is provided to delete directories once they have been emptied of files by rm . ❍ mkdir—the opposite of rmdir , the mkdir command creates new directories. F...

  • Page 62

    P a r t i c o n n e c t i n g t h e b o a r d 46 figure 3-1: the lxde desktop, as loaded on the raspbian distribution on a raspberry pi the following lists describe the software packages, grouped by category. There is also a cate- gory not covered here, dubbed “other”, into which numerous system too...

  • Page 63

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 47 education ❍ scratch—a graphical programming language aimed at young children. You’ll learn more about scratch and its capabilities in chapter 11, “an introduction to scratch”. ❍ squeak—the platform on which scratch runs. You will r...

  • Page 64: Finding Help

    P a r t i c o n n e c t i n g t h e b o a r d 48 preferences ❍ customise look and feel—a toolkit for adjusting the appearance of the gui, includ- ing the style and colour of windows. ❍ desktop session settings—a tool for changing how the system works when the user is logged in, including what progra...

  • Page 65: Alternatives to Raspbian

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 49 about raspbian’s parent, debian raspbian is based on one of the original linux distributions, debian. Named after its creator and his girlfriend—ian and deb—debian is a popular distribution in its own right. It is common in the wor...

  • Page 66

    P a r t i c o n n e c t i n g t h e b o a r d 50 using external storage devices the pi’s sd card, which stores all the various pi files and directories, isn’t very big. The largest available sd card at the time of writing is 256 gb, which is tiny compared to the 4,000 gb (4 tb) available from the la...

  • Page 67: Creating A New User Account

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 51 1. Connect the usb storage device to the pi, either directly or through a connected usb hub. 2. Type sudo fdisk -l to get a list of drives connected to the pi, and find the usb storage device by size. Note the device name: /dev/sdx...

  • Page 68: Users and Groups

    P a r t i c o n n e c t i n g t h e b o a r d 52 2. Type the following as a single line with no spaces after any of the commas: sudo useradd -m -g adm,dialout,cdrom,audio,plugdev,users, ↵ lpadmin,sambashare,vchiq,powerdev username this creates a new, blank user account. 3. To set a password on the n...

  • Page 69: Logical Layout

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 53 logical layout the way linux deals with drives, files, folders and devices is somewhat different to other operating systems. Instead of having multiple drives labelled with a letter, everything appears as a branch beneath what is k...

  • Page 70: Physical Layout

    P a r t i c o n n e c t i n g t h e b o a r d 54 ❍ etc—this stores miscellaneous configuration files, including the list of users and their encrypted passwords. ❍ home—each user gets a subdirectory beneath this directory to store all their personal files. ❍ lib—this is a storage space for libraries,...

  • Page 71

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 55 the second partition is far larger and formatted as ext4, a native linux file system designed for high-speed access and data safety. This partition contains the main chunk of the distribu- tion. All the programs, the desktop, the u...

  • Page 72

    P a r t i c o n n e c t i n g t h e b o a r d 56 software on the pi store is split into categories, ranging from games to tutorials and even media like videos, books and magazines. Clicking on a category across the top will show the software from that category in a grid, which can be scrolled using ...

  • Page 73

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 57 all software you install through the pi store is placed in the my library section (see figure 3-6). This provides an easy way to uninstall software, or to install software that you have previously used but since removed. Downloads ...

  • Page 74: Other Distributions

    P a r t i c o n n e c t i n g t h e b o a r d 58 although apt is designed to be operated from the command line, it’s very user-friendly and easy to learn. There are guis for apt , such as the popular synaptic package manager, but they often struggle to run on the pi due to the lack of memory. As a r...

  • Page 75: Installing Software

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 59 for example, to find a game to play, you can type the following command: apt-cache search game that tells apt-cache to search its list of available software for anything that has the word “game” in its title or description. For com...

  • Page 76

    P a r t i c o n n e c t i n g t h e b o a r d 60 it affects all users of the raspberry pi. As a result, the commands will need to be prefaced with sudo to tell the operating system that it should be run as the root user. For example, to install the package nethack-console (a console-based randomly g...

  • Page 77: Uninstalling Software

    C h a p t e r 3 l i n u x s y s t e m a d m i n i s t r a t i o n 61 uninstalling software if you decide you no longer want a piece of software, apt-get also includes a remove com- mand that cleanly uninstalls the package along with any dependencies that are no longer required. When you’re using a s...

  • Page 78: Shutting The Pi Down Safely

    P a r t i c o n n e c t i n g t h e b o a r d 62 if the package is already installed, apt will treat it as an in-place upgrade. If you’re already running the latest version available, apt will simply tell you it cannot upgrade the software, and will then exit. Tip for more information on package man...

  • Page 79: Chapter

    Chapter 4 troubleshooting.

  • Page 80

    P a r t i c o n n e c t i n g t h e b o a r d 64 sometimes, things don’t go entirely smoothly. The more complex the device, the more complex the problems that can occur—and the pi is an extremely complex device indeed. Thankfully, many of the most common problems are straightforward to diagnose and ...

  • Page 81: Power Diagnostics

    C h a p t e r 4 t r o u b l e s h o o t i n g 65 the issue of compatibility, sadly, is harder to diagnose. While the overwhelming majority of keyboards work just fine with the pi, a small number exhibit strange symptoms. These range from intermittent response, the repeating-letter syndrome or even c...

  • Page 82

    P a r t i c o n n e c t i n g t h e b o a r d 66 to use the voltage test points, you’ll need a voltmeter or multimeter with direct current (dc) voltage measuring capabilities. If your meter has multiple inputs for different voltages, use an appropriate setting. Warning avoid touching the test probes...

  • Page 83: Display Diagnostics

    C h a p t e r 4 t r o u b l e s h o o t i n g 67 the reading on the voltmeter should be somewhere between 4.8 v and 5 v. If it’s lower than 4.8 v, this indicates that the pi is not being provided with enough power. Try swapping the usb adapter for a different model, and check that the label says it ...

  • Page 84: Boot Diagnostics

    P a r t i c o n n e c t i n g t h e b o a r d 68 boot diagnostics the most common cause for a pi to fail to boot is a problem with the sd card. Unlike a desk- top or laptop computer, the pi relies on files stored on the sd card for everything. If pi can’t talk to the card, it won’t display anything ...

  • Page 85

    C h a p t e r 4 t r o u b l e s h o o t i n g 69 called in this manner, ifconfig provides information on all the network ports it can find (see figure 4-2). For the standard raspberry pi model b, there are two ports: the physical ethernet port on the right side of the board, and a virtual loopback i...

  • Page 86

    P a r t i c o n n e c t i n g t h e b o a r d 70 ❍ bcast—the broadcast address for the network to which the pi is connected. Any traffic sent to this address will be received by every device on the network. ❍ mask—the network mask, which controls the maximum size of the network to which the pi is co...

  • Page 87: The Emergency Kernel

    C h a p t e r 4 t r o u b l e s h o o t i n g 71 you can test the networking by using the ping command, which sends data to a remote computer and waits for a response. If everything’s working, you should see the same response as shown in figure 4-3. If not, you may need to manually configure your ne...

  • Page 88

    P a r t i c o n n e c t i n g t h e b o a r d 72 it’s highly unlikely that you’ll ever need to boot a pi using the emergency kernel, but it’s worth learning how to do so just in case. This is especially important if you’re upgrading your kernel or are using a new and potentially poorly tested distri...

  • Page 89: Chapter

    Chapter 5 network configuration.

  • Page 90: Wired Networking

    P a r t i c o n n e c t i n g t h e b o a r d 74 for most users, configuring the raspberry pi’s network is as easy as plugging a cable into the model b’s ethernet port—or a usb ethernet adapter, in the case of the model a. For others, however, the network requires manual configuration. If you know t...

  • Page 91

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 75 figure 5-1: editing /etc/ network/ interfaces with nano make sure that you press the tab key at the start of each line, and don’t actually type [tab]. The x characters in the configuration lines represent network addresses you’ll need to ent...

  • Page 92

    P a r t i c o n n e c t i n g t h e b o a r d 76 if you need to return to automatic settings via dhcp, you need to edit the interfaces file again and delete the address , netmask and gateway settings. Replace static with dhcp at the end of the iface line, and then restart the networking service agai...

  • Page 93: Wireless Networking

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 77 then test the settings by either opening a web browser or using the following ping com- mand (see figure 5-2): ping -c 1 www.Raspberrypi.Org figure 5-2: a successful test of networking on the raspberry pi model b wireless networking although...

  • Page 94: Installing Firmware

    P a r t i c o n n e c t i n g t h e b o a r d 78 before you start to set up the wireless interface, you’ll need to know the service set identifier (ssid)—also known as the network name—of the wireless router to which you want to con- nect, along with the type of encryption in use and the password re...

  • Page 95

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 79 this will print out the entire kernel ring buffer, which will contain all messages output by the kernel since the pi was switched on. If the pi has been running a while, that can be a lot of text. To locate error messages particular to the w...

  • Page 96

    P a r t i c o n n e c t i n g t h e b o a r d 80 the important part of this output is the line that reads manufacturer . In the case of the example zyxel nwd2105, this reads ralink , which is the company that makes the actual chip found inside zyxel usb wireless adapter. It’s this company’s firmware...

  • Page 97

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 81 the firmware for the example zyxel wireless adapter is provided by the firmware-ralink package in this list. This package can be installed using apt-get , but only while the pi is connected to the internet through its wired ethernet port or ...

  • Page 98

    P a r t i c o n n e c t i n g t h e b o a r d 82 connecting to a wireless network via wpa_gui the simplest way to connect to a wireless network from the raspberry pi is to use the wpa_gui tool. This provides a graphical user interface for software that would otherwise require the use of the terminal...

  • Page 99

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 83 finding a wireless network is a process known as scanning, which in wpa_gui is activated by clicking the scan button at the bottom-right of the window. This will pop up a second win- dow, showing the results of the scan (see figure 5-5). Loo...

  • Page 100

    P a r t i c o n n e c t i n g t h e b o a r d 84 figure 5-6: adding a network to wpa_gui if your network includes a wi-fi protected setup (wps) option, you can click the wps button at the bottom-left of the window to perform a one-click setup. Simply press the wps button on your router or access poi...

  • Page 101

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 85 figure 5-7: connected to a wireless network via wpa_gui if you need to connect to a different network in the future, simply start the process again from the beginning. Connecting to a new network won’t cause wpa_gui to forget the old network...

  • Page 102

    P a r t i c o n n e c t i n g t h e b o a r d 86 this command will return a list of all the wireless networks reachable from the pi, together with their details (see figure 5-8). If you receive an error message at this point—in particu- lar, one that claims the network or interface is down—check tha...

  • Page 103

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 87 ❍ standard—the ieee 802.11 wireless standards have a variety of different types, dis- tinguished by a letter suffix. This section lists the standards supported by the usb wireless adapter. For the example adapter, this reads ieee 802.11bgn f...

  • Page 104

    P a r t i c o n n e c t i n g t h e b o a r d 88 figure 5-9: the output of iwconfig when not connected to a wireless network to connect the pi to a wireless network, you will need to add some lines into the /etc/net- work/interfaces file. (for full details on how this file is laid out, see the “wire...

  • Page 105

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 89 figure 5-10: editing the interfaces file for wireless network access once the entry is in place, save the file by pressing ctrl + o and then quit nano with ctrl + x. Tip the device id of wlan0 is correct if this is the first wireless device ...

  • Page 106

    P a r t i c o n n e c t i n g t h e b o a r d 90 the wpasupplicant program stores its configuration in a file called wpa.Conf , located in the /etc directory. To begin configuring the pi for wireless access, first open a new blank file for editing by typing the following: sudo nano /etc/wpa.Conf ent...

  • Page 107

    C h a p t e r 5 n e t w o r k c o n f i g u r a t i o n 91 wpa/wpa2 encryption if your wireless network uses wpa or wpa2 encryption, finish the wpa.Conf file as follows: [tab] key_mgmt=wpa-psk [tab] psk=”your_wpa_key” } replace your_wpa_key with the pass phrase for your wireless network’s encryption...

  • Page 108

    P a r t i c o n n e c t i n g t h e b o a r d 92 to make sure that the network is operational, unplug the pi’s ethernet cable (if attached) and type the following: ping -c 1 www.Raspberrypi.Org tip if you start having problems with your pi following the installation of a usb wireless adapter, it cou...

  • Page 109: Chapter

    Chapter 6 the raspberry pi software configuration tool.

  • Page 110: Running The Tool

    P a r t i c o n n e c t i n g t h e b o a r d 94 the raspberry pi is designed to be as controllable as possible, through the editing of configuration files found in the /boot directory of the sd card. For beginners, these files can seem dauntingly complex—although, with a little time, they soon give...

  • Page 111: The Setup Options Screen

    C h a p t e r 6 t h e r a s p b e r r y p i s o f t w a r e c o n f i g u r a t i o n t o o l 95 the enter key is used to activate an option when it is highlighted by the red band. The default action is always select, so if you’ve highlighted an option in the menu, there’s no need to press the right...

  • Page 112: 2 Change User Password

    P a r t i c o n n e c t i n g t h e b o a r d 96 when the pi reboots, it will perform the remainder of the filesystem expansion. On larger or slower cards, this can take a little time. It’s important that the pi is not disturbed during this process because if the power is lost, the pi’s filesystem c...

  • Page 113

    C h a p t e r 6 t h e r a s p b e r r y p i s o f t w a r e c o n f i g u r a t i o n t o o l 97 4 internationalisation options this menu option provides a way for users in countries other than the uk to configure the pi for their needs (see figure 6-2). By default, raspbian sets itself to use uk en...

  • Page 114

    P a r t i c o n n e c t i n g t h e b o a r d 98 when you have chosen your locales, press enter to confirm the changes and select your default. This allows you to have multiple locales installed at the same time without difficulty, although be aware that each locale will take up space on the pi’s sd...

  • Page 115: 5 Enable Camera

    C h a p t e r 6 t h e r a s p b e r r y p i s o f t w a r e c o n f i g u r a t i o n t o o l 99 5 enable camera this option should be used only if you have a raspberry pi camera module (see chapter 15, “the raspberry pi camera module”) installed in your system. Choose the enable camera option with ...

  • Page 116: 100

    P a r t i c o n n e c t i n g t h e b o a r d 100 7 overclock overclocking refers to the process of running a device at a speed higher than its manufactur- ers intended. The raspberry pi’s bcm2835 processor can be run above its default speed of 700mhz to boost the performance of the system. Such per...

  • Page 117: 101

    C h a p t e r 6 t h e r a s p b e r r y p i s o f t w a r e c o n f i g u r a t i o n t o o l 101 if you want to try overclocking your pi, use the cursor keys to select an option from one of the pre-set overclocks that appear on the next screen: none, modest, medium, high and turbo. Almost all raspb...

  • Page 118: 102

    P a r t i c o n n e c t i n g t h e b o a r d 102 figure 6-4: the raspi- config advanced options screen a1 overscan many tv sets feature overscan, which means the visible picture area is slightly smaller than the transmitted picture. In broadcast tv, this is often used to hide additional data such a...

  • Page 119: 103

    C h a p t e r 6 t h e r a s p b e r r y p i s o f t w a r e c o n f i g u r a t i o n t o o l 103 command. Hostnames should be unique, which can cause a problem if you have more than one raspberry pi on your network. You can change a pi’s hostname at any time using the hostname option of raspi-confi...

  • Page 120: 104

    P a r t i c o n n e c t i n g t h e b o a r d 104 choosing an uneven value, such as 17mb, will not harm the pi but may result in a different value being used than the one selected. When you have chosen a memory split, press the enter key to confirm. To apply the setting, exit raspi-config by pressin...

  • Page 121: 105

    C h a p t e r 6 t h e r a s p b e r r y p i s o f t w a r e c o n f i g u r a t i o n t o o l 105 9 about raspi-config the final menu option available in raspi-config simply introduces the tool. Choosing this option with the cursor keys and pressing enter will launch a window explaining the purpose ...

  • Page 123: Chapter

    Chapter 7 advanced raspberry pi configuration.

  • Page 124: 108

    108 p a r t i c o n n e c t i n g t h e b o a r d owing to its origins in embedded computing, the bcm2835 chip at the heart of the raspberry pi doesn’t have anything like a pc’s basic input-output system (bios) menu where various low-level system settings can be configured. Instead, it relies on tex...

  • Page 125: 109

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 109 figure 7-1: the edit config button in noobs figure 7-2: editing configuration files in noobs.

  • Page 126: 110

    110 p a r t i c o n n e c t i n g t h e b o a r d hardware settings—config.Txt the pi’s hardware is controlled by settings contained in a file called config.Txt , which is located in the /boot directory (see figure 7-3). This file tells the pi how to set up its various inputs and outputs, and at wha...

  • Page 127: 111

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 111 modifying the display usually, the raspberry pi will detect the type of display that’s connected and alter its settings accordingly. Sometimes, however, this automatic detection doesn’t work. This is often the case w...

  • Page 128: 112

    112 p a r t i c o n n e c t i n g t h e b o a r d ❍ framebuffer_ignore_alpha—set to 1 , this value disables the alpha channel, which controls transparency in the console. Disabling the alpha channel is not normally required, but it may correct graphical corruption caused when setting framebuffer_ de...

  • Page 129: 113

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 113 ❍ hdmi_group—sets the hdmi group mode to cea or dmt. You should change this setting according to the display type you’re trying to connect, before using hdmi_mode to control the output resolution and frequency. The t...

  • Page 130: 114

    114 p a r t i c o n n e c t i n g t h e b o a r d for the changes to take effect, the pi must be restarted. If you find that your changes have disabled the pi’s video output on your monitor, simply insert the sd card into another com- puter and either modify the config.Txt file with new settings or ...

  • Page 131: 115

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 115 the bcm2835 multimedia processor at the heart of the pi is a system-on-chip (soc) design split into two main parts: the graphics processor (gpu) and the central processor (cpu). Simply put, the cpu handles all the da...

  • Page 132: 116

    116 p a r t i c o n n e c t i n g t h e b o a r d • isp_freq—sets the clock frequency of the image sensor pipeline, for improving the capture rate of connected video hardware (such as a camera). The default speed is 250 mhz. • v3d_freq—sets the clock frequency of the gpu’s 3d rendering hardware, for...

  • Page 133: 117

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 117 if you want to eke a little more performance out of your pi, there is a way to potentially boost this upper limit: a process known as overvoltage or overvolting. The pi’s bcm2835 system-on- chip processor and the ass...

  • Page 134: 118

    118 p a r t i c o n n e c t i n g t h e b o a r d as an example, the following lines entered into config.Txt will give the bcm2835 a small boost of 0.05 v to 1.25 v and the memory chip a bigger boost of 0.1 v to 1.3 v: over_voltage=2 over_voltage_sdram=4 as with other settings, deleting the lines fr...

  • Page 135: 119

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 119 enabling test mode this final option in config.Txt is one the overwhelming majority of pi users won’t need to touch, but it is included here for completeness: test mode. Used during production of the raspberry pi at ...

  • Page 136: 120

    120 p a r t i c o n n e c t i n g t h e b o a r d warning applications that do heavy graphics work, such as 3d games and high-definition video playback software, typically need 128 mb of memory for the gpu. Reducing this can result in a dramatic drop in performance. The raspberry pi camera module is...

  • Page 137: 121

    C h a p t e r 7 a d v a n c e d r a s p b e r r y p i c o n f i g u r a t i o n 121 in a linux-based desktop or laptop, these options are normally passed to the kernel by a tool known as a bootloader, which has its own configuration file. On the pi, the options are simply entered directly into cmdli...

  • Page 138: 122

    122 p a r t i c o n n e c t i n g t h e b o a r d finally, the rootwait parameter tells the kernel that it should not try to boot the system any further until the device containing the root file system is available. Without this option, the pi can get stuck as it begins to boot before the relatively...

  • Page 139: Building A

    Part ii building a media centre, productivity machine or web server chapter 8 the pi as a home theatre pc chapter 9 the pi as a productivity machine chapter 10 the pi as a web server.

  • Page 141: Chapter

    Chapter 8 the pi as a home theatre pc.

  • Page 142: 126

    126 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r one of the most popular tasks for a pi to carry out is that of a home theatre pc, or htpc. The broadcom bcm2835 at the pi’s heart is specifically designed as a multimedia power- house, origi...

  • Page 143: 127

    C h a p t e r 8 t h e p i a s a h o m e t h e a t r e p c 127 to get started, just enter the console—or a terminal window if you’re using a desktop environment—and type the following: mocp the standard mocp interface is split into two panes (see figure 8-1). The left pane is a file browser, which al...

  • Page 144: 128

    128 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r the most commonly used mocp flags are the following: ❍ -s—stop the current playback ❍ -g—pause playback, or resume playback if currently paused ❍ -f—skip to the next song in the directory or...

  • Page 145: 129

    C h a p t e r 8 t h e p i a s a h o m e t h e a t r e p c 129 when the raspbmc installer has finished, insert the sd card into the pi and re-connect the power supply—but make sure the ethernet cable is connected as well, because raspbmc needs to download some data from the internet when it first loa...

  • Page 146: 130

    130 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r streaming capabilities to the device, including access to various tv channels and internet- only streaming services. After clicking add-ons, choose get more to access the full list of compat...

  • Page 147: 131

    C h a p t e r 8 t h e p i a s a h o m e t h e a t r e p c 131 figure 8-4: a list of videos available through the collegehumor xbmc add-on similar add-ons are available under the music and video menus, and operate in the same way. Using these add-ons, you can view picture content and stream audio con...

  • Page 148: 132

    132 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r to configure xbmc to connect to a server on your home network, choose the media type— video, music or pictures—and click the add source option. In the window that appears, choose browse to r...

  • Page 149: 133

    C h a p t e r 8 t h e p i a s a h o m e t h e a t r e p c 133 figure 8-6: adding a upnp music source to xbmc you can also use the same menu to add an external hard drive as a source to xbmc by select- ing its entry in the initial list. Most external drives will appear automatically, and do not need ...

  • Page 150: 134

    134 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r figure 8-7: configuring raspbmc in the settings menu the second tab, marked xbmc, allows you to install the nightly build of the xbmc software. The nightly build is so called because it is c...

  • Page 151: Chapter

    Chapter 9 the pi as a productivity machine.

  • Page 152: 136

    136 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r the flexibility of the raspberry pi makes it a good choice as a low-power general- purpose desktop computer. Although it will never reach the same levels of performance as a standard desktop...

  • Page 153: 137

    C h a p t e r 9 t h e p i a s a p r o d u c t i v i t y m a c h i n e 137 formats. They are also inaccessible when no internet connection is available, making them a poor choice for users with unreliable connections. If you think that the improved performance and saved space on your pi’s sd card is ...

  • Page 154: 138

    138 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r figure 9-1: google drive running in chromium on the raspberry pi to install the chromium browser under raspbian, open a terminal and type the following: sudo apt-get install chromium-browser...

  • Page 155: 139

    C h a p t e r 9 t h e p i a s a p r o d u c t i v i t y m a c h i n e 139 with chromium installed, using a cloud-based office suite is as simple as visiting the site, signing up for an account—providing your credit card details in the case of premium services like microsoft office 365—and logging in...

  • Page 156: 140

    140 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r when installed, libreoffice will appear as a series of entries in the applications menu in the pi’s desktop environment. These entries are as follows: ❍ libreoffice—the main application, whi...

  • Page 157: 141

    C h a p t e r 9 t h e p i a s a p r o d u c t i v i t y m a c h i n e 141 the pi in libreoffice with users of older software, remember to change the format to ensure that everyone can open the files. Alternatively, you could convince them to install libreoffice themselves, which is available free fo...

  • Page 158: 142

    142 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r the gimp is not installed by default in most raspberry pi distributions, so you’ll have to con- nect your pi to the internet and install it through the package management system (see chapter...

  • Page 159: 143

    C h a p t e r 9 t h e p i a s a p r o d u c t i v i t y m a c h i n e 143 figure 9-4: exporting a file from the gimp.

  • Page 161: Chapter

    Chapter 10 the pi as a web server.

  • Page 162: 146

    146 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r although the pi is significantly less powerful than most devices you would find in a data centre, that doesn’t mean that it can’t act as a useful server in a home or business envi- ronment. ...

  • Page 163: 147

    C h a p t e r 1 0 t h e p i a s a w e b s e r v e r 147 details and other personally identifiable information. Also make sure you pick a password you can remember! You’ll be asked to confirm the password—to check for typing errors— and then the installation will continue. Figure 10-1: installing the...

  • Page 164: 148

    148 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r when the software installation has finished, both the mysql and apache servers—known in linux parlance as daemons—will be running in the background. To check that the server is working corre...

  • Page 165: 149

    C h a p t e r 1 0 t h e p i a s a w e b s e r v e r 149 this command creates a new file called phptest.Php in the /var/www directory. This file tells php to create an information page for diagnostic purposes. Visit this using either a browser on another computer by typing http://ipaddress/phptest.Ph...

  • Page 166: 150

    150 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r by default, files for the web server are stored in the /var/www folder, which is writeable only by the root user. To adjust where apache looks for its files—to move the website onto more cap...

  • Page 167: 151

    C h a p t e r 1 0 t h e p i a s a w e b s e r v e r 151 figure 10-5: installing wordpress on the raspberry pi this adds a new database into mysql, installed as part of the lamp stack, for wordpress to use. This database stores your user accounts, posts, comments and other details. Once this script h...

  • Page 168: 152

    152 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r fill in the form that loads in the web browser, picking a descriptive name for your site and setting a secure—but memorable—password for your user. Be sure to change the username field from ...

  • Page 169: 153

    C h a p t e r 1 0 t h e p i a s a w e b s e r v e r 153 replace ipaddress in this code with the ip address of your raspberry pi. If you’ve given the raspberry pi a hostname in dns, you can also create a configuration file for that hostname using the same command, but replacing ipaddress with the cho...

  • Page 170: 154

    154 pa r t ii b u i l di n g a m e di a c e n t r e , pro d u c t i v i t y m ac h i n e or w e b s e r v e r the wordpress software includes an automatic update feature, which ensures that your installation is running the latest version. Because of its popularity, wordpress is often the target of m...

  • Page 171: Programming

    Part iii programming with the raspberry pi chapter 11 an introduction to scratch chapter 12 an introduction to python.

  • Page 173: Chapter

    Chapter 11 an introduction to scratch.

  • Page 174: 158

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 158 so far in this book, you’ve learned a lot about how to use programs that other people have written on your raspberry pi. The chief goal of the raspberry pi project is to get people writ- ing their own programs, however—and n...

  • Page 175: 159

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 159 example 1: hello world when learning a new programming language, it’s traditional to start with a very basic pro- gram: one that displays a single line of text. This is typically referred to as a hello world program, and it’s the fi...

  • Page 176: 160

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 160 to get the user started, a new scratch project already includes a blank stage and a single sprite. What it lacks is a program, so clicking the green flag icon at the top-right of the win- dow achieves nothing, because scratc...

  • Page 177: 161

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 161 figure 11-3: a control block joined to a looks block in scratch this concept of connecting multiple bricks together is the heart of scratch. If you look at the control brick you just placed, you’ll see there’s no connecting hole at ...

  • Page 178: 162

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 162 figure 11-4: the hello world program executing in scratch example 2: animation and sound while hello world is a very traditional example, it’s not particularly interesting. It also fails to show off the true power of scratch...

  • Page 179: 163

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 163 ten steps isn’t a very large value, so click on the value 10 and change it to 30 . The block should then read move 30 steps . An animation of a cat moving to the right of the stage isn’t that interesting, however, so switch to the s...

  • Page 180: 164

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 164 this simple animation program can be extended in a variety of ways. Using the new sprite option just below the stage on the right side of the scratch window allows the programmer to add more sprites that can move and play so...

  • Page 181: 165

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 165 example 3: a simple game using scratch for simple animation is one thing, but the software also allows users to read inputs from the keyboard to introduce interactivity. By combining some simple animation controls to the previously ...

  • Page 182: 166

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 166 figure 11-7: using input blocks to control the motion of a sprite in scratch a dialogue box will appear asking you to pick a sprite: double-click on the things folder, and then double-click on the cheesy-puffs sprite (shown ...

  • Page 183: 167

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 167 figure 11-8: adding the cheesy-puffs sprite to the scratch game project clicking the shrink sprite button—or the grow sprite button, which does exactly the oppo- site—changes the mouse cursor into a duplicate of the button’s icon. U...

  • Page 184: 168

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 168 mark, and choose sprite1 (the cat sprite) from the list. This block will now be activated when the two sprites collide. Tip you can name a sprite by clicking on the box next to its image in the scripts pane and typing in a n...

  • Page 185: 169

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 169 from the looks block palette, drag a say hello! For 2 secs block into the centre of the if touching sprite1? Conditional. Change the text to read don’t eat me! , and then add a wait 1 secs control block, changing the value to 2 . Ad...

  • Page 186: 170

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 170 figure 11-10: the completed code for the simple scratch game’s cat sprite if you move the cat towards the cheesy-puffs with the right-arrow key on the keyboard, the game starts. When the cat reaches the cheesy-puffs, the dia...

  • Page 187: 171

    C h a p t e r 1 1 a n i n t r o d u c t i o n t o s c r a t c h 171 robotics and sensors in addition to purely software-based inputs and outputs, it’s also possible to tie scratch into external hardware using sensor boards and robotics systems. Thanks to its multithreaded nature and powerful inter-p...

  • Page 188: 172

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 172 further reading although this chapter serves as a brief introduction to scratch, it’s far from exhaustive. It’s also a little verbose for younger readers, who tend to learn more quickly when their lessons are accompanied by ...

  • Page 189: Chapter

    Chapter 12 an introduction to python.

  • Page 190: 174

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 174 the raspberry pi gets the first half of its name from a long-standing tradition of using fruit to name new computing systems—from classic microcomputers like the acorn, apricot and tangerine to more recognisably modern brand...

  • Page 191: 175

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 175 integrated development environment (ide) such as idle, which provides python-specific functionality that’s missing from a standard text editor, including syntax checking, debug- ging facilities and the ability to run your program with...

  • Page 192: 176

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 176 it’s good practice to start all python programs with a line known as a shebang, which gets its name from the # and ! Characters at the beginning of the line. This line tells the operating system where it should look for the ...

  • Page 193: 177

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 177 figure 12-2: syntax highlighting in idle before you run your program, save it as helloworld.Py using the file menu. If you’re using idle, the file will be given the extension .Py automatically. If you’re using a text edi- tor, be sure...

  • Page 194: 178

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 178 figure 12-3: running helloworld. Py in idle this tells the operating system to run python and then load the helloworld.Py file for execution. Unlike the python shell in idle, python will quit when it reaches the end of the f...

  • Page 195: 179

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 179 making python programs executable normally, the only way to run a python program is to tell the python software to open the file. With the shebang line at the top of the file, however, it’s possible to execute the file directly withou...

  • Page 196: 180

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 180 example 2: comments, inputs, variables and loops although the hello world program is a useful, gentle introduction to a language, it’s not ter- ribly exciting. By its nature, it covers only the basics and fails to introduce ...

  • Page 197: 181

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 181 this small line actually achieves quite a lot. The first part, username = , tells python to cre- ate a new variable—a location for storing a piece of information—called username . The equals symbol tells python that the variable shoul...

  • Page 198: 182

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 182 in its most basic form, a calculator takes two numbers as input and performs a mathematical operation on them. To make your calculator work, first take the two numbers from the user with the following lines: firstnumber = in...

  • Page 199: 183

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 183 when it prints integers and strings together. Finally, note that there is no division operation (which would be indicated with the / symbol). This is because the example calculator program uses integers, which can only be whole number...

  • Page 200: 184

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 184 figure 12-5: running calculator. Py in idle for more short programs that introduce important python concepts, visit the official python simple programs wiki page at http://wiki.Python.Org/moin/simpleprograms . Example 3: gam...

  • Page 201: 185

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 185 for other distributions, the pygame source files can be downloaded from the official pygame website at http://www.Pygame.Org/download.Shtml . Instructions for installation are provided on the same page. Starting a pygame program is th...

  • Page 202: 186

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 186 instruction and remember the three colour values for red, green and blue. To define the colours for this example program, type the following lines: redcolour = pygame.Color(255, 0, 0) blackcolour = pygame.Color(0, 0, 0) whit...

  • Page 203: 187

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 187 as with loops, the code for a function should be indented. Every line after the def instruc- tion should have four spaces at the start—if you’re using idle, these spaces will be inserted automatically, but if you’re using a text edito...

  • Page 204: 188

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 188 a quit message (which happens when the user presses the escape key). The two lines beneath that should be familiar from the gameover function: they tell pygame and python to close down and exit. The line beginning elif is us...

  • Page 205: 189

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 189 if changedirection == ‘left’ and not direction == ‘right’: direction = changedirection if changedirection == ‘up’ and not direction == ‘down’: direction = changedirection if changedirection == ‘down’ and not direction == ‘up’: directi...

  • Page 206: 190

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 190 this uses the insert instruction to insert a new value into the snakesegments list: the current position of the snake. Each time python reaches this line, it will increase the length of the snake’s body by one segment, and l...

  • Page 207: 191

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 191 python a place on the playing field to position the new raspberry. It’s important that the location of the raspberry is set randomly: this prevents the player from learning where the raspberry will appear next. Finally, the raspberrys...

  • Page 208: 192

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 192 the for statement runs through each of the snake segments’ locations, from the second list entry to the end of the list, and compares it to the current position of the snake’s head. It’s important to start the comparison at ...

  • Page 209: 193

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 193 section does. In addition to the functions used in raspberry snake, pygame provides lots of features not used in this program, including audio playback, sprite handling for better graph- ics and mouse control. The best place to learn ...

  • Page 210: 194

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 194 these are irc status codes, provided by the server to indicate when particular operations have completed. These are used by the program to know when it has received the required list of names from the irc server. Next, set u...

  • Page 211: 195

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 195 clear that the user is a program rather than a real person. Do the same with username , and fill in the realname variable with a descriptive message about whom the bot belongs to. The hostname and servername variables can be left set ...

  • Page 212: 196

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 196 sends the output through the network connection. In this case, the program is sending strings of text to the irc server and telling it to register the program using the nickname held in the nick variable and the user details...

  • Page 213: 197

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 197 the first line sets the lines variable to a full line of text from the receive buffer by using the split function to find end of line characters—signified by \r . These characters only occur at the end of a line, so when the buffer ...

  • Page 214: 198

    P a r t i i i p r o g r a m m i n g w i t h t h e r a s p b e r r y p i 198 this tells python that when the 366 response has been received, it should print out the now- complete list of names to the standard output before blanking the names list again. This last line— names = [] —is important: witho...

  • Page 215: 199

    C h a p t e r 1 2 a n i n t r o d u c t i o n t o p y t h o n 199 a full copy of the program listing for the irc user list is included in appendix a, “python recipes”, and on the raspberry pi user guide website at www.Wiley.Com/go/ raspberrypiuserguide2e . Downloading the source code from the websit...

  • Page 217: Hardware

    Part iv hardware hacking chapter 13 learning to hack hardware chapter 14 the gpio port chapter 15 the raspberry pi camera module chapter 16 add-on boards.

  • Page 219: Chapter

    Chapter 13 learning to hack hardware.

  • Page 220: 204

    P a r t i v h a r d w a r e h a c k i n g 204 in earlier chapters, you learned how the raspberry pi can be turned into a flexible plat- form for running a variety of software. In this, it’s not alone: any desktop or laptop can run the same software, and in many cases run it far faster than the pi’s ...

  • Page 221: 205

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 205 ❍ push-buttons—a very common input component, a push-button completes an electri- cal circuit when pushed. At the most basic level, a keyboard is little more than a collec- tion of push-buttons. If you’re designing a circuit to provid...

  • Page 222: 206

    P a r t i v h a r d w a r e h a c k i n g 206 ❍ work stand—commonly referred to as helping hands, these are weighted stands with clamps or clips that hold the item to be soldered in place. Some work stands include an integrated magnifying glass for delicate work, while the most expensive work stands...

  • Page 223: 207

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 207 figure 13-1: a four-band resistor and the decoding table for its colour code to read the example resistor, first take the two resistance bands starting from the left: these are coloured red and red. Red, on the table included in figur...

  • Page 224: 208

    P a r t i v h a r d w a r e h a c k i n g 208 there are also five-band resistors. You read these in the same way as a four-band resistor, except that the first three bands offer resistance figures, the fourth the multiplier, and the fifth the tolerance. Sourcing components if you haven’t dabbled in ...

  • Page 225: 209

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 209 offline sources you may find that you need a component immediately, and even next-day delivery would not be soon enough. Or you may only need a single resistor or a small length of wire and can’t justify a high delivery cost or minimu...

  • Page 226: 210

    P a r t i v h a r d w a r e h a c k i n g 210 many of these hobby shops sprang up in the wake of the arduino, an open-source project to create an educational-friendly microcontroller prototyping platform. With the raspberry pi appealing to much the same audience as the arduino—albeit for very differ...

  • Page 227: 211

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 211 figure 13-2: a sample breadboard circuit, with loose-fitting components this, among other reasons, is why the raspberry pi itself is built on a printed circuit board (pcb) rather than a breadboard—although the breadboard method was ce...

  • Page 228: 212

    P a r t i v h a r d w a r e h a c k i n g 212 figure 13-3: the copper tracks on the underside of a piece of stripboard stripboard is extremely easy to use, which makes it a great stepping-stone to custom circuit board design and manufacturing. However, you should be aware of the following before you...

  • Page 229: 213

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 213 there are also some tricks to handling stripboard that, if ignored, can make things harder than they have any reason to be: ❍ the copper tracks on the underside of stripboard are not usually coated in any way. As you touch the tracks,...

  • Page 230: 214

    P a r t i v h a r d w a r e h a c k i n g 214 when you’re starting a soldering project, make sure you have a clean, well-lit workspace. Also make sure the area is well ventilated. Solder fumes aren’t very healthy, and while they’re extremely unlikely to build up to dangerous levels in low-volume hob...

  • Page 231: 215

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 215 across your work area. Make sure you can move the iron freely before plugging it in. If the cable gets caught on something, you may end up burning yourself. Dampen your soldering sponge with water—it should be damp, but not dripping w...

  • Page 232: 216

    P a r t i v h a r d w a r e h a c k i n g 216 this process of tinning the tip protects it against damage and ensures that it will efficiently trans- fer heat onto the surfaces to be soldered. Failure to properly tin the iron’s tip is one of the most common causes of bad solder joints. You may need t...

  • Page 233: 217

    C h a p t e r 1 3 l e a r n i n g t o h a c k h a r d w a r e 217 figure 13-6: soldering a through-hole component into a printed circuit board when you’ve finished, remember to re-tin the tip of the iron. If you don’t, the tip may corrode in storage and need to be replaced significantly sooner than ...

  • Page 235: Chapter

    Chapter 14 the gpio port.

  • Page 236: 220

    P a r t i v h a r d w a r e h a c k i n g 220 the raspberry pi’s general-purpose input-output (gpio) port is located on the top-left of the printed circuit board, labelled p1. It’s a 26-pin port, fitted with two rows of 13 male 2.54 mm headers at the factory. The spacing of these headers is particul...

  • Page 237: 221

    C h a p t e r 1 4 t h e g p i o p o r t 221 figure 14-1: the raspberry pi’s gpio port and its pin definitions figure 14-2: the original raspberry pi model b revision 1 gpio pinout.

  • Page 238: 222

    P a r t i v h a r d w a r e h a c k i n g 222 although the pi’s gpio port provides a 5 v power supply, tapped from the incoming power on the micro-usb hub, on pin 2, the pi’s internal workings are based on 3.3 v logic. This means that the components on the pi work from a 3.3 v power supply. If you’r...

  • Page 239: 223

    C h a p t e r 1 4 t h e g p i o p o r t 223 i 2 c bus as the name suggests, the inter-integrated circuit (i²c) bus is designed to provide communica- tions between multiple integrated circuits (ics). In the case of the pi, one of those integrated circuits is the broadcom bcm2835 soc processor at the ...

  • Page 240: 224

    P a r t i v h a r d w a r e h a c k i n g 224 if true real-time operation is required for your project, the pi may be a bad choice. Instead, consider using a microcontroller platform such as the popular open-source arduino, or one of the msp430 family of microcontrollers from texas instruments. Both...

  • Page 241: 225

    C h a p t e r 1 4 t h e g p i o p o r t 225 to assemble the circuit, you’ll need a breadboard, two jumper wires, an led and an appropri- ate current-limiting resistor (as described in the “calculating limiting resistor values” side- bar). Although it’s possible to assemble the circuit without a brea...

  • Page 242: 226

    P a r t i v h a r d w a r e h a c k i n g 226 figure 14-3: a breadboard circuit for a simple led output remember that python is case-sensitive, so be sure to type rpi.Gpio exactly as it appears. To allow python to understand the concept of time (in other words, to make the led blink, rather than jus...

  • Page 243: 227

    C h a p t e r 1 4 t h e g p i o p o r t 227 the last line tells the gpio library that pin 11 on the raspberry pi’s gpio port should be set up as an output. If you were controlling additional devices, you could add more gpio.Setup lines into the project. For now, however, one will suffice. With the p...

  • Page 244: 228

    P a r t i v h a r d w a r e h a c k i n g 228 figure 14-4: the gpiooutput. Py program, being edited in nano , and waiting for its final line if things don’t work, don’t panic. First, check all your connections. The holes in a breadboard are quite small, and it’s easy to think you’ve inserted a compo...

  • Page 245: 229

    C h a p t e r 1 4 t h e g p i o p o r t 229 if you’ve already built the gpio output example, you can either disconnect that from your pi or leave it connected—this example uses different pins, so both can co-exist quite happily. If you do leave the previous example connected, make sure to use differ...

  • Page 246: 230

    P a r t i v h a r d w a r e h a c k i n g 230 figure 14-5: the example breadboard layout for a simple push- button input you don’t need to import the time library, because this example doesn’t need any timing instructions. Instead, you can get right to enabling board mode and setting up pin 12 as an...

  • Page 247: 231

    C h a p t e r 1 4 t h e g p i o p o r t 231 as with the previous example, the next step is to create an infinite loop that constantly checks the input pin to see if it’s been brought low (in other words, if it’s been pressed). Begin the loop with the following code line: while true: reading the stat...

  • Page 248: 232

    P a r t i v h a r d w a r e h a c k i n g 232 save the file as gpioinput.Py , and then execute it from the terminal with sudo python gpioinput.Py . At first, nothing will happen—but if you press the push-button switch, the program will print the message from line seven to the terminal (see figure 14...

  • Page 249: Chapter

    Chapter 15 the raspberry pi camera module.

  • Page 250: 234

    P a r t i v h a r d w a r e h a c k i n g 234 designed by engineers working for the raspberry pi foundation, the raspberry pi camera module is the most compact way of adding the ability to record still images and video to your project. Designed to connect to the camera serial interface (csi) of the ...

  • Page 251: 235

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 235 the pi has only a limited number of usb ports, however, and these are often used for more critical functions such as a keyboard, mouse or wireless network adapter. This is particularly important in the raspberry pi model a, wh...

  • Page 252: 236

    P a r t i v h a r d w a r e h a c k i n g 236 figure 15-2: the raspberry pi’s csi connector, pictured on a model a figure 15-3: raising the lugs of the csi connector, prior to inserting the ribbon cable.

  • Page 253: 237

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 237 insert the free end of the ribbon cable into the csi port, making sure that the silver contacts are facing towards the left of the pi and the blue portion of the cable to the right. Gently push the cable home, making sure not ...

  • Page 254: 238

    P a r t i v h a r d w a r e h a c k i n g 238 the final step in the camera’s physical installation is to remove the small, protective piece of plastic film found over the lens. This can simply be peeled off and discarded by pulling gently upwards on the tab that extends out from the lens. Although i...

  • Page 255: 239

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 239 tip if you can’t find a camera option in raspi-config, you may be running an outdated version. Use the sudo apt-get update && sudo apt-get upgrade command to update your software, then reboot the pi and try running raspi-confi...

  • Page 256: 240

    P a r t i v h a r d w a r e h a c k i n g 240 which are saved, by default, in the joint photographers expert group (jpeg) file format—the same format used by most digital cameras and smartphones. The software for the raspberry pi camera is run from the terminal or console. For best results, run the ...

  • Page 257: 241

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 241 tip sometimes the position of the camera module’s cable makes it difficult to place the camera the right way up. If your preview appears upside-down, use the -vf (vertical flip) option to correct the image. Just add the option...

  • Page 258: 242

    P a r t i v h a r d w a r e h a c k i n g 242 the filename after the output ( -o ) option can be anything you want it to be, with the preced- ing commands merely using example filenames. When writing the filename, always be sure to add the extension appropriate to your file type: .Jpg for jpeg image...

  • Page 259: 243

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 243 other options supported by raspistill are also supported by raspivid . For example, to set the width and height of the recorded video to 1,920x1,080—full hd—use the -w and -h options as in the following command: raspivid -w 19...

  • Page 260: 244

    P a r t i v h a r d w a r e h a c k i n g 244 a shell script is simply a collection of commands, each exactly as you would type them at the console or terminal. Each command is run in order, from top to bottom, but it’s also possible to use loops as in programming languages like scratch and python (...

  • Page 261: 245

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 245 although other shells are available, bash is the default for most modern linux distributions and offers a good mix of guaranteed compatibility and advanced features. Everything after this line will be run by the operating syst...

  • Page 262: 246

    P a r t i v h a r d w a r e h a c k i n g 246 after the final backtick is .Jpg . This completes the filename, telling the shell script to save the image file in the international date format with the extension of a jpeg file. If you cap- ture an image at exactly two in the afternoon on 17 th january...

  • Page 263: 247

    C h a p t e r 1 5 t h e r a s p b e r r y p i c a m e r a m o d u l e 247 figure 15-9: the complete timelapse. Sh script, in the nano text editor although this brings the script to a close, the loop used is known as an infinite loop: the instruction while true at the start tells the script to run th...

  • Page 264: 248

    P a r t i v h a r d w a r e h a c k i n g 248 figure 15-10: the timelapse. Sh script, running on a raspberry pi model b time-lapse photography is often used to compress a long or complex task into an entertain- ing video. Commercial time-lapse photography rigs, which can be extremely expensive, are ...

  • Page 265: Chapter

    Chapter 16 add-on boards.

  • Page 266: 250

    P a r t i v h a r d w a r e h a c k i n g 250 although you can use the raspberry pi’s general-purpose input-output (gpio) header directly, as you learned in chapter 13, “learning to hack hardware”, a much better idea is to use a specialist prototyping board. Prototyping boards are add-on boards desi...

  • Page 267: 251

    C h a p t e r 1 6 a d d - o n b o a r d s 251 figure 16-1: the ciseco slice of pi the primary advantages of using the low-cost slice of pi rather than just connecting directly to the pi’s gpio header are the labelling on the circuit board, which the gpio header lacks, and the use of female headers. ...

  • Page 268: 252

    P a r t i v h a r d w a r e h a c k i n g 252 figure 16-2: connecting the slice of pi to a breadboard the slice of pi also offers a small amount of protection for the pi. The use of female headers means it’s harder to accidentally short-circuit two pins, and the spacing of the headers—with the eight...

  • Page 269: 253

    C h a p t e r 1 6 a d d - o n b o a r d s 253 figure 16-3: the slice of pi, connected to the pi’s gpio port the disadvantage of the slice of pi lies in its simplicity. Its compact size means that it is unsuitable for the creation of larger or more complex prototypes, and while the header layout prov...

  • Page 270: 254

    P a r t i v h a r d w a r e h a c k i n g 254 the slice of pi is available from ciseco’s web shop at http://shop.Ciseco.Co.Uk/ slice-of-pi/ . Adafruit prototyping pi plate the prototyping pi plate by adafruit is based around the same general principle as ciseco’s slice of pi. The add-on is provided ...

  • Page 271: 255

    C h a p t e r 1 6 a d d - o n b o a r d s 255 the design and layout of the prototyping pi plate will be familiar to anyone who has used an arduino microcontroller. An add-on board with the same footprint as the target device, designed to connect to on-board headers and sit above the surface of the o...

  • Page 272: 256

    P a r t i v h a r d w a r e h a c k i n g 256 figure 16-5: a pi plate with a quarter-size breadboard perhaps the biggest reason for choosing a pi plate over the slice of pi is its clever design. Once connected to the gpio header on the pi, there is little reason to remove it unless you need access t...

  • Page 273: 257

    C h a p t e r 1 6 a d d - o n b o a r d s 257 figure 16-6: the pi plate connected to a cased raspberry pi fen logic gertboard properly termed the raspberry pi i/o extension, the gertboard (shown in figure 16-7) is named for its inventor, gert van loo. An employee of broadcom and a member of the team...

  • Page 274: 258

    P a r t i v h a r d w a r e h a c k i n g 258 figure 16-7: the gertboard, designed by gert van loo as the list of features indicates, the gertboard is a powerful and complex device. Its flexibil- ity makes it a great choice for more complex projects—but that same complexity affects its size. The ger...

  • Page 275: 259

    C h a p t e r 1 6 a d d - o n b o a r d s 259 figure 16-8: the gertboard connected to a raspberry pi the motor controller functionality of the gertboard makes it easy to integrate the pi into a robotics project. It’s suitable for motors of up to 48 v at 4 a, which is easily powerful enough to drive ...

  • Page 276: 260

    P a r t i v h a r d w a r e h a c k i n g 260 figure 16-9: using the gertboard motor controller for sensing or feedback projects, the gertboard’s adc and dac components are handy addi- tions. Unlike the gpio pins on the pi itself, which can only receive and send digital signals, the gertboard includ...

  • Page 277: 261

    C h a p t e r 1 6 a d d - o n b o a r d s 261 figure 16-10: connecting a potentiometer to the gertboard’s adc pins finally, the gertboard provides access to a 28-pin microcontroller, which can be fitted to a socket located on the board itself. When fitted, the gertboard can be wired to address eithe...

  • Page 278: 262

    P a r t i v h a r d w a r e h a c k i n g 262 the gertboard is a very advanced device, and isn’t suitable for every project. For many simple uses, a cheaper passive add-on board like the slice of pi will usually be enough. However, for in-depth experimentation of just what is possible with the raspb...

  • Page 279: Appendixes

    Part v appendixes appendix a python recipes appendix b camera module quick reference appendix c hdmi display modes.

  • Page 281: Appendix A

    Appendix a python recipes.

  • Page 282: 266

    P a r t v a p p e n d i x e s 266 the following recipes provide the program code for example 3 and example 4 in chapter 12, “an introduction to python”, and a sample solution for the combined input-output program suggested in chapter 14, “the gpio port”. Each recipe is also available for download fr...

  • Page 283: 267

    A p p e n d i x a p y t h o n r e c i p e s 267 time.Sleep(5) pygame.Quit() sys.Exit() while true: for event in pygame.Event.Get(): if event.Type == quit: pygame.Quit() elif event.Type == keydown: if event.Key == k_right or event.Key == ord(‘d’): changedirection = ‘right’ if event.Key == k_left or e...

  • Page 284: 268

    P a r t v a p p e n d i x e s 268 raspberryposition = [int(x*20),int(y*20)] raspberryspawned = 1 playsurface.Fill(blackcolour) for position in snakesegments: pygame.Draw.Rect(playsurface,whitecolour,rect ↵ (position[0], position[1], 20, 20)) pygame.Draw.Rect(playsurface,redcolour,rect ↵ (raspberrypo...

  • Page 285: 269

    A p p e n d i x a p y t h o n r e c i p e s 269 ‘hostname’: ‘localhost’, ‘servername’: ‘localhost’, ‘realname’: ‘raspberry pi names bot’ } s = socket.Socket(socket.Af_inet, socket.Sock_stream) print ‘connecting to %(host)s:%(port)s...’ % irc try: s.Connect((irc[‘host’], irc[‘port’])) except socket.E...

  • Page 286: 270

    P a r t v a p p e n d i x e s 270 gpio input and output (chapter 14) #!/usr/bin/env python # raspberry pi gpio input/output example # written by gareth halfacree for the raspberry pi user guide import rpi.Gpio as gpio gpio.Setmode(gpio.Board) gpio.Setup(11, gpio.Out) gpio.Setup(12, gpio.In) gpio.Out...

  • Page 287: Appendix B

    Appendix b raspberry pi camera module quick reference.

  • Page 288: 272

    P a r t v a p p e n d i x e s 272 the raspberry pi camera module comes with software designed to capture still images and videos, each of which has a variety of options that can control the final output. Options for raspistill, raspiyuv and raspivid are included in this appendix. For more informatio...

  • Page 289: 273

    A p p e n d i x b r a s p b e r r y p i c a m e r a m o d u l e q u i c k r e f e r e n c e 273 ❍ -ex or --exposure (exposure mode)—sets the camera’s automatic exposure setting, which controls how long the camera spends capturing an individual image or frame and is largely a factor of available ligh...

  • Page 290: 274

    P a r t v a p p e n d i x e s 274 ❍ -op or --opacity (preview opacity)—controls how transparent the preview window appears. Possible values are any whole number from 0 to 255, where 0 is completely transparent and thus invisible and 255 is completely visible. Using a value around 128 allows you to s...

  • Page 291: 275

    A p p e n d i x b r a s p b e r r y p i c a m e r a m o d u l e q u i c k r e f e r e n c e 275 ❍ -w or --width (width)—specifies the width, or horizontal resolution, of the captured image or video. This should be set to the desired width in pixels; for example, a full hd capture would require a wid...

  • Page 292: 276

    P a r t v a p p e n d i x e s 276 raspivid options designed to capture moving images, raspivid has some specific options that do not apply to raspistill . These options are listed below. ❍ -b or --bitrate (encoding bitrate)—sets the bitrate of the captured video, in bits per second (bps.) the higher...

  • Page 293: Appendix C

    Appendix c hdmi display modes.

  • Page 294: 278

    P a r t v a p p e n d i x e s 278 you can use the values in table c-1 and table c-2 with the hdmi_mode option in con- fig.Txt to alter the hdmi video output stream. For more information, see chapter 7, “advanced raspberry pi configuration”. Table c-1 hdmi group 1 (cea) value description 1 vga (640x4...

  • Page 295: 279

    A p p e n d i x b h d m i d i s p l a y m o d e s 279 value description 28 288p 50hz (pixel quadrupling enabled) (16:9 aspect ratio) 29 576p 50hz (pixel doubling enabled) 30 576p 50hz (pixel doubling enabled) (16:9 aspect ratio) 31 1080p 50hz 32 1080p 24hz 33 1080p 25hz 34 1080p 30hz 35 480p 60hz (p...

  • Page 296: 280

    P a r t v a p p e n d i x e s 280 table c-2 hdmi group 2 (dmt) value description 1 640×350 85hz 2 640×400 85hz 3 720×400 85hz 4 640×480 60hz 5 640×480 72hz 6 640×480 75hz 7 640×480 85hz 8 800×600 56hz 9 800×600 60hz 10 800×600 72hz 11 800×600 75hz 12 800×600 85hz 13 800×600 120hz 14 848×480 60hz 15 ...

  • Page 297: 281

    A p p e n d i x b h d m i d i s p l a y m o d e s 281 value description 34 1280×960 120hz (reduced blanking) 35 1280×1024 60hz 36 1280×1024 75hz 37 1280×1024 85hz 38 1280×1024 120hz (reduced blanking) 39 1360×768 60hz 40 1360×768 120hz (reduced blanking) 41 1400×1050 (reduced blanking) 42 1400×1050 ...

  • Page 298: 282

    P a r t v a p p e n d i x e s 282 table c-2 continued value description 67 1856×1392 120hz (reduced blanking) 68 1920×1200 (reduced blanking) 69 1920×1200 60hz 70 1920×1200 75hz 71 1920×1200 85hz 72 1920×1200 120hz (reduced blanking) 73 1920×1440 60hz 74 1920×1440 75hz 75 1920×1440 120hz (reduced bl...

  • Page 299: Symbols

    Index symbols * (asterisk), python, 182 ` (backtick) symbol, 245 $ (dollar) symbol, 246 == (double-equals) evaluation, python, 182 = (equals sign) evaluation, python, 182 > (greater than) evaluation, python, 182 >= (greater than or equal to) evaluation, python, 182 # (hash), python, 180 - (minus) sy...

  • Page 300: 284

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 284 breadboards disadvantages, 210–211 gpio input example, python, 228–232, 270 gpio output example, python, 223–228, 270 overview, 204 quarter-size, 255 brick-and-mortar electronics stores, 209 bricking, 115 bricks, scratch, 161 broadcas...

  • Page 301: 285

    I n d e x 285 config.Txt file boot options, 114 capabilities, 110 display settings, 111–114 editing via noobs, 108 l2 cache, 118 memory partitioning, 119–120 overclocking, 114–116 overvoltage, 116–118 test mode, 119 configuration, using raspi-config about screen, 105 boot to desktop, 96 change hostn...

  • Page 302: 286

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 286 directories, 43, 53–54, 150. See also specific directories disable_commandline_tags setting, config.Txt file, 114 disable_l2cache=1 option, config.Txt file, 118 disable_overscan setting, config.Txt file, 111 diskutil command, 38 displ...

  • Page 303: 287

    I n d e x 287 flashing, software downloads, 35–36 from linux, 37 from os x, 38 from windows, 38–39 floating pins, 229 flooding, 198 flux, 205, 213 for instruction block, pygame, 187 forever block, scratch, 164 forking, 49 forward current, 224 forward voltage, 224 fpsclock variable, pygame, 185 fpscl...

  • Page 304: 288

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 288 graphics processing unit (gpu) memory partition, 103–104, 119–120 greater than (>) evaluation, python, 182 greater than or equal to (>=) evaluation, python, 182 green flag icon, scratch, 160, 163 grep tool, 79 groups, 52 grub (grand u...

  • Page 305: 289

    I n d e x 289 image editing, 141–143 image writer for windows (website), 38 import command, 185, 220 impress, libreoffice, 140 indiecity, 56 inet addr , ifconfig tool output, 69 infinite loop, 247 infrared remote controls, 129 init_emmc_clock setting, config.Txt file, 116 init_uart_baud option, conf...

  • Page 306: 290

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 290 link encap , ifconfig tool output, 69 linking files or directories, 150 linux basic commands, 44–45 debian distribution, 49 distributions, 36 flashing sd cards, 37 glossary, 43 installing, 33–39 kernel, 71–72 os x, compared to, 20 ove...

  • Page 307: 291

    I n d e x 291 mtu , ifconfig tool output, 70 mullins, rob (computer scientist), 4 multimeter, 66, 206 multiplier colour band, 206 multi-threaded programming language, 166 music on console (moc) tool, 126–128 music playback, 126–128 μtorrent (website), 36 mv command, 44 my library, 57 mycroft, alan (...

  • Page 308: 292

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 292 overscan_left setting, config.Txt file, 111 overscan_right setting, config.Txt file, 111 overscan_top setting, config.Txt file, 111 over_voltage setting, config.Txt file, 117–118 overvoltage settings, config.Txt file, 116–118 over_vol...

  • Page 309: 293

    I n d e x 293 productivity software cloud-based apps, 136–139 the gimp, 141–143 libreoffice suite, 139–141 programming skills, need for, 1–3 project board, 212 protocols, 204 prototyping boards, 254–262 ps/2 connectors, 25 pull-up resistors, 223 purge command, 61 push-button controllers, 232 push-bu...

  • Page 310: 294

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 294 running, 94–95 ssh, 104 update, 104 raspistill package file extensions, 241 filenames, 242 flip images, 241 options, 272–275 test camera, 239–240 time-lapse mode, 243–248 raspivid package, 242–243, 272–275, 276 raspiyuv package, 272–2...

  • Page 311: 295

    I n d e x 295 shaw, zed a. (author) learn python the hard way, 199 shebang (#!), 176 shell, 43 shell scripting, 243–248 shinners, pete (programmer), 184 shutdown command, 33, 62 side cutters, 205 single-board computer, 18–20 sleep command, 246 smalltalk programming language, 158 socket module, pytho...

  • Page 312: 296

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 296 terminal connecting to wireless network, 85–92 definition, 37, 43, 44 gui packages, 46 running python programs, 177–178 test mode, 119 test points, voltage, 65–66 testcapture.Jpg , camera module, 241 think block, scratch, 164 thinkfre...

  • Page 313: 297

    I n d e x 297 v v3d_freq setting, config.Txt file, 116 van loo, gert (inventor), 257 var directory, 54 variables, python, 181, 194 var/www folder, 150 vector images, 140, 141 veroboard, 211 -vf (vertical flip) option, camera, 241 vfat partition, 54 vga adapter dongle, 23 vias, 66 video connecting, 2...

  • Page 314: 298

    R a s p b e r r y p i u s e r g u i d e , 2 n d e d i t i o n 298 y yum tool, 58 z zeroconf standard, 131 000-default file, 150 zip archive, defined, 27, 36 zoho, 137.