Innovate with Sense HAT for Raspberry Pi (Extract)

Page 1

Innovate with Sense HAT for Raspberry Pi

Exploring with Sense HAT for Raspberry Pi includes projects featuring external hardware components in addition to the Sense HAT board. You will learn to connect the Sense HAT board to the Raspberry Pi using jumper wires so that some of the GPIO ports are free to be interfaced to external components, such as to buzzers, relays, LEDs, LCDs, motors, and other sensors. The book includes full program listings and detailed project descriptions. Complete circuit diagrams of the projects using external components are given where necessary. All the projects were developed using the latest version of the Python 3 programming language. You can easily download projects from the book’s web page. Let’s start exploring with Sense HAT.

Innovate with Sense HAT for Raspberry Pi

Innovate with Sense HAT for Raspberry Pi

Dr. Dogan Ibrahim holds a BSc in Electronic Engineering, an MSc in Automatic Control Engineering, and a PhD in Digital Signal Processing. He worked in many industrial organizations before returning to academia. Professor Ibrahim is the author of more 60 technical books and more than 200 technical articles on microcontrollers, microprocessors, and related fields. He is a Chartered Electrical Engineer and a Fellow at the Institution of Engineering Technology.

The Sense HAT contains several helpful environmental sensors: temperature, humidity, pressure, accelerometer, magnetometer, and gyroscope. Additionally, an 8x8 LED matrix is provided with RGB LEDs, which can be used to display multi-color scrolling or fixed information, such as the sensor data. Use the small onboard joystick for games or applications that require user input. In Innovate with Sense HAT for Raspberry Pi, Dr. Dogan Ibrahim explains how to use the Sense HAT in Raspberry Pi Zero W-based projects. Using simple terms, he details how to incorporate the Sense HAT board in interesting visual and sensor-based projects. You can complete all the projects with other Raspberry Pi models without any modifications.

45 Sensor Projects in Python

Ready to explore the world around you? By attaching the Sense HAT to your Raspberry Pi, you can quickly and easily develop a variety of creative applications, useful experiments, and exciting games.

Dogan Ibrahim

45 Sensor Projects in Python

ISBN 978-1-907920-90-5

Elektor International Media

Dogan Ibrahim

www.elektor.com

LEARN DESIGN SHARE

LEARN DESIGN SHARE

LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● RN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR SIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN RN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR

Cover Using Sense HAT With Raspberry Pi UK 200512.indd Alle pagina's

14-05-20 09:41



Innovate with Sense HAT for Raspberry Pi 45 Sensor Projects in Python

â—? Dogan Ibrahim

LEARN DESIGN SHARE

Using Sense HAT With Raspberry Pi UK 200430.indd 3

14-05-20 09:32


This is an Elektor Publication. Elektor is the media brand of

Elektor International Media B.V. 78 York Street, London W1H 1DP, UK Phone: (+44) (0)20 7692 8344

All rights reserved. No part of this book may be reproduced in any material form, including

photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally to some other sue of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to reproduce any part of the publication should be addressed to the publishers.

Declaration

The author and publisher have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, or hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause..

British Library Cataloguing in Publication Data

ISBN 978-1-907920-90-5

A catalogue record for this book is available from the British Library

© Copyright 2020: Elektor International Media b.v. Prepress Production: D-Vision, Julian van den Berg First published in the United Kingdom 2020

Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com

LEARN DESIGN SHARE

Using Sense HAT With Raspberry Pi UK 200430.indd 4

14-05-20 09:32


Content Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 1 • Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.1 The Raspberry Pi Zero W . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 2 • Installing the operating system on Raspberry Pi . . . . . . . . . . . . . . . . 13 2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2 Raspbian Buster installation steps on Raspberry Pi Zero W . . . . . . . . . . . . . . . . . 13 2.3 Remote access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.4 Using the Putty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.5 Remote Access of the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.6 Using the Python programming language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Chapter 3 • Introduction to Sense HAT and simple projects . . . . . . . . . . . . . . . . . 25 3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2 Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.3 Programming Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.4 Project 1 – Displaying text on Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.5 Project 2 – Generate a dice numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.6 Project 3 – Generate two dice numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.7 Project 4 – Random letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.8 Project 5 – Display the current time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.9 Project 6 – Test your maths skills - multiplication . . . . . . . . . . . . . . . . . . . . . . . . 33 3.10 Project 7 – Test your maths skills – using all four operators . . . . . . . . . . . . . . . . 35 3.11 Project 8 – Learning the times tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.12 Project 9 – Displaying images on Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.13 Project 10 – Displaying a Christmas tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.14 Project 11 – Rotating the Christmas tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.15 Reading the pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.16 Loading an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.17 Project 12 – Displaying two-digit integer numbers . . . . . . . . . . . . . . . . . . . . . . 45 3.18 Project 13 – Up counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.19 Using the joystick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

●5

Using Sense HAT With Raspberry Pi UK 200430.indd 5

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi 3.20 Project 14 – Joystick control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.21 Project 15 – Event counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.22 Project 16 – Reaction timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.23 Project 17 – Joystick LED control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.24 Reading the temperature, pressure, and humidity . . . . . . . . . . . . . . . . . . . . . . . 64 3.25 Project 18 – Display the temperature, humidity, and pressure . . . . . . . . . . . . . . 65 3.26 Project 19 – Joystick controlled selection of temperature, humidity, and pressure display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 3.27 Project 20 – Calibrating the temperature readings . . . . . . . . . . . . . . . . . . . . . . 69 3.28 Project 21 – Weather report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.29 Project 22 – Display of temperature by LED count . . . . . . . . . . . . . . . . . . . . . . 72 3.30 Project 23 – Display of temperature as LED-based decimal number . . . . . . . . . . 75 3.31 Project 24 – Display temperature and humidity on non-scrolling display . . . . . . . 78 3.33 Project 26 – Flashing LED lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 3.34 The inertial measurement sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.34.1 Reading the compass direction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.34.2 Project 27 - Display the compass direction . . . . . . . . . . . . . . . . . . . . . . . . 82 3.34.3 Reading the acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.34.4 Project 28 – Accelerometer-based dice . . . . . . . . . . . . . . . . . . . . . . . . . . 86 3.34.5 Reading the orientation (pitch, roll, yaw) . . . . . . . . . . . . . . . . . . . . . . . . . 87 3.34.6 Project 29 – Accelerometer-based LED shapes . . . . . . . . . . . . . . . . . . . . . 89 3.35 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 3.36 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Chapter 4 • Using the Sense HAT emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.2 Web-based Sense HAT emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.3 Emulator on the Raspberry Pi desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4.4 Recording and playing back sensor readings . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Chapter 5 • Using Node-RED with Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

●6

Using Sense HAT With Raspberry Pi UK 200430.indd 6

14-05-20 09:32


5.2 Node-RED Sense HAT nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5.3 Project 1 - Displaying the temperature, humidity, and pressure (environmental events) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.4 Project 2 - Displaying the compass heading (motion events) . . . . . . . . . . . . . . . 103 5.5 Project 3 - Displaying the acceleration (motion events) . . . . . . . . . . . . . . . . . . . 104 5.6 Using the joystick with Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 5.7 Using the LED matrix with Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 5.8 Project 4 – Random flashing LED lights having random colours . . . . . . . . . . . . . 109 5.9 Displaying and scrolling data on the LED matrix . . . . . . . . . . . . . . . . . . . . . . . . 110 5.10 Project 5 – Scrolling the pressure readings on the LED matrix . . . . . . . . . . . . . 111 5.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Chapter 6 • Using external components with Sense HAT . . . . . . . . . . . . . . . . . . . 113 6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 6.2 Raspberry Pi Zero W pin configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 6.3 Sense HAT interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 6.4 Project 1 – ON-OFF temperature controller . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 6.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Chapter 7 • Intermediate level Sense HAT based Raspberry Pi projects . . . . . . . 119 7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 7.2 Project 1 – Event counter with an external button . . . . . . . . . . . . . . . . . . . . . . 119 7.3 Project 2 – Reaction timer with an external button . . . . . . . . . . . . . . . . . . . . . . 121 7.4 Project 3 – Displaying the temperature on LCD . . . . . . . . . . . . . . . . . . . . . . . . 122 7.5 Project 4 – Displaying the temperature, humidity, and pressure on LCD . . . . . . . 126 7.6 Project 5 – Displaying the temperature as a bar chart . . . . . . . . . . . . . . . . . . . . 127 7.7 Project 6 – Displaying the temperature, humidity, and pressure as bar charts . . . 130 7.8 Project 7 – Displaying the temperature history . . . . . . . . . . . . . . . . . . . . . . . . . 132 7.9 Project 8 – Displaying random dice images . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 7.10 Project 9 – Ultrasonic car parking aid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 7.11 Plotting graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 7.11.1 Graph of a quadratic function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

●7

Using Sense HAT With Raspberry Pi UK 200430.indd 7

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi 7.11.2 Drawing multiple graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 7.12 Python file processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 7.13 Project 10 – Saving and plotting the temperature data . . . . . . . . . . . . . . . . . . 149 7.14 Project 11 – Saving and plotting the temperature and humidity data . . . . . . . . 151 7.15 Project 12 – Plotting the temperature and humidity data in real-time . . . . . . . . 154 7.16 Project 13 – Plotting the temperature in real-time with time stamping . . . . . . . 156 7.17 Internet communication protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 7.17.1 Project 14 – S ending the temperature, humidity, and pressure data to a mobile phone – using Wi-Fi . . . . . . . . . . . . . . . . . . . . . 159 7.17.2 Project 15 – S ending the temperature, humidity, and pressure data to a mobile phone – using Bluetooth . . . . . . . . 161 7.18 Project 16 – Sending the temperature, humidity, and pressure data to the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 7.19 Games with the Sense HAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 7.19.1 Project 17 – Diagonally moving ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 7.19.2 Project 18 – Pong game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 7.20 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 7.21 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

●8

Using Sense HAT With Raspberry Pi UK 200430.indd 8

14-05-20 09:32


Preface

Preface The Raspberry Pi is a credit-card sized computer that can be used in many applications, such as in audio and video media centers, as a desktop computer, in industrial controllers, robotics, and many domestic and commercial applications. In addition to its many features, the Raspberry Pi also has Wi-Fi and Bluetooth capability, making it highly desirable for remote and internet-based control and monitoring applications. Sense HAT is an add-on board for Raspberry Pi which can be plugged-in to the 40-way connector of the Raspberry Pi. The Sense HAT contains several useful environmental sensors, such as temperature, humidity, pressure, accelerometer, magnetometer, and gyroscope. Additionally, an 8 x 8 LED matrix is provided with RGB LEDs which can be used to display multi-colour scrolling or fixed information, such as the sensor data. A small joystick is provided on the board that can be used in-game programs or in other applications where input from the user may be required. Sense HAT can be used with all models of the Raspberry Pi. This book is about using the Sense HAT multi-sensor and display board in Raspberry Pi Zero W based projects. The book explains in simple terms and with tested and working example projects, how to use the Sense HAT board in interesting visual and sensor-based projects. The book starts with an introduction to the Sense HAT board and covers many projects using this board with the Raspberry Pi Zero W computer. Although the projects are based on the Raspberry Pi Zero W, they can all be implemented on other Raspberry Pi models without any modifications. One unique feature of this book is the development of projects using external hardware components in addition to the Sense HAT board. The book explains in detail how to connect the Sense HAT board to the Raspberry Pi using jumper wires so that some of the GPIO ports are free and can be interfaced to external components, such as buzzers, relays, LCDs, motors, other sensors, etc. Full program listings of all the projects are given in the book together with a full description of each project. All projects in the book have been developed using the latest version of the Python 3 programming language, although they will work with the earlier Python 2 as well. Readers can download the projects from the webpage of the book. I hope the readers find the book helpful and enjoy reading it. Prof. Dr. Dogan Ibrahim January 2020 London.

â—?9

Using Sense HAT With Raspberry Pi UK 200430.indd 9

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

Chapter 1 • Sense HAT The Astro Pi is a small computer enclosed in a special case, which has various sensors and two cameras (infrared and visible light camera) used to collect environmental data onboard the International Space Station (ISS). Sense HAT is the main component, and the LED matrix the only form of visual output. Sense HAT is a small plug-in board developed by the Raspberry Pi Foundation in collaboration with the UK Space Agency and the European Space Agency (ESA). The board includes several sensors, hence its name "Sense". The word "HAT" stands for "Hardware Attached on Top" to indicate that the board is attached or plugged in on top of the Raspberry Pi. Sense HAT gives the flexibility to carry out various environmental measurements using its built-in sensors. The board was specially developed for the Astro Pi challenge and competition. An emulator-based version of Sense HAT is also available, to enable students to carry out experiments without needing the physical board. Each year, students aged up to 19 from across Europe are encouraged to write code for astronauts to run on the Astro Pis, the aim being to inspire young people to have an interest in space science and coding. The Astro Pi challenge consists of two phases, as explained below: Mission Zero, and Mission Space Lab. Mission Zero This is aimed for students up to 14 years old, who are expected to participate in groups of two to four. The students are given the chance to have their code run on the ISS. For example, participants can write codes to display the temperature on the LED matrix so that the astronauts can see as part of their daily tasks. It is guaranteed that all entries to the competition that follow the challenge rules will run in space for the astronauts to see. Students working in teams are expected to write Python programs that show a message and the air temperature for the ISS astronauts on the LED matrix. No extra hardware is needed and everything can be done using the Sense HAT emulator. The challenge started on 12 September 2019 and ends on 20 March 2020. The flight status will be confirmed in May 2020, and the certificates to be delivered to the teams in May/ June 2020. Mission Zero guidelines are available from the following website: https://astro-pi.org/wp-content/uploads/2019/09/Astro_Pi_Mission_Zero_ Guidelines_2019_20_English.pdf Mission Space Lab This lab is aimed for groups of two to six students aged 11-19, to give them a chance to have their codes run on the ISS. The participants are asked to develop codes and report their results. Accepted experiments will be deployed by the ISS and ten teams with the best reports will be announced as winners. Mission Space Lab has four phases, including design,

â—? 10

Using Sense HAT With Raspberry Pi UK 200430.indd 10

14-05-20 09:32


Chapter 1 • Sense HAT

creating, deploying, and analysing the results by writing a report. Mission Space Lab guidelines are available at the following website: https://astro-pi.org/missions/space-lab/ This book is about developing projects using the Sense HAT board together with the Raspberry Pi computer. The Raspberry Pi Zero W is chosen as the host computer in this book since it is relatively cheap, widely available, and ideal for running Sense HAT projects. All projects given in this book will also run on the different models of the Raspberry Pi without any modification. In the next section, we will briefly remind ourselves of the basic features of the Raspberry Pi Zero W computer. 1.1 The Raspberry Pi Zero W The Raspberry Pi Zero W is the smallest model of the Raspberry Pi family of computers. Launched at the end of February 2017, Raspberry Pi Zero W is very similar to the Raspberry Pi Zero, but it includes wireless LAN and Bluetooth connectivity on the board. Figure 1.1 shows a picture of the Raspberry Pi Zero W.

Figure 1.1 Raspberry Pi Zero W The basic features of the Raspberry Pi Zero W are: • 1 GHz single-core processor • 512 MB DDR RAM @ 400 MHz • Mini HDMI port • Micro USB power port • Micro USB data port • 40-pin standard connector • 802.11 b/g/n wireless LAN • Bluetooth 4.1 • Bluetooth Low Energy (BLE) • CSI camera interface

● 11

Using Sense HAT With Raspberry Pi UK 200430.indd 11

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

• Composite video and reset headers • Micro SD card slot for the operating system • UART, SPI, I2C, GPIO • Small size: 65 x 30 x 5 mm • Raspberry Pi Zero W is powered from a 5 V, 2 A power supply. In the next chapter, we will see how to load the latest Raspbian operating system onto the micro SD card ready for the Raspberry Pi Zero W.

● 12

Using Sense HAT With Raspberry Pi UK 200430.indd 12

14-05-20 09:32


Chapter 2 • Installing the operating system on Raspberry Pi

Chapter 2 • Installing the operating system on Raspberry Pi 2.1 Overview The Sense HAT board is compatible with all models of the Raspberry Pi. In this book, we will use the Raspberry Pi Zero W which is a cheap model with a small footprint, but it includes Wi-Fi and a reasonable amount of memory. This model does not have USB ports or Ethernet port, which makes it difficult to start using the Raspberry Pi Zero W. In this chapter we will learn how to install the latest operating system (Raspbian Buster) on the Raspberry Pi Zero W, and also learn the different ways that the Python programming language can be used with this processor. 2.2 Raspbian Buster installation steps on Raspberry Pi Zero W Raspbian Buster is the latest operating system of the Raspberry Pi. This section gives the steps for installing this operating system on a new blank SD card, ready to use with your Raspberry Pi Zero W. You will need a micro SD card with a capacity of at least 8 GB (16 GB is even better) to install the new operating system on. The steps to install the Raspbian Buster are as follows: • Download the Buster image to a folder on your PC (e.g. C:\RPIBuster) from the following link by clicking the Download ZIP under section Raspbian Buster with desktop and recommended software (see Figure 2.1). At the time of writing this book the file is called 2020-02-13-raspbian-buster-full.img. You may have to use the Windows 7Zip software to unzip the download since some of the features are not supported by older unzip software. https://www.raspberrypi.org/downloads/raspbian/

Figure 2.1 Raspbian Buster download page

● 13

Using Sense HAT With Raspberry Pi UK 200430.indd 13

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

• Put your blank micro SD card into the card slot on your computer. You may need to use an adapter to do this. • Download the Etcher program to your PC to flash the disk image. The link is (see Figure 2.2): https://www.balena.io/etcher/

Figure 2.2 Download Etcher • Double click to Open Etcher, and click Select image. Select the Raspbian Buster file you just downloaded and unzipped. • Click Select target and select the micro SD card. • Click Flash (see Figure 2.3). This may take several minutes, wait until it is finished. The program will then validate and unmount the micro SD card. You can remove your micro SD card after it is unmounted.

● 14

Using Sense HAT With Raspberry Pi UK 200430.indd 14

14-05-20 09:32


Chapter 2 • Installing the operating system on Raspberry Pi

Figure 2.3 Click Flash to flash the disk image Your micro SD card has now been loaded with the Raspberry Pi operating system. But before we use this card on our Raspberry Pi Zero W, we have to configure the SD card so that the Wi-Fi and SSH are enabled when the Raspberry Pi is started. This way, we can log in using remote terminal software such as Putty. The steps are as follows: • Install the Nodepad++ software to your PC from the following website: https://notepad-plus-plus.org/downloads/v7.8.5/ • Insert the SD card back to your PC and start the Notepad++ software. • Click Edit -> EOL Conversion -> UNIX/OSX Format. • Enter the following statements into the blank file (replace the MySSID and MyPassword with the details of your own Wi-Fi router):

country=GB

update_config=1 ctrl_interface=/var/run/wpa_supplicant network={

scan_ssid=1

ssid="MySSID"

psk="MyPassword"

}

• Copy the file (save) to the boot folder on your SD card with the name wpa_supplicant.conf. In Windows, this is the only folder you will see which contains items like loader.bin, start.elf, kernel.img, etc.

● 15

Using Sense HAT With Raspberry Pi UK 200430.indd 15

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

• Create a new empty file with the Notepad++ and save it in the boot folder of the SD card with the name ssh. where this file will enable the SSH to be used with your Raspberry Pi Zero W. • Remove the SD card from your PC and insert it into your Raspberry Pi Zero W. • Power up the Raspberry Pi Zero W. Before logging in using the Putty terminal software, we have to know the wireless IP address of our Raspberry Pi Zero W. There are several ways that we can find the IP address of our Raspberry Pi. Perhaps the easiest way is to look at the devices connected to our Wi-Fi router by accessing the router from our PC. You can also get the IP address of your Raspberry Pi using your mobile phone. There are several apps free of charge that you can install on your mobile phone that will show you the IP addresses of all the devices connected to your router. In this section, the Android app called Who's On My Wi-Fi – Network Scanner by Magdalm was used to show the IP address of the Raspberry Pi used by the author. Running this program will display the Raspberry Pi Wireless IP address under the heading Raspberry Pi Trading Ltd. In addition to the IP address, other parameters such as the MAC address, gateway address, IP mask, etc. are all displayed by this app. • After knowing the IP address of our Raspberry Pi Zero W, we can log in using the Putty software (see next section) with the following default username and password: username: pi password: raspberry • After logging in you are advised to change your password for security reasons. You should also run the sudo raspi-config from the command line to enable the VNC, I2C, and SPI as they are useful interface tools that can be used in your future GPIO based work. 2.3 Remote access It is much easier to access the Raspberry Pi remotely over the internet, for example using a PC rather than connecting a keyboard, mouse, and display to it. Before being able to access the Raspberry Pi remotely, we have to enable the SSH by entering the following command at a terminal session (if you have followed the steps in Section 2.2 then the SSH is already enabled and you can skip the following command):

pi$raspberrypi:~ $ sudo raspi-config

Go to the configuration menu and select Interface Options. Go down to P2 SSH (see Figure 2.4) and enable SSH. Click Finish to exit the menu.

● 16

Using Sense HAT With Raspberry Pi UK 200430.indd 16

14-05-20 09:32


Chapter 2 • Installing the operating system on Raspberry Pi

Figure 2.4 Enable SSH You should also enable VNC so that the Raspberry Pi Desktop can be accessed graphically over the internet. This can be done by entering the following command at a terminal session:

pi$raspberrypi:~ $ sudo raspi-config

Go to the configuration menu and select Interface Options. Go down to P3 VNC and enable VNC. Click Finish to exit the menu. At this stage, you may want to shut down your Raspberry Pi by clicking the Applications Menu on Desktop and selecting the Shutdown option. 2.4 Using the Putty Putty is a communications program that is used to create a connection between your PC and the Raspberry Pi. This connection uses a secure protocol called SSH (Secure Shell). Putty does not need to be installed; you can simply store it in any folder of your choice and run it from there. Putty can be downloaded from the following website:

https://www.putty.org/

Simply double click to run it and the Putty startup screen will be displayed. Click SSH and enter the Raspberry Pi IP address, then click Open (see Figure 2.5). The message shown in Figure 2.6 will be displayed the first time you access the Raspberry Pi. Click Yes to accept this security alert.

â—? 17

Using Sense HAT With Raspberry Pi UK 200430.indd 17

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

Figure 2.5 Putty startup screen

Figure 2.6 Click Yes to accept You will be prompted to enter the username and password. Notice that the default username and password are:

username: password:

pi raspberry

You now have a terminal connection with the Raspberry Pi and you can type in commands, including the sudo commands. You can use the cursor keys to scroll up and down through the commands you've previously entered in the same session. You can also run programs, although not graphical programs.

â—? 18

Using Sense HAT With Raspberry Pi UK 200430.indd 18

14-05-20 09:32


Chapter 2 • Installing the operating system on Raspberry Pi

2.5 Remote Access of the Desktop You can control your Raspberry Pi via Putty, and run programs on it from your Windows PC. This, however, will not work with graphical programs because Windows doesn't know how to represent the display. As a result, for example, we cannot run any graphical programs in the Desktop mode. We can get around this problem using some extra software. Two popular programs used for this purpose are VNC (Virtual Network Connection), and Xming. Here, we shall be learning how to use the VNC. Installing and using VNC VNC consists of two parts: VNC Server and the VNC Viewer. VNC Server runs on the Raspberry Pi, and the VNC Viewer runs on the PC. VNC server is already installed on your Raspberry Pi and is enabled as described in Section 2.3. The steps to install and use the VNC Viewer onto your PC are given below: • There are many VNC Viewers available, but the recommended one is the TightVNC which can be downloaded from the following website: https://www.tightvnc.com/download.php • Download and install the TightVNC software for your PC. You will have to choose a password during the installation. • Start the TightVNC Viewer on your PC and enter the Raspberry Pi IP address (see Figure 2.7) followed by :1. Click Connect to connect to your Raspberry Pi.

Figure 2.7 Start the TightVNC and enter the IP address

● 19

Using Sense HAT With Raspberry Pi UK 200430.indd 19

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

Figure 2.8 shows the Raspberry Pi Desktop displayed on the PC screen.

Figure 2.8 Raspberry Pi Desktop on the PC screen 2.6 Using the Python programming language At the time of writing this book, there are two versions of the Python programming language: Python 2.7 and Python 3.x. Although Python 2.7 is still used by many programmers, it is not supported any longer. There are only a few changes between the two versions. All the programs in this book are based on Python 3.x, which is the latest version. It is assumed in this book that the reader is familiar with the Python programming language and has developed and run Python programs in the past. Many references, tutorials, and example programs on the Python programming language can be found on the internet. Python programs can be written and run in three different ways: interactively, using an editor in the command line, and using the Thonny program in Desktop. Using Python interactively Python can be used interactively, where the program statements are entered after Python is started. To start the Python, simply enter Python3 while in the command line. This method can only be used for very small programs, such as to test a small code or a function. An example is shown in Figure 2.9, where the area of a rectangle with 5 and 6 unit sides is calculated.

â—? 20

Using Sense HAT With Raspberry Pi UK 200430.indd 20

14-05-20 09:32


Chapter 2 • Installing the operating system on Raspberry Pi

Figure 2.9 Using Python interactively Enter Cntrl+Z to exit from Python. Using an editor in the command line The second way in which a Python program can be written and run is to use a text editor to create the program and then issue a command to run the program. One of the popular text editors in Raspberry Pi is called nano. The steps to use the nano editor to create a Python program to calculate the area of the rectangle given earlier are as follows (characters entered by the user are in bold for clarity): • Using the nano text editor, create a new file called rectangle.py. Note that Python programs must have the extensions .py. pi@raspberrypi:~ $ nano rectangle.py • Enter the following statements inside the blank file (see Figure 2.10): a=5 b=6 area = a * b print(area) • Enter Cntrl+X, followed by Y, and then press Enter to save the program. You should now have a new file called rectangle.py in your default folder (/home/ pi). This can be checked by entering the command ls.

Figure 2.10 A very simple Python program created using the nano text editor

● 21

Using Sense HAT With Raspberry Pi UK 200430.indd 21

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

The program is run by entering python3, followed by the full filename (see Figure 2.11):

pi@raspberrypi:~ $ python3 rectangle.py

Figure 2.11 Running the Python program The nano text editor includes a number of useful functions to help edit a file, such as cutting and pasting text, replacing text, searching text, help, etc. Interested readers should enter Cntrl+G to display help on how to use the nano. Using the Thonny Thonny is the Python 3 integrated development environment (IDE), available only for Python 3. Thonny is accessed from the Desktop. The steps to use Thonny for the same rectangle problem are as follows: • Start the Desktop on your Raspberry Pi (assuming that you normally login remotely using the Putty) by double-clicking the VNC Viewer on your PC. Enter the username and password for the VNC Viewer. You should see the Raspberry Pi Desktop displayed on your PC screen. • Click the applications menu at the top left corner of the display. • Select Programming -> Thonny Python IDE to start the Thonny as shown in Figure 2.12.

Figure 2.12 Start the Thonny The Thonny startup screen will be displayed on your PC screen. Enter the program statements as shown in Figure 2.13. The screen is in two parts: user programs are written in the upper part; the results of the run are displayed by the shell in the lower part of the program.

● 22

Using Sense HAT With Raspberry Pi UK 200430.indd 22

14-05-20 09:32


Chapter 2 • Installing the operating system on Raspberry Pi

Figure 2.13 Enter the program statements • Click Save and give a name to your program. e.g. rectangle2 (you do not have to enter the file extension). You should now see the name of the file, rectangle. py, displayed at the top left corner of Thonny. • Click Run to run the program. The result will be displayed inside the Shell section as shown in Figure 2.14.

Figure 2.14 Using the Thonny Notice that you can enter Python statements interactively inside the Shell section of Thonny. Using Thonny has the advantage that programs can be debugged using the built-in Debug utility. Although the debugger provided is a very simple one, it can be useful during program development. The steps to debug the program in Figure 2.14 is as follows: • Click Debug at the top menu of Thonny. You should see a new window opening on the right-hand side of Thonny with the heading Variables. At the same time, the cursor will be positioned on the first statement with a yellow bar to indicate the next statement to be executed. • Click menu item Into. You should see the variable a and its value displayed at the righthand side. At the same time, the yellow bar will move to the next statement. • Keep clicking Into until you see the result (area) displayed.

● 23

Using Sense HAT With Raspberry Pi UK 200430.indd 23

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

You can also place the cursor on a statement and then right-click to select Run to cursor. The program will run-up to the highlighted statement and then stop so that the variables of interest can be examined. Using the Thonny has the other important advantage that the program statements are automatically indented by the Thonny when conditional statements or loop statements are used. The author has used the nano editor as well as Thonny during the development of the programs in this book. It is left to the readers to choose the method they prefer. 2.7 Summary In this chapter, we learned how to install the latest Raspberry Pi operating system to an SD card, and also how to start using the Raspberry Pi Zero W remotely. Additionally, it is shown how to develop, run, and debug programs using the Python 3 programming language. In the next chapter, we will be looking at the basic details of the Sense HAT board. 2.8 Exercises 1. Describe the basic features of the Raspberry Pi Zero W. In what way is this board different from Raspberry Pi 4? 2. What is the latest Raspberry Pi operating system called? 3. Where can you find the latest Raspberry Pi operating system? 4. Explain the steps to load the latest Raspberry Pi operating system onto an SD card. 5. Explain how you can start using a Raspberry Pi Zero W after loading the operating system onto an SD card. 6. Explain the different ways that a Python program can be developed. 7. Why would you choose to use Python interactively? Give an example. 8. Write a Python program to receive the sides of a rectangle from the keyboard and then calculate and display both the perimeter and the area of this rectangle. Use the nano editor to create your program. Explain how you can test your program. 9. Write a Python program to read the temperature in degrees Celsius. Convert the temperature into Fahrenheit and display on the PC screen. You should use Thonny for this question. Test your program by running it with a few different temperatures. 10. Explain how you can debug the program you have written in Exercise 9 above using the Thonny. â—? 24

Using Sense HAT With Raspberry Pi UK 200430.indd 24

14-05-20 09:32


Index

Index A Acceleration 85 Accelerometer 25 B Bar chart 127 Barometer 25 Bluetooth 161 Button 119 C Car parking 137 Cloud 168 Compass direction 82 Compass heading 103 Current time 32 D Dice images Dice numbers Displaying images Displaying text

134 29 38 27

E Emulator 93 Etcher 14 Event counter 58, 119 F File processing

145

G Games 172 Graphics controller 25 Gyroscope 25 I Inertial measurement Input node International space station Internet communication

82 100 10 158

J Joystick 53

Joystick control Joystick events

54 100

L LCD 123 LED matrix 105 LED temperature 76 M Magnetometer 25 Mission zero 10 Mission space lab 10 Moving ball 173 Multiple graphs 144 N Nodepad++ 15 Node-red 100 Non-scrolling display 78 O Orientation 87 Output node 101 P Pitch 88 Playback sensor 98 Plotting graphs 142 Plotting in real time 154 Plotting temperature 149 Pressure readings 111 Pong game 174 Putty 15, 17 R Random colours 109 Random LEDs 79 Random letters 31 Raspbian Buster 13 Raspi-config 16 Reaction timer 59, 121 Reading humidity 64 Reading pixels 45 Reading pressure 64

â—? 181

Using Sense HAT With Raspberry Pi UK 200430.indd 181

14-05-20 09:32


Innovate with Sense HAT for Raspberry Pi

Reading temperature 64 Recording sensor 98 Roll 88 Rotate the screen 111 Rotation 44 S Screen brightness Screen flip

112 112

T TCP/IP 158 ThingSpeak Cloud 168 Temperature calibration 69 Temperature controller 115 Temperature history 132 Thonny 22 TightVNC 19 Times tables 36 U UDP 158 Ultrasonic sensor module 138 Up counter 49 Y Yaw 88 W Weather report 68, 71 Wi-Fi 159

â—? 182

Using Sense HAT With Raspberry Pi UK 200430.indd 182

14-05-20 09:32



Innovate with Sense HAT for Raspberry Pi

Exploring with Sense HAT for Raspberry Pi includes projects featuring external hardware components in addition to the Sense HAT board. You will learn to connect the Sense HAT board to the Raspberry Pi using jumper wires so that some of the GPIO ports are free to be interfaced to external components, such as to buzzers, relays, LEDs, LCDs, motors, and other sensors. The book includes full program listings and detailed project descriptions. Complete circuit diagrams of the projects using external components are given where necessary. All the projects were developed using the latest version of the Python 3 programming language. You can easily download projects from the book’s web page. Let’s start exploring with Sense HAT.

Innovate with Sense HAT for Raspberry Pi

Innovate with Sense HAT for Raspberry Pi

Dr. Dogan Ibrahim holds a BSc in Electronic Engineering, an MSc in Automatic Control Engineering, and a PhD in Digital Signal Processing. He worked in many industrial organizations before returning to academia. Professor Ibrahim is the author of more 60 technical books and more than 200 technical articles on microcontrollers, microprocessors, and related fields. He is a Chartered Electrical Engineer and a Fellow at the Institution of Engineering Technology.

The Sense HAT contains several helpful environmental sensors: temperature, humidity, pressure, accelerometer, magnetometer, and gyroscope. Additionally, an 8x8 LED matrix is provided with RGB LEDs, which can be used to display multi-color scrolling or fixed information, such as the sensor data. Use the small onboard joystick for games or applications that require user input. In Innovate with Sense HAT for Raspberry Pi, Dr. Dogan Ibrahim explains how to use the Sense HAT in Raspberry Pi Zero W-based projects. Using simple terms, he details how to incorporate the Sense HAT board in interesting visual and sensor-based projects. You can complete all the projects with other Raspberry Pi models without any modifications.

45 Sensor Projects in Python

Ready to explore the world around you? By attaching the Sense HAT to your Raspberry Pi, you can quickly and easily develop a variety of creative applications, useful experiments, and exciting games.

Dogan Ibrahim

45 Sensor Projects in Python

ISBN 978-1-907920-90-5

Elektor International Media

Dogan Ibrahim

www.elektor.com

LEARN DESIGN SHARE

LEARN DESIGN SHARE

LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● RN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR SIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN RN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR

Cover Using Sense HAT With Raspberry Pi UK 200512.indd Alle pagina's

14-05-20 09:41


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.