Book Image

Raspberry Pi for Python Programmers Cookbook - Second Edition

Book Image

Raspberry Pi for Python Programmers Cookbook - Second Edition

Overview of this book

Raspberry Pi cookbook for Python Programmers is a practical guide for getting the most out of this little computer. This book begins by guiding you through setting up the Raspberry Pi, performing tasks using Python 3 and introduces the first steps to interface with electronics. As you work through each chapter you will build up your skills and knowledge and apply them as you progress throughout the book, delving further and further into the unique abilities and features of the Raspberry Pi. Later, you will learn how to automate tasks by accessing files, build applications using the popular Tkinter library and create games by controlling graphics on screen. You will harness the power of the built-in graphics processor by using Pi3D to generate your own high quality 3D graphics and environments. Connect directly to the Raspberry Pi’s hardware pins to control electronics from switching on LEDs and responding to push buttons right through to driving motors and servos. Learn how to monitor sensors to gather real life data and to use it to control other devices, and view the results over the Internet. Apply what you have learnt by creating your own Pi-Rover or Pi-Hexipod robots. Finally, we will explore using many of the purpose built add-ons available for the Raspberry Pi, as well as interfacing with common household devices in new ways.
Table of Contents (18 chapters)
Raspberry Pi for Python Programmers Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Hardware and Software List
Index

Introduction


This chapter introduces the Raspberry Pi and the process to set it up for the first time. We will connect the Raspberry Pi to a suitable display, power, and peripherals. We will install an operating system on an SD card. This is required for the system to boot. Next, we will ensure that we can connect successfully to the Internet through a local network.

Finally, we will make use of the network to provide ways to remotely connect to and/or control the Raspberry Pi from other computers and devices, as well as to ensure that the system is kept up to date.

Once you have completed the steps within this chapter, your Raspberry Pi will be ready for you to use for programming. If you already have your Raspberry Pi set up and running, ensure that you take a look through the following sections as there are many helpful tips.

Introducing the Raspberry Pi

The Raspberry Pi is a single-board computer created by the Raspberry Pi Foundation, a charity formed with the primary purpose of reintroducing low-level computer skills to children in the UK. The aim was to rekindle the microcomputer revolution of the 1980s, which produced a whole generation of skilled programmers.

Even before the computer was released at the end of February 2012, it was clear that the Raspberry Pi had gained a huge following worldwide and, at the time of writing this book, has sold over 10 million units. The following image shows several different Raspberry Pi Models:

Raspberry Pi Model 3B, Model A+, and Pi Zero

What is with the name?

The name, Raspberry Pi, was the combination of the desire to create an alternative fruit-based computer (such as Apple, BlackBerry, and Apricot) and a nod to the original concept of a simple computer that can be programmed using Python (shortened to Pi).

In this book, we will take this little computer, find out how to set it up, and then explore its capabilities chapter by chapter using the Python programming language.

Why Python?

It is often asked, "Why has Python been selected as the language to use on the Raspberry Pi?" The fact is that Python is just one of the many programming languages that can be used on the Raspberry Pi.

There are many programming languages that you can choose, from high-level graphical block programming, such as Scratch, to traditional C, right down to BASIC, and even raw Machine Code Assembler. A good programmer often has to be code multilingual to be able to play to the strengths and weaknesses of each language in order to best meet the needs of their desired application. It is useful to understand how different languages (and programming techniques) try to overcome the challenge of converting "what you want" into "what you get" as this is what you are trying to do as well while you program.

Python has been selected as a good place to start when learning about programming, by providing a rich set of coding tools while still allowing simple programs to be written without fuss. This allows beginners to gradually be introduced to the concepts and methods on which modern programming languages are based without requiring them to know it all from the start. It is very modular with lots of additional libraries that can be imported to quickly extend the functionality. You will find that over time, this encourages you to do the same, and you will want to create your own modules that you can plug into your own programs, thus taking your first steps into structured programming.

Like all programming languages, Python isn't perfect; things such as adding a space at the start of a line will often break your code (indents matter a lot in Python; they define how blocks of code are grouped together). Generally, Python is slow; since it is interpreted, it takes time to create a module while it is running the program. This can be a problem if you need to respond to time critical events. However, you can precompile Python or use modules written in other languages to overcome this. It hides the details; this is both an advantage and disadvantage. It is excellent for beginners but can be difficult when you have to second-guess aspects such as data-types. However, this in turn forces you to consider all the possibilities, which can be a good thing.

Python 2 and Python 3

A massive source of confusion for beginners is that there are two versions of Python on the Raspberry Pi (Version 2.7 and Version 3.4), which are not compatible with one another, so code written for Python 2.7 may not run with Python 3.4 (and vice versa).

The Python Software Foundation is continuously working to improve and move forward with the language, which sometimes means they have to sacrifice backward compatibility in order to embrace new improvements (and importantly, remove redundant and legacy ways of doing things).

Note

Supporting both Python 2 or Python 3

There are many tools that will ease the transition from Python 2 to Python 3, including converters such as 2to3, which will parse and update your code to use Python 3 methods. This process is not perfect, and in some cases, you'll need to manually rewrite sections and fully retest everything. You can write the code and libraries that will support both. The import __future__ statement allows you to import the friendly methods of Python 3 and run them using Python 2.7.

Which version of Python should you use?

Essentially, the selection of which version to use will depend on what you intend to do. For instance, you may require Python 2.7 libraries, which are not yet available for Python 3.4. Python 3 has been available since 2008, so these tend to be older or larger libraries that have not been translated. In many cases, there are new alternatives to legacy libraries; however, their support can vary.

In this book, we have used Python 3.4, which is also compatible with Python 3.3 and 3.2.

The Raspberry Pi family – a brief history of Pi

Since its release, the Raspberry Pi has come in various iterations, featuring both small and large updates and improvements to the original Raspberry Pi Model B unit. Although it can be confusing at first, there are three basic types of Raspberry Pi available (and one special model).

The main flagship model is called Model B . This has all the connections and features, as well as the maximum RAM and the latest processor. Over the years, there have been several versions, most notably Model B (which had 256 MB and then 512 MB RAM) and then Model B+ (which increased the 26-pin GPIO to 40 pins, switched to using a micro SD card slot, and had four USB ports instead of two). These original models all used the Broadcom BCM2835 SOC (short for System On Chip), consisting of a single core 700 MHz ARM11 and VideoCore IV GPU (short for Graphical Processing Unit).

The release of the Raspberry Pi 2 Model B (also referred to as 2B) in 2015 introduced a new Broadcom BCM2836 SOC, providing a quad-core 32-bit ARM Cortex A7 1.2 GHz processor and GPU, with 1 GB of RAM. The improved SOC added support for Ubuntu and Windows 10 IoT. Finally we had the latest Raspberry Pi 3 Model B, using another new Broadcom BCM2837 SOC, which provides a quad-core 64-bit ARM Cortex-A53 and GPU, alongside adding on-board Wi-Fi and Bluetooth.

Model A has always been targeted as a cut-down version. While having the same SOC as Model B, there are limited connections consisting of a single USB port and no wired network (LAN). Model A+ again added more GPIO pins and a micro SD slot. However, the RAM was later upgraded to 512 MB of RAM and again only a single USB port/no LAN. The Broadcom BCM2835 SOC on Model A has not been updated so far (so is still a single core ARM11); however, a Model 3A (most likely using the BCM2837) is expected 2016/2017.

The Pi Zero is an ultra-compact version of the Raspberry Pi intended for embedded applications where cost and space are a premium. It has the same 40-pin GPIO and micro SD card slot as the other models, but lacks the on-board display (CSI and DSI) connection. It does still have HDMI (via a mini-HDMI) and a single micro USB OTG (on-the-go) connection. Although not present in the first revision of the Pi Zero, the most recent model also includes a CSI connection for the on-board camera.

Note

The Pi Zero was famously released in 2015 by being given away with the Raspberry Pi foundations magazine The MagPi, giving the magazine the kudos of being the first magazine to give away a computer on its cover! This did make me rather proud since (as you may have read in my biography at the start of this book) I was one of the founders of the magazine.

The special model is known as the compute module. This takes the form of a 200-pin SO-DIMM card. It is intended for industrial use or within commercial products, where all the external interfaces would be provided by a host/motherboard, into which the module would be inserted. Example products include the Slice media player (http://fiveninjas.com) and the Otto camera. The current module uses the BCM2835, although an updated compute module (CM3) is expected in 2016.

The Raspberry Pi Wikipedia page provides a full list of the all different variants and their specifications:

https://en.wikipedia.org/wiki/Raspberry_Pi#Specifications

Which Pi to choose?

All sections of this book are compatible will all current versions of the Raspberry Pi, but Model 3B is recommended as the best model to start with. This offers the best performance (particularly useful for the GPU examples in Chapter 5, Creating 3D Graphics, and the OpenCV examples used in Chapter 8, Creating Projects with the Raspberry Pi Camera Module), lots of connections, and built-in Wi-Fi, which can be very convenient.

The Pi Zero is recommended for projects where you want low power usage or reduced weight/size but do not need the full processing power of Model 3B. However, due to its ultra-low cost, the Pi Zero is ideal for deploying a completed project after you have developed it.