Summary of Raspberry Pi

  • Page 1

    Www.Allitebooks.Com.

  • Page 2: Raspberry Pi

    Eben upton and gareth halfacree raspberry pi ® user guide a john wiley and sons, ltd., publication www.Allitebooks.Com.

  • Page 3: Raspberry Pi

    Raspberry pi ® user guide www.Allitebooks.Com.

  • Page 4

    This edition first published 2012 © 2012 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 5

    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 assistant editor ellie scott project editor kathryn duggan copy editor kathryn dugg...

  • Page 6

    Www.Allitebooks.Com.

  • Page 7: 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 customers. In an ear...

  • Page 8

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

  • Page 9: Table of Contents

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

  • Page 10: Xii

    Xii r a s p b e r r y p i u s e r g u i d e chapter 3 troubleshooting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 keyboard and mouse diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 power diagnostics . . . . . ...

  • Page 11: Xiii

    Xiii t a b l e o f c o n t e n t s part ii: using the pi as a media centre, productivity machine and web server chapter 7 the pi as a home theatre pc . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 playing music at the console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

  • Page 12: Xiv

    Xiv r a s p b e r r y p i u s e r g u i d e example 3: gaming with pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 example 4: python and networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171 further readi...

  • Page 13: 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 setting up our pc than i do .” i asked him if they could program, to which he replied: “why would they want to? T...

  • Page 14

    R a s p b e r r y p i u s e r g u i d e 2 people looking on jealously, and that’ll make you feel wonderfully smug all afternoon . In my day job, where i design the sort of silicon chips that we use in the raspberry pi as a processor and work on the low-level software that runs on them, i basically g...

  • Page 15: A Bit of History

    I n t r o d u c t i o n 3 where computing is arriving on the syllabus and ict is being reshaped, and we’ve seen a massive change in awareness of a gap in our educational and cultural provision for kids just in the short time since the raspberry pi was launched . Too many of the computing devices a c...

  • Page 16

    R a s p b e r r y p i u s e r g u i d e 4 two years . The best undergraduates—the ones who performed the best at the end of their three-year course—were the ones who weren’t just programming when they’d been told to for their weekly assignment or for a class project . They were the ones who were pro...

  • Page 17

    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-megapixel came...

  • Page 18

    R a s p b e r r y p i u s e r g u i d e 6 cost model we’d suggested (the $25 figure came from my statement to the bbc that the raspberry pi should cost around the same as a text book, and is a splendid demonstration of the fact that i had no idea how much text books cost these days), and the tiny pr...

  • Page 19

    I n t r o d u c t i o n 7 part of my job at raspberry pi involves giving talks to hacker groups, computing conferences, teachers, programming collectives and the like, and there’s always someone in the audience who has talked to me or to my wife liz (who runs the community) on the raspberry pi websi...

  • Page 20

    R a s p b e r r y p i u s e r g u i d e 8 so what can you do with the raspberry pi? This book explores a number of things you can do with your raspberry pi, from controlling hardware with python, to using it as a media centre, or building games in scratch . The beauty of the raspberry pi is that it’...

  • Page 21: Chapter 1

    Chapter 1 meet the raspberry pi chapter 2 linux system administration chapter 3 troubleshooting chapter 4 network configuration chapter 5 partition management chapter 6 configuring the raspberry pi part i connecting the board.

  • Page 23: Chapter

    Chapter 1 meet the raspberry pi.

  • Page 24: Arm Vs  X86

    R a s p b e r r y p i u s e r g u i d e 12 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 things, but there are a few things you’re going to need to know before you plunge head-first int...

  • Page 25: Windows Vs  Linux

    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 13 developed by acorn computers back in the late 1980s, the arm architecture is a relatively uncommon sight in the desktop world . Where it excels, however, is in mobile devices: the phone in your pocket almost certainly has at least one arm-based ...

  • Page 26: Connecting A Display

    R a s p b e r r y p i u s e r g u i d e 14 the raspberry pi, by contrast, is designed to run an operating system called gnu/linux— hereafter referred to simply as linux . Unlike windows or os x, linux is open source: it’s possible to download the source code for the entire operating system and make ...

  • Page 27

    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 found within an image—red, green and blue—and sends it down a single wire to the display device, typically an old cathode-ray tube (crt) tv . Figure 1-2: the yellow rca phono connector, for composite video output when no other display device is ...

  • Page 28: Connecting Audio

    R a s p b e r r y p i u s e r g u i d e 16 figure 1-3: the silver hdmi connector, for high-definition video output if your monitor has a vga input—a d-shaped connector with 15 pins, typically coloured silver and blue—the raspberry pi can’t connect to it . Adapters are available that will take in a d...

  • Page 29

    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 assuming you’re connecting the pi to a standard hdmi display, there’s very little to do at this point . For now, it’s enough to simply connect the cable . If you’re using the pi with a dvi-d monitor via an adapter or cable, audio will not be inc...

  • Page 30

    R a s p b e r r y p i u s e r g u i d e 18 can connect the keyboard and mouse directly to these ports . If you’re using model a, you’ll need to purchase a usb hub in order to connect two usb devices simultaneously . Figure 1-4: model b’s two usb ports a usb hub is a good investment for any pi user: ...

  • Page 31: A Note On Storage

    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 a note on storage as you’ve probably noticed, the raspberry pi doesn’t have a traditional hard drive. Instead it uses a secure digital (sd) memory card, a solid-state storage system typically used in digital cameras. Almost any sd card will work...

  • Page 32

    R a s p b e r r y p i u s e r g u i d e 20 which distribution you choose to download is up to you . Instructions in the rest of the book will be based on the debian raspberry pi distribution, a good choice for beginners . Where possible, we’ll give you instructions for other distributions as well . ...

  • Page 33

    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 21 figure 1-5: flashing the sd card using the dd command in linux flashing from os x if your current pc is a mac running apple os x, you’ll be pleased to hear that things are as simple as with linux . Thanks to a similar ancestry, os x and linux bo...

  • Page 34: Connecting External Storage

    R a s p b e r r y p i u s e r g u i d e 22 image file flashed onto the sd card . Although it’s possible to install a windows-compatible version of dd , there is an easier way: the image writer for windows . Designed specifically for creating usb or sd card images of linux distributions, it features ...

  • Page 35: Connecting The Network

    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 23 figure 1-6: two usb mass storage devices: a pen drive and an external hard drive the majority of usb mass storage devices can be read by the pi, whether or not they have existing content . In order for the pi to be able to access these devices, ...

  • Page 36

    R a s p b e r r y p i u s e r g u i d e 24 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 connecting ...

  • Page 37

    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 25 port and another (typically wireless) connection . Doing so is outside the scope of this book, but if you are completely unable to connect the pi to the internet in any other way, you can try searching your operating system’s help file for “brid...

  • Page 38: Connecting Power

    R a s p b e r r y p i u s e r g u i d e 26 ❍ ensure that linux is listed as a supported operating system . Some wireless adapters are provided with drivers for windows and os x only, making them incompatible with the raspberry pi . A list of wi-fi adapters known to work with the raspberry pi can be ...

  • Page 39: Chapter

    Chapter 2 linux system administration.

  • Page 40: Linux: An Overview

    R a s p b e r r y p i u s e r g u i d e 28 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 of you...

  • Page 41

    C h a p t e r 2 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 29 table 2 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 . C...

  • Page 42: Linux Basics

    R a s p b e r r y p i u s e r g u i d e 30 linux basics although there are hundreds of different linux distributions available, they all share a com- mon set of tools . These tools, which are operated via the terminal, are analogous to similar tools on windows and os x . To get started, you’ll need ...

  • Page 43

    C h a p t e r 2 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 31 the raspberry pi build of debian includes a desktop environment known as the lightweight x11 desktop environment (lxde) . Designed to offer an attractive user interface using the x window system software, lxde provides a familiar p...

  • Page 44

    R a s p b e r r y p i u s e r g u i d e 32 accessories ❍ debian reference—a built-in reference guide, this provides a detailed explanation of the debian linux distribution and how programmers can contribute to its development . ❍ file manager—the pcmanfm file manager provides a graphical browser for...

  • Page 45

    C h a p t e r 2 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 33 ❍ idle 3—clicking this entry loads idle configured to use the newer python 3 pro- gramming language, rather than the default python 2 .7 language . Both are largely compatible with each other, but some programs may require features...

  • Page 46: Finding Help

    R a s p b e r r y p i u s e r g u i d e 34 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 64 gb, which is tiny compared to the 3,000 gb (3 tb) available from the largest ...

  • Page 47

    C h a p t e r 2 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 35 figure 2-2: lxde automatically mounting a usb mass storage device from the console, things are only slightly more difficult . To make a device accessible to linux when the desktop environment isn’t loaded, follow these steps: 1 con...

  • Page 48: Creating A New User Account

    R a s p b e r r y p i u s e r g u i d e 36 creating a new user account unlike windows and os x, which are largely designed for use by a single individual, linux is at heart a social operating system designed to accommodate numerous users . By default, debian is configured with two user accounts: pi,...

  • Page 49: File System Layout

    C h a p t e r 2 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 37 file system layout the content of the sd card is known as its file system, and is split into multiple sections each with a particular purpose . Although it’s not necessary for you to understand what each sec- tion does in order to ...

  • Page 50

    R a s p b e r r y p i u s e r g u i d e 38 figure 2-3: a directory listing for the pi’s root file system the directories visible on the default debian distribution are as follows: ❍ boot—this contains the linux kernel and other packages needed to start the pi . ❍ bin—operating system-related binary ...

  • Page 51: Physical Layout

    C h a p t e r 2 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 39 ❍ media—this is a special directory for removable storage devices, like usb memory sticks or external cd drives . ❍ mnt—this folder is used to manually mount storage devices, such as external hard drives . ❍ opt—this stores optiona...

  • Page 52: Other Distributions

    R a s p b e r r y p i u s e r g u i d e 40 installing and uninstalling software the default software installed with the debian distribution is enough to get you started, but chances are you’re going to want to customise your pi according to your own requirements . Installing new software onto the pi...

  • Page 53: Finding Software

    C h a p t e r 2 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 41 finding software the first step to installing a new piece of software is to find out what it’s called . The easiest way to do this is to search the cache of available software packages . This cache is a list of all the software ava...

  • Page 54: Installing Software

    R a s p b e r r y p i u s e r g u i d e 42 tip if your search term brings up too many different packages to see on a single screen display, you can tell linux that you want it to pause on each screenfull by piping the output of apt-cache through a tool called less. Simply change the command to apt-c...

  • Page 55: Uninstalling Software

    C h a p t e r 2 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 figure 2-5: apt listing the dependencies for the openoffice .Org package 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...

  • Page 56: Upgrading Software

    R a s p b e r r y p i u s e r g u i d e 44 upgrading software in addition to installing and uninstalling packages, apt can be used to keep them up to date . Upgrading a package through apt ensures that you’ve received the latest updates, bug fixes and security patches . Before trying to upgrade a pa...

  • Page 57: Chapter

    Chapter 3 troubleshooting.

  • Page 58

    R a s p b e r r y p i u s e r g u i d e 46 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 fix ...

  • Page 59: Power Diagnostics

    C h a p t e r 3 t r o u b l e s h o o t i n g 47 pi from operating . Sometimes, these issues don’t appear until other usb devices are con- nected to the pi . If your keyboard was working fine until another usb device, in particular a usb wireless adapter, was connected, you may have an issue of inco...

  • Page 60

    R a s p b e r r y p i u s e r g u i d e 48 warning avoid touching the test probes to anything not labelled as a test point. It’s possible to bridge the 5 v supply that comes in to the pi to the internal 3.3 v supply, creating a short circuit which can damage the device. Be especially careful around ...

  • Page 61: Display Diagnostics

    C h a p t e r 3 t r o u b l e s h o o t i n g 49 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...

  • Page 62: Boot Diagnostics

    R a s p b e r r y p i u s e r g u i d e 50 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 on t...

  • Page 63

    C h a p t e r 3 t r o u b l e s h o o t i n g 51 figure 3-2: the output of ifconfig on a raspberry pi model b the output of ifconfig is split into the following sections: ❍ link encap—the type of encapsulation used by the network, which on the model b will either read ethernet for the physical netwo...

  • Page 64

    R a s p b e r r y p i u s e r g u i d e 52 ❍ rx—this section provides feedback on the received network traffic, including the number of errors and dropped packets recorded . If you start to see errors appearing in this section, there’s something wrong with the network . ❍ tx—this provides the same i...

  • Page 65: The Emergency Kernel

    C h a p t e r 3 t r o u b l e s h o o t i n g 53 figure 3-3: the result of a successful test of the network, using the ping command the emergency kernel the linux kernel is the heart of the operating system that drives the pi . It’s responsible for everything from making sure that you can access you...

  • Page 66

    R a s p b e r r y p i u s e r g u i d e 54 the linux kernel is a single file located in the /boot directory called kernel.Img . When the pi is first switched on and begins to load the operating system, it looks for this file, and if the file is missing, the pi won’t work . The emergency kernel is a ...

  • Page 67: Chapter

    Chapter 4 network configuration.

  • Page 68: Wired Networking

    R a s p b e r r y p i u s e r g u i d e 56 for most users, configuring the 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 that your networ...

  • Page 69

    C h a p t e r 4 n e t w o r k c o n f i g u r a t i o n 57 netmask , you should enter the network mask—which controls the size of the connected network—in what is known as dotted-quad format . If you’re using a home network, this is typically 255 .255 .255 .0 . For gateway , you should enter the ip ...

  • Page 70

    R a s p b e r r y p i u s e r g u i d e 58 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 again . S...

  • Page 71: Wireless Networking

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

  • Page 72

    R a s p b e r r y p i u s e r g u i d e 60 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 required...

  • Page 73

    C h a p t e r 4 n e t w o r k c o n f i g u r a t i o n 61 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 message particular to the ...

  • Page 74

    R a s p b e r r y p i u s e r g u i d e 62 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 that...

  • Page 75

    C h a p t e r 4 n e t w o r k c o n f i g u r a t i o n 63 connected to the internet through its wired ethernet port or a usb ethernet adapter . When connected, install the firmware by typing the following: sudo apt-get install firmwarepackage replace firmwarepackage in this command with the name of...

  • Page 76

    R a s p b e r r y p i u s e r g u i d e 64 this command will return a list of all the wireless networks reachable from the pi and their details (see figure 4-4) . If you receive an error message at this point—in particular, one that claims the network or interface is down—check that you have install...

  • Page 77

    C h a p t e r 4 n e t w o r k c o n f i g u r a t i o n 65 ❍ 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.11bg...

  • Page 78

    R a s p b e r r y p i u s e r g u i d e 66 figure 4-5: 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 “wired net...

  • Page 79

    C h a p t e r 4 n e t w o r k c o n f i g u r a t i o n 67 figure 4-6: 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 80

    R a s p b e r r y p i u s e r g u i d e 68 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 enter th...

  • Page 81

    C h a p t e r 4 n e t w o r k c o n f i g u r a t i o n 69 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 82

    R a s p b e r r y p i u s e r g u i d e 70 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 could be ...

  • Page 83: Chapter

    Chapter 5 partition management.

  • Page 84: Creating A New Partition

    R a s p b e r r y p i u s e r g u i d e 72 having the raspberry pi’s operating system provided as an image of somebody else’s sd card is convenient, but a little inflexible . Most distribution images available for download assume a 2 gb or 4 gb sd card, meaning that people with 8 gb or larger cards ...

  • Page 85

    C h a p t e r 5 p a r t i t i o n m a n a g e m e n t 73 figure 5-1: creating a new partition using cfdisk 5 you will be prompted to create a primary or logical partition . The default is to create a primary partition, so just press enter to accept this . 6 the next prompt will ask you how big the p...

  • Page 86

    R a s p b e r r y p i u s e r g u i d e 74 when the pi has restarted and you’ve logged back in, you can use fdisk to verify that the new partition is ready for use with the following command: sudo fdisk -l before you can store files on the new partition, however, you need to put a file system in pla...

  • Page 87

    C h a p t e r 5 p a r t i t i o n m a n a g e m e n t 75 you’ll also need to allow all members of the group to write to the directory . To do this, you use the chmod command with the option g+w , which tells chmod to allow write access from the group: sudo chmod -r g+w /storage the new partition is ...

  • Page 88: Resizing Existing Partitions

    R a s p b e r r y p i u s e r g u i d e 76 figure 5-2: editing fstab to automatically mount the new partition resizing existing partitions creating a new partition is one way to make use of a larger sd card, but it’s not the most flex- ible . A better method is to resize existing partitions to make ...

  • Page 89: Manual Resizing

    C h a p t e r 5 p a r t i t i o n m a n a g e m e n t 77 to resize the root file system using the raspi-config tool, follow these instructions: 1 if this is the first time you have loaded debian on the raspberry pi, raspi-config will load automatically . If it does not, type sudo raspi-config at the...

  • Page 90

    R a s p b e r r y p i u s e r g u i d e 78 the disc works on both pcs and macs, and operates entirely from memory . As a result, it won’t try to replace your existing operating system . It’s also compatible with any distribution for the raspberry pi, unlike the debian-specific raspi-config tool . Ti...

  • Page 91

    C h a p t e r 5 p a r t i t i o n m a n a g e m e n t 79 by default, the partition editor will look at the first drive it finds in your system, which is usu- ally your pc’s hard drive . You don’t want to make changes to that, so make sure to click on the device selector in the top-right corner and c...

  • Page 92

    R a s p b e r r y p i u s e r g u i d e 80 figure 5-6: moving the swap partition in the partition editor 4 click the resize/move button to confirm the change . Moving the partition will trigger a warning about the potential for the new partition table to lead to booting problems . That doesn’t apply...

  • Page 93: Moving to A Bigger Sd Card

    C h a p t e r 5 p a r t i t i o n m a n a g e m e n t 81 thanks to the changes made in the partition editor (see figure 5-8), the main partition on the sd card is now as big as the card will allow it to be . As this is where most linux distribu- tions store both their own files and the users’ files,...

  • Page 94: Imaging From Linux

    R a s p b e r r y p i u s e r g u i d e 82 the first step to moving to a larger sd card is to create an image of the existing card . If you have access to two sd card readers, you can skip this step and, in the later instructions, replace the name of the image file with the device address of the sd ...

  • Page 95: Imaging From Os X

    C h a p t e r 5 p a r t i t i o n m a n a g e m e n t 83 imaging from os x imaging the pi’s sd card on os x is almost exactly the same as flashing the sd card was back in chapter 1 . Again, make sure you have enough hard disk space to hold a file the size of the sd card . Then follow these steps: 1 ...

  • Page 96

    R a s p b e r r y p i u s e r g u i d e 84 figure 5-10: the terminal icon in parted magic now that you have your disk image, use the instructions on flashing an sd card from chapter 1, “meet the raspberry pi”, to write it to the new card . Remember that writing an image takes time, so be patient and...

  • Page 97: Chapter

    Chapter 6 configuring the raspberry pi.

  • Page 98

    R a s p b e r r y p i u s e r g u i d e 86 because of its origins in embedded computing, the bcm2835 chip at the heart of the raspberry pi doesn’t have anything like a pc’s bios menu where various low-level system settings can be configured . Instead, it relies on text files containing configuration...

  • Page 99: Modifying The Display

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 87 if you’re having problems with graphics output, such as the image not filling the screen or spilling over the edge, config.Txt is where you’ll be able to fix it . Normally, the file is empty or—on some distributions—simply not pres...

  • Page 100

    R a s p b e r r y p i u s e r g u i d e 88 ❍ overscan_bottom—this can be used to skip a number of pixels from the bottom of the display . Typically, the values for all the overscan_ settings would be the same, creating a regular border around the display . ❍ disable_overscan—if you use a monitor or ...

  • Page 101

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 89 ❍ hdmi_mode—in addition to setting the video mode for the analogue composite out- put, it’s also possible to override automatic resolution detection on the hdmi port . This is handy if you want to run your pi at a lower resolution ...

  • Page 102: Boot Options

    R a s p b e r r y p i u s e r g u i d e 90 each option in config.Tx t should be on its own line, with the option name followed by an equals sign ( = ) and then the required value . For example, to tell the pi to use a pal-format analogue tv with a 4:3 aspect ratio and a 20-pixel overscan on all side...

  • Page 103

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 91 ❍ kernel—the name of the kernel file to be loaded . This can be used as a way to load the emergency kernel (see chapter 3, “troubleshooting”) . ❍ ramfsfile—the name of the initial ram file system (ramfs) to be loaded . This should ...

  • Page 104

    R a s p b e r r y p i u s e r g u i d e 92 the reason the pi isn’t provided with higher operating speeds in the first place is related to the chips’ lifespan . The bcm2835 is rated by its manufacturer, broadcom, to operate at a speed of 700 mhz . Increasing the speed above this officially-rated leve...

  • Page 105

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 93 as an example, to overclock the cpu to 800 mhz, the gpu to 280 mhz and the ram to 420 mhz, enter the following options into config.Txt , one per line: arm_freq=800 gpu_freq=280 sdram_freq=420 as with adjusting the display configura...

  • Page 106: Disabling L2 Cache

    R a s p b e r r y p i u s e r g u i d e 94 the voltage adjustment settings have upper and lower limits of 8 and -16, equivalent to 0 .2 v above stock voltage or 1 .4 v absolute and 0 .4 v below stock voltage or 0 .8 v absolute . The voltage must be adjusted in whole numbers, and it cannot be adjuste...

  • Page 107: Enabling Test Mode

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 95 because of the bcm2835’s origins as a multimedia processor targeted at set-top boxes, this l2 cache is designed to be used by the gpu portion of the chip alone . Unlike a traditional processor, the cpu doesn’t have any l2 cache of ...

  • Page 108

    R a s p b e r r y p i u s e r g u i d e 96 memory partitioning—start elf although the raspberry pi only has a single 256 mb memory chip, that memory can be apportioned to the hardware in a variety of ways . The bcm2835 is split into two main sec- tions: the general-purpose cpu and the graphics-orien...

  • Page 109

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 97 the first file, arm128_start.Elf , is configured to split the memory down the middle, with 128 mb available to the bcm2835’s arm cpu and 128 mb available to the videocore iv gpu . The second and third files gradually reduce the amo...

  • Page 110

    R a s p b e r r y p i u s e r g u i d e 98 figure 6-3: the cmdline .Txt file in /boot 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 cm...

  • Page 111

    C h a p t e r 6 c o n f i g u r i n g t h e r a s p b e r r y p i 99 the console option tells linux that it should create a serial console—device ttyama0 — and at what speed it should operate . In most cases, the speed should be left at the default of 115,200 kb/s (kilobytes per second) . If the pi ...

  • Page 112: 100

    R a s p b e r r y p i u s e r g u i d e 100 next . Entries that work on fedora remix may not work on debian, and vice versa . The options available to cmdline.Txt depend on what kernel the distribution is using and what features were included when the kernel was built . If you’re a kernel developer,...

  • Page 113: Chapter 7

    Chapter 7 the pi as a home theatre pc chapter 8 the pi as a productivity machine chapter 9 the pi as a web server part ii using the pi as a media centre, productivity machine and web server.

  • Page 115: Chapter

    Chapter 7 the pi as a home theatre pc.

  • Page 116: 104

    R a s p b e r r y p i u s e r g u i d e 104 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, originally developed for use in htpcs . The graphics portion of the bc...

  • Page 117: 105

    C h a p t e r 7 t h e p i a s a h o m e t h e a t r e p c 105 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 7-1) . The left pane is a file browser, which a...

  • Page 118: 106

    R a s p b e r r y p i u s e r g u i d e 106 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 playlist ❍ -r—return to the previous song in the directory or pla...

  • Page 119: 107

    C h a p t e r 7 t h e p i a s a h o m e t h e a t r e p c 107 warning if you already have an sd card you use with the pi, be aware that installing rasbmc on it will delete the contents of the card. Back up any files you want to keep, or if you want to be able to switch between the two distributions,...

  • Page 120: 108

    R a s p b e r r y p i u s e r g u i d e 108 streaming internet media by default, xbmc is configured to play only files it can find on the raspberry pi itself . If you choose add-ons from beneath the video menu, you’ll be able to add some impressive internet streaming capabilities to the device, incl...

  • Page 121: 109

    C h a p t e r 7 t h e p i a s a h o m e t h e a t r e p c 109 in the centre of the screen or press enter, and then choose video add-ons from the options that appear . This provides access to your installed add-ons . Click on one to download a list of files for viewing . If an add-on has various cate...

  • Page 122: 110

    R a s p b e r r y p i u s e r g u i d e 110 xbmc isn’t just limited to upnp connections, however—the software can also connect to network servers running the network file system (nfs) standard common to unix-based sys- tems, the server message block (smb) standard common to windows servers and the z...

  • Page 123: 111

    C h a p t e r 7 t h e p i a s a h o m e t h e a t r e p c 111 figure 7-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 124: 112

    R a s p b e r r y p i u s e r g u i d e 112 figure 7-7: configuring rasbmc 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 created from the program’s source code automati- cally every night,...

  • Page 125: Chapter

    Chapter 8 the pi as a productivity machine.

  • Page 126: 114

    R a s p b e r r y p i u s e r g u i d e 114 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 or laptop, its low cost and environmentally-friendly power consu...

  • Page 127: 115

    C h a p t e r 8 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 115 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 i...

  • Page 128: 116

    R a s p b e r r y p i u s e r g u i d e 116 ❍ zoho—with five million registered users, zoho is another popular choice . As with google drive, a word processor, spreadsheet and presentation package are included, but zoho also offers enhanced business-centric features like a wiki-based knowledge base ...

  • Page 129: 117

    C h a p t e r 8 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 117 using openoffice org if you would prefer not to use a cloud-based service, the alternative is to install openoffice .Org . Designed as an open-source, cross-platform alternative to the popular microsoft office suite, openoffic...

  • Page 130: 118

    R a s p b e r r y p i u s e r g u i d e 118 ❍ openoffice.Org impress—the presentation application, equivalent to microsoft powerpoint ❍ openoffice.Org math—a small yet powerful package designed to make it easy to cre- ate and edit scientific formulae and equations for embedding in other openoffice ....

  • Page 131: 119

    C h a p t e r 8 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 119 created on the pi in openoffice .Org 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 openoffice .Org themselves, which ...

  • Page 132: 120

    R a s p b e r r y p i u s e r g u i d e 120 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 2, “linux system administration”, for details) . The gimp takes u...

  • Page 133: 121

    C h a p t e r 8 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 121 figure 8-4: exporting a file from the gimp.

  • Page 135: Chapter

    Chapter 9 the pi as a web server.

  • Page 136: 124

    R a s p b e r r y p i u s e r g u i d e 124 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 . Despite a small amount of memory and relatively underpowered proc...

  • Page 137: 125

    C h a p t e r 9 t h e p i a s a w e b s e r v e r 125 you can remember! You’ll be asked to confirm the password—to check for typing errors— and then the installation will continue . Figure 9-1: installing the lamp stack on debian figure 9-2: choosing a password for mysql.

  • Page 138: 126

    R a s p b e r r y p i u s e r g u i d e 126 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 correctly, use another computer on the network to connect to the raspb...

  • Page 139: 127

    C h a p t e r 9 t h e p i a s a w e b s e r v e r 127 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 140: 128

    R a s p b e r r y p i u s e r g u i d e 128 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 capacious external storage, for example—edit the text file 000-defau...

  • Page 141: 129

    C h a p t e r 9 t h e p i a s a w e b s e r v e r 129 figure 9-5: installing wordpress on the raspberry pi linking a file or directory is different to moving: the files for wordpress now exist in both /usr/share and /var/www simultaneously, without taking up any extra space on the pi’s sd card . If ...

  • Page 142: 130

    R a s p b e r r y p i u s e r g u i d e 130 figure 9-6: configuring wordpress in the netsurf web browser 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 admi...

  • Page 143: 131

    C h a p t e r 9 t h e p i a s a w e b s e r v e r 131 before you can access wordpress from another computer, you’ll need to create an additional configuration file . This is created by linking the existing configuration file—set up for local access—using the following command, typed as a single line...

  • Page 144: 132

    R a s p b e r r y p i u s e r g u i d e 132 tip wordpress is very memory intensive. For best results, use the pi as a headless server, which is a system that runs without a graphical user interface loaded. Then use a web browser on another computer connected to the network to access the wordpress da...

  • Page 145: Chapter 10

    Chapter 10 an introduction to scratch chapter 11 an introduction to python chapter 12 hardware hacking chapter 13 add-on boards part iii programming and hacking.

  • Page 147: Chapter

    Chapter 10 an introduction to scratch.

  • Page 148: 136

    R a s p b e r r y p i u s e r g u i d e 136 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 not just adults . The raspberry ...

  • Page 149: 137

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 137 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 f...

  • Page 150: 138

    R a s p b e r r y p i u s e r g u i d e 138 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 scratch doesn’t yet know what you wan...

  • Page 151: 139

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 139 figure 10-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 152: 140

    R a s p b e r r y p i u s e r g u i d e 140 figure 10-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, which lies in its impressive ...

  • Page 153: 141

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 141 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 154: 142

    R a s p b e r r y p i u s e r g u i d e 142 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 sounds independently . Adding in ...

  • Page 155: 143

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 143 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 156: 144

    R a s p b e r r y p i u s e r g u i d e 144 figure 10-7: using input blocks to control the motion of a sprite in scratch now that the sprite can be moved by the player, it’s time to give the sprite something to do . As this is just a very simple game, the goal should also be simple: to collect some ...

  • Page 157: 145

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 145 figure 10-8: adding the cheesy-puffs sprite to the scratch game project by default, any sprite added into a scratch project will appear in the dead centre of the stage . As a result, this obscures the existing cat sprite . Click on ...

  • Page 158: 146

    R a s p b e r r y p i u s e r g u i d e 146 try using the arrow keys on the keyboard to move the cat sprite towards the cheesy-puffs sprite now . As you can see, when the two sprites meet, nothing happens . That’s because the program doesn’t contain any instructions for what to do when the two sprit...

  • Page 159: 147

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 147 boolean logic named for george boole, boolean logic or boolean algebra is a key concept to understand- ing how computers work. In scratch, boolean logic is implemented in three operators bricks: and, or and not. The and operator req...

  • Page 160: 148

    R a s p b e r r y p i u s e r g u i d e 148 double-click the cat sprite on the stage to return to editing its scripts . The script you created for the cheesy-puffs sprite will disappear, but don’t worry—it’s still saved but only appears when you’re editing that particular sprite . Drag another if bl...

  • Page 161: 149

    C h a p t e r 1 0 a n i n t r o d u c t i o n t o s c r a t c h 149 although this example is effective for introducing some important programming concepts, it’s hardly the best way the game could be coded . Scratch includes a message broadcast system that allows code attached to one object to commun...

  • Page 162: 150

    R a s p b e r r y p i u s e r g u i d e 150 robotics with lego in addition to add-in sensor modules, it’s possible to control an external robotics system through scratch . The lego education wedo robotics kit is designed to connect motors, distance sensors, and speed sensors to a computer—all of whi...

  • Page 163: Chapter

    Chapter 11 an introduction to python.

  • Page 164: 152

    R a s p b e r r y p i u s e r g u i d e 152 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 brands including apple and blackberry...

  • Page 165: 153

    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 p y t h o n 153 environment (ide) such as idle, which provides python-specific functionality that’s missing from a standard text editor, including syntax checking, debugging facilities and the ability to run your program without having to leave the e...

  • Page 166: 154

    R a s p b e r r y p i u s e r g u i d e 154 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 python files . Although this is ...

  • Page 167: 155

    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 p y t h o n 155 to understand at a glance . It also makes it easy to spot so-called syntax errors caused by for- getting to put an end-quote in a print command or forgetting to comment out a remark . For this short example, syntax highlighting isn’t ...

  • Page 168: 156

    R a s p b e r r y p i u s e r g u i d e 156 figure 11-3: running helloworld .Py in idle if you created the helloworld.Py program in a text editor, you’ll need to open a terminal window from the accessories menu on the desktop . If you saved the file anywhere except your home directory, you’ll also h...

  • Page 169: 157

    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 p y t h o n 157 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 170: 158

    R a s p b e r r y p i u s e r g u i d e 158 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 some of the con- cepts required...

  • Page 171: 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 p y t h o n 159 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 shou...

  • Page 172: 160

    R a s p b e r r y p i u s e r g u i d e 160 the first line creates a new variable called goagain and sets it to 1 . The second line begins the loop, and tells python that while the goagain variable is equal to 1 , it should continue to loop through the following code . As the next few lines are writ...

  • Page 173: 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 p y t h o n 161 print firstnumber, “multiplied by”, secondnumber, “equals”, Æ firstnumber * secondnumber notice that while the addition and subtraction operations use the expected plus and minus symbols, multiplication uses the * symbol . Also notice...

  • Page 174: 162

    R a s p b e r r y p i u s e r g u i d e 162 save the program as calculator.Py , and run it by choosing run module from the run menu in idle or by typing python calculator.Py at the terminal . Enter your user name when prompted, and then provide the numbers that you want to calculate (see figure 11-5...

  • Page 175: 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 p y t h o n 163 before you can write a pygame program, you need to install the pygame library . If you’re using the recommended debian distribution, this is as simple as typing the following at the console or terminal: sudo apt-get install python-pyg...

  • Page 176: 164

    R a s p b e r r y p i u s e r g u i d e 164 the first line tells pygame to initialise itself, and the second line sets up a new variable called fpsclock , which will be used to control the speed of the game . Next, set up a new pygame display surface—the canvas onto which in-game objects will be dra...

  • Page 177: 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 p y t h o n 165 multiple points in the same program, using def to create a function means you’ll only have to type them once—and only have to change them in a single place if you alter the program later . Type the following lines to define the gameov...

  • Page 178: 166

    R a s p b e r r y p i u s e r g u i d e 166 continue the program with the following lines, paying attention to the indentation levels: for event in pygame.Event.Get(): if event.Type == quit: pygame.Quit() sys.Exit() elif event.Type == keydown: the first line, which comes right after the while loop b...

  • Page 179: 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 p y t h o n 167 these instructions modify the value of the changedirection variable, used to control the direction the player’s snake is travelling during the game . Using or with an if statement allows more than one evaluation to be made . In this c...

  • Page 180: 168

    R a s p b e r r y p i u s e r g u i d e 168 the += and -= operators are used to change the value of a variable by a certain amount: += sets the variable to its previous value plus the new value, while -= sets the variable to its previous value minus the new value . By way of example, snakeposition[0...

  • Page 181: 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 p y t h o n 169 the pop instruction is simple but clever: it returns the oldest value from the list but also removes it, making the list one item shorter . In the case of the snakesegment list, it tells python to delete the portion of the snake’s bod...

  • Page 182: 170

    R a s p b e r r y p i u s e r g u i d e 170 these tell pygame to fill in the background of the playing surface in black, draw the snake’s head and body segments in white, and finally, draw a raspberry in red . The last line, pygame. Display.Flip() , tells pygame to update the screen—without this ins...

  • Page 183: 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 p y t h o n 171 run it either by using idle’s run module option in the run menu or from the terminal by typing python raspberrysnake.Py . The game will start as soon as it has loaded (see figure 11-6), so make sure you’re ready! Figure 11-6: playing ...

  • Page 184: 172

    R a s p b e r r y p i u s e r g u i d e 172 as usual, create a new project in idle or a text editor and enter the shebang line along with a comment describing the purpose of the program: #!/usr/bin/env python # irc channel checker, written for the Æ raspberry pi user guide by tom hudson next, import...

  • Page 185: 173

    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 p y t h o n 173 the first line tells python to create a dict data type . Short for dictionary, this allows multiple variables to be stored in a single master variable—in this case, irc . These individual vari- ables can then be recalled later in the ...

  • Page 186: 174

    R a s p b e r r y p i u s e r g u i d e 174 next, you need to tell the program to try connecting to the irc server specified in the vari- ables at the start of the program . Type the following lines: print ‘connecting to %(host)s:%(port)s...’ % irc try: s.Connect((irc[‘host’], irc[‘port’])) except s...

  • Page 187: 175

    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 p y t h o n 175 receiving data from the socket is a little more complicated . First, you’ll need to create an empty string variable that will act as the receive buffer, holding data from the server as it’s received until it can be processed . Initial...

  • Page 188: 176

    R a s p b e r r y p i u s e r g u i d e 176 sure that only full lines are removed from the read_buffer : because responses from the server are read in 1 kb chunks, it’s likely that at any given time the buffer will contain only fractions of a line . When that’s the case, the fraction is left in the ...

  • Page 189: 177

    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 p y t h o n 177 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: without it, ea...

  • Page 190: 178

    R a s p b e r r y p i u s e r g u i d e 178 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 http://www.Wiley.Com/go/ raspberrypiuserguide . Downloading the source code from the website will save you s...

  • Page 191: Chapter

    Chapter 12 hardware hacking.

  • Page 192: 180

    R a s p b e r r y p i u s e r g u i d e 180 in earlier chapters, you learned how the raspberry pi can be turned into a flexible platform 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 low...

  • Page 193: 181

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 181 ❍ push-buttons—a very common input component, a push-button completes an elec- trical circuit when pushed . At the most basic level, a keyboard is little more than a col- lection of push-buttons . If you’re designing a circuit to provide a simple i...

  • Page 194: 182

    R a s p b e r r y p i u s e r g u i d e 182 ❍ 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 195: 183

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 183 figure 12-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 figure 12-1, equat...

  • Page 196: 184

    R a s p b e r r y p i u s e r g u i d e 184 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 resistance . Sourcing components if you haven’t dabbled in...

  • Page 197: 185

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 185 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 minimum order value...

  • Page 198: 186

    R a s p b e r r y p i u s e r g u i d e 186 hobby specialists in addition to the major chains, there are smaller companies that specialise in working with hobbyists . Although their selection is dwarfed by those of the larger chains, it’s typically hand- picked and comes with the personal recommenda...

  • Page 199: 187

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 187 each pin of the gpio port has its own purpose, with several pins working together to form particular circuits . The layout of the gpio port can be seen in figure 12-2 . Figure 12-2: the raspberry pi’s gpio port and its pin definitions 2 1 4 3 6 5 8...

  • Page 200: 188

    R a s p b e r r y p i u s e r g u i d e 188 creating a circuit that will interface with the pi through its gpio port, make sure you are using components compatible with 3 .3 v logic or are passing the circuit through a voltage regulator before it reaches the pi . Warning connecting a 5 v supply to a...

  • Page 201: 189

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 189 i²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 ...

  • Page 202: 190

    R a s p b e r r y p i u s e r g u i d e 190 may want to investigate a lower-level language such as c++ or even assembler running on a dedicated real-time microcontroller. If true real-time operation is required for your project, the pi may be a bad choice . Instead, consider using a microcontroller ...

  • Page 203: 191

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 191 although the gpio library is now installed in python, it won’t be loaded by default . Like pygame , the library needs to be explicitly imported into your program . To use the library, start your program with import rpi.Gpio as gpio at the top . You...

  • Page 204: 192

    R a s p b e r r y p i u s e r g u i d e 192 gpio output: flashing an led for the first example, you’ll need to build a simple circuit consisting of an led and a resistor . The led will provide visual confirmation that the pi’s gpio port is doing what your python program tells it to do, and the resis...

  • Page 205: 193

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 193 figure 12-3: a breadboard circuit for a simple led output to gpio pin 11 to gpio pin 6 to gpio pin 11 to gpio pin 6 at this point, nothing will happen . That’s perfectly normal: by default, the raspberry pi’s gpio pins are switched off . If you wan...

  • Page 206: 194

    R a s p b e r r y p i u s e r g u i d e 194 before you can use the gpio library you installed earlier in this chapter, you’ll need to import it into your python project . Accordingly, start the file with the following line: import rpi.Gpio as gpio remember that python is case-sensitive, so be sure t...

  • Page 207: 195

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 195 next, add the following lines to switch the pin on, wait 2 seconds, and then switch it off again before waiting another 2 seconds . Make sure each line starts with four spaces, to sig- nify that it is part of the infinite while loop: gpio.Output(11...

  • Page 208: 196

    R a s p b e r r y p i u s e r g u i d e 196 save the file as gpiooutput.Py . If you’re using a python development environment such as spe, don’t try to run the program from within the editor . Most raspberry pi linux distri- butions restrict the use of the gpio port to the root user, so the program ...

  • Page 209: 197

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 197 2 connect a 10 kΩ resistor to the same row as one of the push-button’s legs and an unused row . This is a pull-up resistor, and will provide the pi with a reference voltage so it knows when the button has been pressed . 3 connect the unused leg of ...

  • Page 210: 198

    R a s p b e r r y p i u s e r g u i d e 198 the circuit you just built creates a situation whereby the input pin, which in this instance is pin 12 of the raspberry pi’s gpio port, is constantly high thanks to the pull-up resistor connected to a 3 .3 v supply . When the push-button is pressed, the ci...

  • Page 211: 199

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 199 although the program could be executed now and work, it doesn’t do anything useful . To make sure you know what’s going on, add the following print instruction to get feedback: if input_value == false: print(“the button has been pressed.”) while in...

  • Page 212: 200

    R a s p b e r r y p i u s e r g u i d e 200 figure 12-6: the output of the gpioinput .Py program by extending the code to look for multiple push-buttons, each on an individual gpio pin, you could even create a simple four-button game controller . For example, you could combine the preceding code wit...

  • Page 213: 201

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 201 figure 12-7: 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 certainly used i...

  • Page 214: 202

    R a s p b e r r y p i u s e r g u i d e 202 figure 12-8: 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 215: 203

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 203 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, you will tar...

  • Page 216: 204

    R a s p b e r r y p i u s e r g u i d e 204 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 217: 205

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 205 with your work surface chosen and protected, lay out your equipment . The iron should be placed on the side of your dominant hand, and positioned so that the cable isn’t trailing across your work area . Make sure you can move the iron freely before...

  • Page 218: 206

    R a s p b e r r y p i u s e r g u i d e 206 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 219: 207

    C h a p t e r 1 2 h a r d w a r e h a c k i n g 207 4 as the solder flows, you’ll see it drawn down into the hole in the board . This is an indi- cation that the area is hot enough for a good solder join . If the solder floats, it indicates that the area is not yet hot enough . 5 remove the solder f...

  • Page 221: Chapter

    Chapter 13 add-on boards.

  • Page 222: 210

    R a s p b e r r y p i u s e r g u i d e 210 although you can to use the raspberry pi’s general-purpose input-output (gpio) header directly, as you learned in chapter 12, “hardware hacking”, a much better idea is to use a specialist prototyping board . Prototyping boards are add-on boards designed to...

  • Page 223: 211

    C h a p t e r 1 3 a d d - o n b o a r d s 211 figure 13-1: the ciseco slice of pi the primary advantages over 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 224: 212

    R a s p b e r r y p i u s e r g u i d e 212 figure 13-2: connecting the slice of pi to a breadboard the connections for an xbee wireless interface could also prove useful if you are considering using the pi in an embedded computing project . When combined with a compatible transceiver—such as an xbe...

  • Page 225: 213

    C h a p t e r 1 3 a d d - o n b o a r d s 213 figure 13-3: the slice of pi, connected to the pi’s gpio port table 13 1 slice of pi gpio translations physical pin number official designation slice of pi label 7 general purpose clock (or gpio pin 4) gp7 11 gpio pin 17 gp0 12 gpio pin 18 gp1 13 gpio pi...

  • Page 226: 214

    R a s p b e r r y p i u s e r g u i d e 214 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 in kit form, and offers you a prototyping surface along with easy access to the pi’s gpio pins (s...

  • Page 227: 215

    C h a p t e r 1 3 a d d - o n b o a r d s 215 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 ...

  • Page 228: 216

    R a s p b e r r y p i u s e r g u i d e 216 all 26 of the pi’s gpio header pins—meaning that you could accidentally connect things to the pins marked as do not connect on the gpio header diagram (see chapter 12, “hardware hacking”) . As with connecting things to these pins directly, this is inadvisa...

  • Page 229: 217

    C h a p t e r 1 3 a d d - o n b o a r d s 217 figure 13-6: the pi plate connected to a cased raspberry pi you can purchase the prototyping pi plate kit from adafruit at http://www.Adafruit .Com/products/801 . Fen logic gertboard properly termed the raspberry pi i/o extension, the gertboard (shown in...

  • Page 230: 218

    R a s p b e r r y p i u s e r g u i d e 218 figure 13-7: the gertboard, designed by gert van loo unlike the slice of pi and prototyping pi plate, the gertboard is an active add-on board with numerous components designed to extend the functionality of the pi . The gertboard pro- vides 12 buffered io ...

  • Page 231: 219

    C h a p t e r 1 3 a d d - o n b o a r d s 219 figure 13-8: the gertboard connected to a raspberry pi perhaps the most important feature of the gertboard is the protection it offers to the pi . Using a 3 .3 v regulator, the gertboard is designed to prevent the pi’s gpio port from receiv- ing voltages...

  • Page 232: 220

    R a s p b e r r y p i u s e r g u i d e 220 figure 13-9: using the gertboard motor controller for project building, the gertboard offers significantly more functionality than a bare raspberry pi . The 12 buffered io ports, located at the top of the board, can be configured as inputs or outputs and p...

  • Page 233: 221

    C h a p t e r 1 3 a d d - o n b o a r d s 221 operate . The open collector driver can control the power supplies of up to six devices simultane- ously without requiring any hardware . This gives the gertboard a large amount of flexibility— although transistors or relays can be used with passive add-...

  • Page 234: 222

    R a s p b e r r y p i u s e r g u i d e 222 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 235: Appendix A

    Appendix a python recipes appendix b hdmi display modes part iv appendixes.

  • Page 237: Appendix A

    Appendix a python recipes.

  • Page 238: 226

    R a s p b e r r y p i u s e r g u i d e 226 the following recipes provide the program code for example 3 and example 4 in chapter 11, “an introduction to python”, and a sample solution for the combined input- output program suggested in chapter 12, “hardware hacking” . Each recipe is also available ...

  • Page 239: 227

    A p p e n d i x a p y t h o n r e c i p e s 227 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 240: 228

    R a s p b e r r y p i u s e r g u i d e 228 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 Æ (r...

  • Page 241: 229

    A p p e n d i x a p y t h o n r e c i p e s 229 ‘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 242: 230

    R a s p b e r r y p i u s e r g u i d e 230 gpio input and output (chapter 12) #!/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.Setup(11, gpio.Out) gpio.Setup(12, gpio.In) gpio.Output(11, false) ...

  • Page 243: Appendix B

    Appendix b hdmi display modes.

  • Page 244: 232

    R a s p b e r r y p i u s e r g u i d e 232 you can use the values in table b-1 and table b-2 with the hdmi_mode option in config.Txt to alter the hdmi video output stream . For more information, see chapter 6, “configuring the raspberry pi” . Table b-1 hdmi group 1 (cea) value description 1 vga (64...

  • Page 245: 233

    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 233 value description 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 (pixel quadrupling enabled) 36 480p 60hz (pixel quadrupling ena...

  • Page 246: 234

    R a s p b e r r y p i u s e r g u i d e 234 table b-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×48...

  • Page 247: 235

    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 235 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 248: 236

    R a s p b e r r y p i u s e r g u i d e 236 table b-2 continued value description 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 blanking) 76 2560×1600 (reduce...

  • Page 250: 238

    R a s p b e r r y p i u s e r g u i d e 238 c calc app, openoffice.Org, 117 calculator.Py program, 158–162 cathode-ray tube (crt) tvs, connecting to, 14–15 cathodes, 192 cd command, 30, 156 cellan-jones, rory, 5–6 central processor. See cpu cfdisk partition management tool, 72–73 change ownership (c...

  • Page 251: 239

    I n d e x 239 dmesg command, 60–62 dns (domain name service) servers, 58 domain names (hostnames), 58–59 dotted-quad format, 57 downloading rasbmc distribution installer, 106 raspberry-gpio-python library, 190 draw app, openoffice.Org, 117 dry joints (cold joints), 207 dsi (display serial interface)...

  • Page 252: 240

    R a s p b e r r y p i u s e r g u i d e 240 g -g flag, 36, 106 gameover function, python, 165, 170 gaming with pygame library, 162–171 with scratch, 143–149 general-purpose input-output port. See gpio port gertboard, 217–222 gid (group id), 37 the gimp (gnu image manipulation program), 119–121 gnome...

  • Page 253: 241

    I n d e x 241 hostname variable, python, 173 hostnames (domain names), 58–59 htpc. See home theater pc hudson, tom, 171 hwaddr , ifconfig tool output, 51 i -i flag, 106 i²c (inter-integrated circuit) bus, 180, 189 idle, 32, 153–154 idle 3, 33, 153 ieee 802.11 wireless standards, 65 if block, scratch...

  • Page 254: 242

    R a s p b e r r y p i u s e r g u i d e 242 keyboard and mouse tool, 33 keyboards connecting, 17–18 troubleshooting, 46–47 kgdboc kernel option, cmdline.Txt file, 99 kiloohms, 183 l l2 cache, disabling, 94–95 lamp stack defined, 124 installing, 124–128 lang, jack, 4, 7 leafpad text editor, 32, 152 l...

  • Page 255: 243

    I n d e x 243 monitor (mode), iwconfig command output, 65 monitor settings tool, 33 mosi (spi master output, slave input) signal, 189 motion blocks, scratch, 140, 143 motor controllers, 219–220 mount command, 75 mount points, 74–75 mounting defined, 34 external storage devices, 75 partitions, 74–75 ...

  • Page 256: 244

    R a s p b e r r y p i u s e r g u i d e 244 partition editor tool, parted magic, 78–81 partition table, 73 partitions creating, 72–76 defined, 29 moving to larger sd card, 81–84 resizing, 76–81 $path environment variable, 154, 157 pcmanfm file manager, 32 pen drives (flash drives), 22–23 php, 126–12...

  • Page 257: 245

    I n d e x 245 raspi-config tool, 76–77 raw_input command, python, 159, 160 rca phono connector, 14–15, 17, 49 read_buffer variable, python, 175–176 ready to send/clear to send (rts/cts) handshaking, 65 receive buffer, 175 received network traffic, 52 recursive (-r) flag, 74 remove command, 43 repeat...

  • Page 258: 246

    R a s p b e r r y p i u s e r g u i d e 246 side cutters, 181 slck (serial clock), 189 smalltalk language, 136 smb (server message block) standard, 110 socket module, python, 172–174 socket objects, 173 software settings, 97–100 soic surface, 215 solder, 181, 203–207 solder mask, 203 soldering basic...

  • Page 259: 247

    I n d e x 247 tx-power , iwconfig command output, 65 txqueuelen , ifconfig tool output, 52 u uart (universal asynchronous receiver/ transmitter) serial bus, 92, 188 uid (user id), 37 ums (usb mass storage) devices, 22, 34–35 uninstalling software, 40, 43 universal asynchronous receiver/transmitter (...

  • Page 260: 248

    R a s p b e r r y p i u s e r g u i d e 248 windows flashing sd cards from, 21–22 imaging sd cards from, 83–84 linux versus, 13–14 wired equivalent privacy (wep) encryption, 67–68 wired network tab, rasbmc settings menu, 111–112 wired networking configuring, 56–59 connections, 24–25 wireless network...