cover.eps

Title page image









For Mum & Dad—my best teachers.

Publisher’s Acknowledgements

Some of the people who helped bring this book to market include the following:

 

Editorial
Series Creator: Carrie Anne Philbin
VP Consumer and Technology Publishing Director: Michelle Leete
Associate Director—Book Content Management: Martin Tribe
Professional Technology & Strategy Director: Barry Pruett
Acquisitions Editor: Jody Lefevere
Project Editor: Charlotte Kughen
Technical Editor: Martin O’Hanlon
Editorial Manager: Mary Beth Wakefield
Editorial Assistant: Matthew Lowe
Production Editor: Athiyappan Lalith Kumar

Marketing
Marketing Manager: Lorna Mein
Marketing Assistant: Polly Thomas

About the Author

CARRIE ANNE PHILBIN is an award winning Computing & ICT Teacher, Author & YouTuber. She currently works as Director of Education for the Raspberry Pi Foundation to put the power of computing and digital making into the hands of people all over the world. She also volunteers on the board for Computing At School and as a Director of the Python Software Foundation. Alongside her work in formal education in the UK, Carrie Anne also hosts a Computer Science series on popular educational YouTube channel Crash Course (www.youtube.com/crashcourse) and creates technology tutorials on her own channel the Geek Gurl Diaries (www.geekgurldiaries.co.uk/).

Acknowledgments

I would like to express my deep gratitude to the Raspberry Pi Foundation for allowing me to set my creativity free on their marvellous inventions. In particular I’d like to thank Alex Bradbury, Dr Sam Aaron and Nicholas Tollervey for their enthusiastic encouragement and patient guidance. Their willingness to give their time so generously is very much appreciated. I would also like to thank Martin O’Hanlon and the Raspberry Pi Community for their useful critiques of this work. Thanks to Jennifer Mayberry for her design work and Sarah Wright for much of the art.

Special thanks should also be given to the staff of Pimoroni for providing necessary equipment in order to complete elements of this book, as well as members of CAS #include and the Rainham Library Book Club, for keeping my progress on schedule with their kind words of encouragement. Thanks to my colleagues at Raspberry Pi, PyConUK and the wider community who have been parts of these adventures for many years.

My special thanks are also extended to my good friends—Emma, Sian, Helen, Viv and Kylie—who are a constant source of inspiration in my life.

Finally, I wish to thank my parents, husband, brother and sister-in-law for their patience, support and encouragement throughout.

Adventures in Raspberry Pi®

Introduction

ARE YOU AN intrepid adventurer? Do you like to try new things and learn new skills? Would you like to be a pioneer in creating technology? Do you own a Raspberry Pi, or are you considering getting one? If the answer is a resounding “Yes!” then this is the book for you.

What Is the Raspberry Pi and What Can You Do With It?

The Raspberry Pi is a computer. A very small computer. In fact, it is roughly the size of a credit card. Don’t be fooled by its size; as we know, good things come in small packages. However, the Raspberry Pi does not come in a package at all. It does not come in a case (although you can build one, as discussed in Adventure 1) and its circuit board and chips are fully visible, as you can see in Figure 1. You can plug a Raspberry Pi into a digital TV or monitor and use a USB keyboard and mouse with it, making it very easy to use. Because of its size, you can easily transport it anywhere.

image

FIGURE 1 Raspberry Pi computers.

The Raspberry Pi gives you the opportunity to build and control a device that does what you want it to do. For example, you can deploy your very own robot arm, controlled by a program that you have written. You can design and create your own role-playing game, or produce beautiful computer art or music, all by using code.

Just because the Raspberry Pi is small doesn’t mean you can’t do big things with it. Here are just a few examples of some incredible Pi projects:

In the final adventure of this book, you use your Pi to build a jukebox that plays your favorite tunes and displays track information on an LCD screen. And with the skills you learn throughout the book, you’ll be ready to dream up your own exciting projects—and create them.

Who Should Read This Book?

Adventures in Raspberry Pi is for any young person who has an interest in making things happen using computing. You might perhaps be unsure of how to get started or want to further your current skills. Whatever your reasons, this book will be your guide for a journey with your Raspberry Pi, the most important item in your backpack. Your trek will take you from setting up your Pi, through learning the basics of programming, to discovering how to create your own project. By the end of your adventures you will have acquired the skills you need to become a pioneer of technology!

What You Will Learn

This book will help you discover some of the amazing things you can do with your new Raspberry Pi, and introduce you to many of the developer tools and projects available to you. With this book, you learn how to set up and use your Raspberry Pi easily so that you can experience its potential for yourself. You learn the skills you need to design and create your own computing projects.

You find out that you can give instructions to your Raspberry Pi in a variety of ways, using different programming languages and tools. The adventures in the book allow you to experience programming using Scratch, Turtle Graphics, Python, Sonic Pi and Minecraft Pi.

You also learn some computing (and electronics) concepts that you can apply to other devices and programming situations. Many fundamental computing concepts are similar for all programming languages, so once you understand the basics of programming in one language you can apply that knowledge to others very easily.

What You Need for the Projects

First and foremost, of course, you need a Raspberry Pi. If you don’t already own one, you can buy a Raspberry Pi from a distributor in your country. You also need a monitor or other screen, a mouse and a keyboard to connect to your Raspberry Pi.

Each chapter—adventure—in the book notes any special items you need to build the project covered in that adventure. Along with your Pi, some projects require Internet access to enable you to download software or other materials. You need headphones or speakers to listen to the music you make in Adventure 7. For the projects in Adventures 8 through 10, you need some specific cables, wires, LEDs, resistors and other hardware. You can purchase these items from your local electronics store, or from various online retailers.

As final ingredients, you need some curiosity and a willingness to try new skills!

How This Book Is Organised

Every chapter of the book is a separate adventure, teaching you to use new skills and concepts while you create a project. The book is organised so that as you progress, the concepts and projects get more complex, building on what you learned in earlier adventures. Each chapter begins with an introduction to the language or tool for that adventure, provides instructions for downloading, installing, and setting up whatever you need, and usually gives you a short task to help you become familiar with the tool. After you’ve got the basics, I lead you step by step through the instructions for the main project.

In Adventures 1 and 2, you learn how to get started with your equipment and use common text commands, perhaps for the first time. These two chapters are necessary for the beginner Pi explorer, as further adventures depend on the skills covered here.

The two most common ways to program a Raspberry Pi are to use the Scratch or Python languages that come preinstalled on the Pi’s main operating system, Raspbian. Adventures 3, 4 and 5 get you started with the basics of these languages. In Adventure 3, you use Scratch, a simple drag-and-drop programming language, to design and create your own computer game, while getting an introduction to the programming concepts of loops and variables. Adventure 4 is a bridge between Scratch and the more conventional programming language, Python. In this adventure, you use Turtle Graphics to create shapes and spirals with both programming languages. In Adventure 5, you learn how to create an adventure game program that asks for user input, uses lists, imports functions and prints text to the screen, all using text commands written in the programming language Python.

Adventures 6 and 7 take programming on the Raspberry Pi further by looking at two developer tools that you can download and use with the Raspberry Pi: Minecraft Pi and Sonic Pi. Minecraft Pi enables you to interact with and adapt the popular computer game Minecraft, using Python code to build your own transporter. With Sonic Pi, you can create electronic music by writing programs.

Another exciting aspect of using the Raspberry Pi is that it gives you the option to add on to the main board by using GPIO pins. Adventure 8 looks at the GPIO pins in more detail, introducing you to electronics and computer programming while you build a program that uses a marshmallow to make a light blink (yes, you read that right).

You don’t always need to program individual electronic components; you can also buy specially designed hardware that fits on top of the GPIO pins on the Raspberry Pi, called HATs. In Adventure 9, you learn more about the features of the Sense HAT and the Explorer HAT before programming them with Python.

Adventure 10 draws on the computing concepts and skills learned through completing the preceding adventures in this book to create one big project—a jukebox. In this chapter, you learn how to plan, design and create a project from start to finish.

Finally, the Appendix suggests where you might go next to learn more about the different aspects of computer science and Raspberry Pi—including how to locate or set up your own club to share project ideas with others.

The Companion Website

Throughout this book you’ll find references to the Adventures in Raspberry Pi companion website, www.wiley.com/go/adventuresinrp3E. (It’s a good idea to bookmark that site so you can return to it as you need to.) The website includes video tutorials to help you out if you get stuck, and code files for some of the more extensive projects.

Conventions

Throughout the book, there are some special boxes to guide and support you. They use the following key:

You will also find two sets of sidebars in the book. Challenge sidebars ask you how you might expand on the projects in the book to make changes or add new features. Digging into the Code sidebars explain some of the special syntax or programming language, to give you a better understanding of the computer languages.

When following steps or instructions using code, especially in adventures using Python, you should type in the code as set out by the instructions. Sometimes you need to type a very long line of code, longer than will fit on a single line in this book. If you see a ImageD symbol at the end of a line of code, it means that line and the following line are part of a single code line, so you should type them as one line, not on separate lines. For example the following code should be typed on one line, not two:

print("Welcome to Adventures in Raspberry Pi by ImageD
Carrie Anne Philbin")

Most chapters include a Quick Reference Table at the end to sum up the main commands or concepts from the chapter. You can refer to these guides when you need a refresher on the commands.

Whenever you complete a chapter, you unlock an achievement and collect a new badge. You can collect badges to represent these achievements from the Adventures in Raspberry Pi companion website (www.wiley.com/go/adventuresinrp3E).

Reaching Out

The Appendix explains ways you can take your Raspberry Pi knowledge further, with references to websites, organisations, videos and other resources. Many of those resources include forums where you can ask questions or get in touch with other Raspberry Pi users.

You can also contact me by sending me a message through my website, www.geekgurldiaries.co.uk.

Time to start your adventures!

image

IN THE PAGES of this book you discover how to do great things with your Raspberry Pi. You create art and music, programs, games, even create your own jukebox! But first, you need to get your system working.

If you are new to Raspberry Pi, the initial tasks of getting it set up and running might seem a little daunting, but it is not that complicated to do. By setting up the Raspberry Pi yourself you learn more about how it and other computers work. You will encounter technical jargon and procedures that you may not have come across before. In this chapter, I show you how to set up your Raspberry Pi so it is ready for you to use for the first time. I explain what hardware and software you need, and tell you how to put it all together into a working system. You also find out how to create a backup copy of your system in case you need to replace it at some stage in the future.

What Hardware Do You Need?

Of course, the first thing you need is a Raspberry Pi. If you have used games consoles or computing devices before, you’ll notice something different about Raspberry Pi—it doesn’t come with a power supply, a charger or any connecting cables. It doesn’t have a storage device to keep your programs on either—or even a case!

So, to get started, you first need to get the following hardware together (see Figure 1-1):

image

FIGURE 1-1 The essential hardware you’ll need before you can use your Raspberry Pi.

What Other Equipment Is Helpful?

The following additional accessories are not vital, but you might want to consider acquiring some of them to improve your Raspberry Pi experience.

image

Reproduced by permission of Pimoroni

FIGURE 1-2 The PiBow case can help protect your Raspberry Pi.

image

Reproduced by permission of The Daily Brick

FIGURE 1-3 Build a LEGO case for your Raspberry Pi.