Learning Python with Raspberry Pi (Extract)

Page 1

# RESISTIVE POTENTIAL DIVIDER # --------------------------# # This is a resistive potential divider circuit program. # The program calculates the resistance values that will # lower the input voltage to the desired value # # Program: divider.py # Date : July, 2019 # Author : Dogan Ibrahim #======================================================= print("RESISTIVE POTENTIAL DIVIDER") print("===========================") R1flag = 1 R2flag = 0

For Electronics Engineers

Learning Python with Raspberry Pi

while R1flag == 1: Vin = float(raw_input("\nInput voltage (Volts): ")) Vo = float(raw_input("Desired output voltage (Volts): ")) R2 = float(raw_input("Enter R2 (in Ohms): ")) # # Calculate R1 # R1 = R2 * (Vin - Vo) / Vo print("\nR1 = %3.2f Ohms R2 = %3.2f Ohms" %(R1, R2)) # # Read chosen physical R1 and display actual Vo # NewR1 = float(raw_input("\nEnter chosen R1 (Ohms): ")) # # Display and print the output voltage with chosen R1 # print("\nWith the chosen R1,the results are:") Vo = R2 * Vin / (NewR1 + R2) print("R1 = %3.2F R2 = %3.2f Vin = %3.2f Vo = %3.3f" %(NewR1,R2,Vin,Vo)) # # Check if happy with the values ? # happy = raw_input("\nAre you happy with the values? ") happy = happy.lower() if happy == 'y': break else: mode = raw_input("Do you want to try again? ") mode = mode.lower() if mode == 'y': R1flag = 1 else: LEARN DESIGN SHARE R1flag = 0 break

Dogan Ibrahim

GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DE HARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DE HARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN



Learning Python with Raspberry Pi for electronic engineers

â—? Dogan Ibrahim

LEARN DESIGN SHARE


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

Acknowledgements

The author would like to express his thanks to Ferdinand te Walvaart of Elektor for the valuable suggestions he made throughout the duration of the preparation of this book. The author would like to thank also to his wife Nadire for her encouragement, motivation, and for being patient with me while working on this book.

British Library Cataloguing in Publication Data

ISBN 978-1-907920-80-6

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

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

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


Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 1 • Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2 Parts of the Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.3 Requirements of the Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.3.1 Setup Option 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.3.2 Setup Option 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Chapter 2 • Setting Up the Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 Installation Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 Powering Up your Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4 Remote Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.4.1 Configuring the Putty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.5 Remote Access of the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Chapter 3 • Using the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.2 The Raspberry Pi Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.3 File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.4 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.5 Date, Time, and Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.6 File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.7 System and User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.8 Resource Monitoring on Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.9 Shutting Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Chapter 4 • Using a Text Editor in Command Mode . . . . . . . . . . . . . . . . . . . . . . . . 44 4.1 nano Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 5 • Creating and Running a Python Program . . . . . . . . . . . . . . . . . . . . . . 48 5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 5.2 Method 1 – Interactively from Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . 48

●5


Learning Python with Raspberry Pi 5.3 Method 2 – Create a Python File in Command Mode . . . . . . . . . . . . . . . . . . . . . . 49 5.4 Method 3 – Create a Python File in GUI mode . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.5 Which Method? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 5.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Chapter 6 • Python Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.2 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.3 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.5 Line Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.6 Blank Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.7 More Than One Statement on a Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.8 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.9 Python Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.11 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 6.11.1 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 6.11.2 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 6.12 Print Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 6.13 List Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.13.1 List Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 6.14 Tuple Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 6.15 Dictionary Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 6.15.1 Dictionary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 6.16 Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6.17 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6.18 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.19 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.20 Control of Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.20.1 if, if-else, and elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.20.2 for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 6.20.3 while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

●6


6.20.4 continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.20.5 break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 6.20.6 pass Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 6.21 Case Study 1 – 4 Band Resistor Colour Code Identifier . . . . . . . . . . . . . . . . . . . 70 6.22 Case Study 2 – 4 Band Resistor Colour Code Identifier Including Small Resistors . 72 6.23 Case Study 3 – Series or Parallel Resistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.24 Case Study 4 – Resistive Potential Divider . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 6.25 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 6.26 User-defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 6.27 Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 6.28 Case Study 5 - Resistive Attenuator Design – Equal Source & Load Resistances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 6.29 Case Study 6 - Resistive Attenuator Design – Unequal Source & Load Resistances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 6.30 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 6.31 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 6.31.1 try/finally Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 6.32 Case Study 7 – Zener Diode Based Voltage Regulator . . . . . . . . . . . . . . . . . . . 109 6.33 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 6.34 Creating Our Own Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6.35 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Chapter 7 • Plotting Graphs With Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 7.2 The Matplotlib Graph Plotting Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 7.3 Case Study 8 – RC Transient Circuit Analysis - Charging . . . . . . . . . . . . . . . . . . 130 7.4 Case Study 9 – RC Transient Circuit Analysis - Discharging . . . . . . . . . . . . . . . . 132 7.5 Transient RL Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 7.6 Case Study 10 – RCL Transient Circuit Analysis . . . . . . . . . . . . . . . . . . . . . . . . 136 7.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Chapter 8 • Files in Python With the Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . 140 8.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 8.2 Python File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

●7


Learning Python with Raspberry Pi 8.3 Case Study 11 – RC Circuit Frequency Response . . . . . . . . . . . . . . . . . . . . . . . 146 8.4 Case Study 12 – Save Raspberry Pi 4 CPU Temperature in a File . . . . . . . . . . . . 148 8.5 Saving Data on an External USB Memory Stick . . . . . . . . . . . . . . . . . . . . . . . . 150 8.6 Case Study 13 – Save Raspberry Pi 4 CPU Temperature on Memory Stick . . . . . . 152 8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Chapter 9 • Array and Matrix Operations With Python . . . . . . . . . . . . . . . . . . . . . 155 9.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 9.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 9.2.1 Array Operations and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 9.2.2 Array Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 9.2.3 Copying Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 9.3 Systems of Linear Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 9.4 Case Study 14 – DC Circuits Mesh Analysis 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 160 9.5 Case Study 15 – DC Circuits Mesh Analysis 2 . . . . . . . . . . . . . . . . . . . . . . . . . . 163 9.6 Case Study 16 – DC Circuits Mesh Analysis 3 . . . . . . . . . . . . . . . . . . . . . . . . . . 164 9.7 Case Study 17 – DC Circuits Node Analysis 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 165 9.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Chapter 10 • Using the Tkinter Graphical User Interface (GUI) in Python . . . . . . 169 10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 10.2 GUI with the Tkinter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 10.2.1 Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 10.2.2 Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 10.2.3 Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 10.2.4 Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 10.2.5 Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 10.2.6 Radio Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 10.2.7 Checkbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 10.2.8 Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 10.2.9 Scale (Slider) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 10.2.10 Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 10.2.11 Binding to Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 10.3 Case Study 18 – Resonance in Series RLC Circuits . . . . . . . . . . . . . . . . . . . . . 196

●8


10.4 Case Study 19 – Inductance of a Single Layer Coil . . . . . . . . . . . . . . . . . . . . . 200 10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Chapter 11 • BJT Transistor Circuit Analysis and Design . . . . . . . . . . . . . . . . . . . 203 11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 11.2 BJT Transistor DC Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 11.2.1 Collector Feedback Biasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 11.3 Case Study 20 – Analyzing Collector Feedback Biasing . . . . . . . . . . . . . . . . . . 203 11.4 Voltage Divider Biasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 11.5 Case Study 21 – Analyzing Voltage Divider Biasing . . . . . . . . . . . . . . . . . . . . . 206 11.6 Case Study 22 – Designing Transistor Amplifier Circuit . . . . . . . . . . . . . . . . . . 208 11.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Chapter 12 • Active Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 12.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 12.2 Case Study 23 – Designing Low-Pass Active Filters . . . . . . . . . . . . . . . . . . . . . 214 12.3 High-Pass Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 12.4 Band-Pass Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 12.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Chapter 13 • Accessing Raspberry Pi 4 Hardware and Peripheral Devices From Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 13.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 13.2 GPIO – Parallel Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 13.2.1 The GPIO Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 13.2.2 Pin Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 13.2.3 Channel (I/O port pin) Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 13.2.4 Case Study 24 – Flashing an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 13.3 PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 13.3.1 Case Study 25 – Changing the Brightness of an LED . . . . . . . . . . . . . . . . . . 228 13.4 I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 13.4.1 Case Study 26 – Using I2C LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 13.5 SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 13.5.1 Case Study 27 – Using SPI – Analog Temperature Sensor . . . . . . . . . . . . . . . 234 13.6 The serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

●9


Learning Python with Raspberry Pi 13.6.1 Case Study 28 – Using Serial Communication – Serial Loopback . . . . . . . . . . 239 13.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Chapter 14 • Python and the Internet on Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . 242 14.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 14.2 Internet Communication Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 14.3.1 Case Study 29 – Sending a Text Message to a Mobile Phone Using TCP/IP . . . 243 14.3.2 Case Study 30 – Communicating with a PC Using TCP/IP . . . . . . . . . . . . . . . 246 14.3.3 Case Study 31 – Controlling an LED Connected to Raspberry Pi From Mobile Phone Using TCP/IP . . . . . . . . . . . . . . . . . . . . 249 14.4 UDP Based Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 14.4.1 Case Study 32 – Sending a Text Message to a Mobile Phone Using UDP . . . . . 252 14.4.2 Case Study 33 – Controlling an LED Connected to Raspberry Pi From Mobile Phone Using UDP . . . . . . . . . . . . . . . . . . . . . . . . . . 254 14.5 U sing Flask to Create a Web Server to Control Raspberry Pi GPIO Ports From the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 14.5.1 Case Study 34 – Web Server - Controlling an LED Connected to Raspberry Pi Using Flask . . . . . . . . . . . . . . . . . . . . . . . . . . 257 14.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Chapter 15 • B luetooth Communication on Raspberry Pi 4 Using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 15.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 15.2 Case Study 35 – Bluetooth Control of LED From a Mobile Phone . . . . . . . . . . . . 262 15.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Appendix A • Using wxPython Graphical User Interface . . . . . . . . . . . . . . . . . . . 269 A.1 wxPython Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 A.2 Some Example Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 A.3 Case Study 36 – Display the Raspberry Pi 4 CPU Temperature Using wxPython . . 276 Appendix B • Object-oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 B.1 Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 B.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 B.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

● 10


Preface

Preface Python is an interpreted high-level language created in The 1990s by Guido van Rossum. The language is procedural and object-oriented. Python is a general purpose language which can be used for web development, general software development, mathematics and system scripting. The latest version of Python is Python 3. However, Python 2.7 still seems to be very popular and is used extensively. Python has some similarities to the English language and relies on indentation to define the scope of loops, functions and classes. Other programming languages (e.g. C) use curly-brackets for this purpose. Python is currently the most popular teaching language. It is used as the first programming language in most colleges and universities around the world. Most software development companies (e.g. Google, Dropbox, Spotify, Netflix, PayPal, Reddit, etc) prefer Python language because of its versatile features and fewer programming codes to accomplish a task. Some of the advantages of Python can be summarized as: • Interpreted • Interactive • Dynamic • Modular • Object-oriented • Portable (available on Raspberry Pi as well as on the PC) • Easy to learn • Machine learning and artificial intelligence support • Extensive support libraries • Support of multitasking • Well supported and maintained • Free of charge Perhaps the strongest point of Python is that it supports extensive support libraries that include string operations, web tools, Wi-Fi and Bluetooth support, and operating system interfaces. Python is available on many operating systems, including Linux, Windows, MAC, and many others. Python has recently become more popular since it is the most commonly used programming language on the Raspberry Pi family of single-board computers. For example, students can purchase a $50 rather powerful Raspberry Pi computer with the Python programming language already installed. They can learn Python by experimenting on the Raspberry Pi. This book is about teaching the Python programming language using the Raspberry Pi 4 computer. The book is aimed for beginners, students, practicing engineers, hobbyists, and for anyone else who may want to learn to program in Python. The book includes many example programs and case studies. All the example programs and case studies have been tested fully by the author and are all working. Most of the programs in the book have been developed using Python 2.7, but they will work on Python 3 with minor changes. The graph-

● 11


Learning Python with Raspberry Pi

ics programs (tkinter) have been developed using Python 3. The example programs are general in the sense that they aim to teach the various programming concepts of Python. The case studies cover the use of Python in the analysis and design of electronic circuits. Electronic engineers and readers interested in electronic circuit analysis and design will find the case studies very useful. The following sub-headings are used while describing the example programs and case studies: • Title • Description • Aim • Background Information • Program Listing Full program listings of all the programs used in the book are available at the Elektor web site of the book. Readers should be able just to copy and use these programs in their Raspberry Pi projects without any modifications. The operation of all the programs given in the book are fully described in the book and therefore readers should find it easy to develop these programs further, or to modify them for their own needs. I hope you enjoy reading the book and use Python in your next Raspberry Pi project. Dogan Ibrahim September, 2019 London

● 12


Chapter 1 • Raspberry Pi 4

Chapter 1 • Raspberry Pi 4 1.1 Overview Raspberry Pi has recently become one of the most popular and most powerful single-board computers used by students, hobbyists, and professional engineers. Raspberry Pi 4 is the latest and the most powerful version of the Raspberry Pi. In this chapter, we shall be looking at the basic specifications and requirements of the Raspberry Pi 4 computer. What is included in this chapter, can easily be applied to other models in the Raspberry Pi family. 1.2 Parts of the Raspberry Pi 4 Just like its earlier versions, Raspberry Pi 4 is a single-board computer having the following basic specifications: • 1.5GHz 64-bit quad-core CPU • 1GB, 2GB, or 4GB RAM • 2.4GHZ and 5.0GHz IEEE 802.11ac Wi-Fi • Bluetooth 5.0 BLE • Gigabit Ethernet • 2 x USB 3.0, 2 x USB 2.0 and 1 x USB-C ports • 2 x micro-HDMI ports for dual display, supporting up to 4K resolution • DSI display and CSI camera ports • micro SD card slot for the operating system and data storage • 4-pole stereo audio and composite video port • 40-pin GPIO header • Power over Ethernet (PoE) enabled with the PoE HAT • OpenGL ES 3.0 graphics

Figure 1 shows the Raspberry Pi 4 board with its major components identified.

● 13


Learning Python with Raspberry Pi

A brief description of the various components on the board is given below: Processor: the processor is enclosed in a metal cap and it is based on Broadcom BCM2711B0, which consists of a Cortex A-72 core, operating at 1.5GHz. RAM: There are 3 versions of Raspberry Pi 4 depending on the amount of DDR4 RAM required: 1GB, 2GB, and 4GB. USB Ports: Raspberry Pi 4 includes 2 x USB 3.0, 2 x USB 2.0, and 1 x USB-C ports. USB 3.0 data transfer rate is 4,800 Mbps (megabits per second), while USB 2.0 can transfer at up to 480Mbps, i.e. 10 times slower than the USB 2.0. The USB-C port enables the board to be connected to a suitable power source. Ethernet: The Ethernet port enables the board to be connected directly to an Ethernet port on a router. The port supports Gigabit connections (125Mbps). HDMI: Two micro HDMI ports are provided that support up to 4K screen resolutions. HDMI adapters can be used to interface the board to standard size HDMI devices. GPIO: A 40-pin header is provided as the GPIO (General Purpose Input Output). This is compatible with the earlier GPIO ports. Audio and Video Port: A 3.5mm jack type socket is provided for stereo audio and composite video interface. Headphones can be connected to this port. External amplifier devices will be required to connect speakers to this port. This port also supports composite video, enabling TVs, projectors, and other composite video compatible display devices to be connected to the port. CSI Port: This is the camera port (Camera Serial Interface), allowing a compatible camera to be connected to the Raspberry Pi. DSI Port: This is the display port (Display Serial Interface), allowing a compatible display (e.g. 7 inch Raspberry Pi display) to be connected to the Raspberry Pi. PoE Port: This is a 4-pin header, allowing the Raspberry Pi to receive power from a network connection. Micro SD Card: This card is mounted on the cardholder placed at the bottom of the board and it holds the operating system software as well as the operating system and user data. 1.3 Requirements of the Raspberry Pi 4 As listed below, a number of external devices are required before the Raspberry Pi can be used:

â—? 14


Chapter 1 • Raspberry Pi 4

• Power supply • Micro SD card • Operating system software • USB keyboard and mouse • Micro HDMI cable to receive sound and video signals • HDMI compatible display or TV (you may also need to have micro HDMI to DVI-D or VGA adapters. A 3.5mm TRRS type cable and plug will be required if you will be using an old TV with composite video) Power Supply: A 5V 3A power supply with USB-C type connector is required. You may either purchase the official Raspberry Pi 4 power supply (Figure 1.2), or use a USB-C adapter to provide power from an external source.

Figure 1.2 Official Raspberry Pi 4 power supply Micro SD Card: It is recommended to use a micro SD card with a capacity of at least 8GB, although higher capacity (e.g. 16GB or 32GB) is better as there will be room to grow in the future. A Class 10 (or faster) card is recommended. Operating System: You can purchase the operating system pre-loaded on a micro SD card, known as NOOBS (New Out Of Box Software) which require minimum configuration before it is fully functional. The alternative is to purchase a blank micro SD card and upload the operating system on this card. The steps to prepare a new micro SD card with the operating system is given in the next chapter. USB Keyboard and Mouse: You can either use a wireless or wired keyboard and mouse pair. If using a wired pair, you should connect the keyboard to one of the USB ports and the mouse to another USB port. If using a wireless keyboard and mouse, you should connect the wireless dongle to one of the USB ports. Display: A standard HDMI compatible display monitor with a micro HDMI to standard HDMI adapter can be used. Alternatively, a VGA type display monitor with a micro HDMI to VGA adapter or DVI-D adapter can be used. If you have an old TV with composite video inter-

● 15


Learning Python with Raspberry Pi

face, then you can connect it to the Raspberry Pi 3.5mm port with a TRRS type connector. You may also consider purchasing additional parts, such as a case, CPU fan, and so on. A case is very useful as it protects your Raspberry Pi electronics. The working temperature of the CPU can go as high as 80 degrees Centigrade. Using a fan (see Figure 1.3) makes the CPU more efficient as it can lower its temperature by about 50%.

Figure 1.3 Raspberry Pi 4 CPU fan (www.seeedstudio.com) 1.3.1 Setup Option 1 As shown in Figure 1.4, in this option various devices are connected directly to the Raspberry Pi 4. Depending on what type of display monitor we have, we can use an HDMI display, VGA monitor, DVI-D monitor, or TV. Notice that depending on the external USB devices used, you can use either the USB 2.0 or the USB 3.0 ports.

â—? 16


Chapter 1 • Raspberry Pi 4

Figure 1.4 Raspberry Pi 4 setup - option 1 1.3.2 Setup Option 2 In this option, as shown in Figure 1.5, a powered hub is used to connect the USB devices.

Figure 1.5 Raspberry Pi 4 setup – option 2

● 17


Learning Python with Raspberry Pi

1.4 Summary In this chapter we have learned the main parts and their functions on the Raspberry Pi 4 board. In addition, we have learned how to setup the Raspberry Pi 4. In the next chapter we shall be installing the operating system on a new blank micro SD card.

â—? 18


Chapter 2 • Setting Up the Raspberry Pi 4

Chapter 2 • Setting Up the Raspberry Pi 4 2.1 Overview In this chapter we shall be learning how to install the Raspberry Pi 4 operating system Raspbian on a new blank micro SD card. If you have purchased a pre-installed micro SD card with NOOBS, you do not need to do anything else other than plug it into your Raspberry Pi. 2.2 Installation Steps The instructions to install the operating system on a blank micro SD card using the NOOBS are given below. You will need a micro SD card with a capacity of at least 8GB, although 16GB is recommended for future expansion and installing new applications and programs. You might need a standard size SD card adapter to insert the micro SDcard into your computer's card slot. The steps are as follows: • Activate the following link to select the operating system as shown in Figure 2.1, and click NOOBS.

https://www.raspberrypi.org/downloads/

Figure 2.1 Operating system selection • As shown in Figure 2.2, there are two versions of the software: NOOBS and NOOBS Lite. You should select to download NOOBS as it contains the Raspbian operating system and other products. Click Download ZIP under NOOBS and save the file in a folder (e.g. C:\RPI).

● 19


Learning Python with Raspberry Pi

Figure 2.2 Click Download ZIP under NOOBS • We should now format our micro SD card using SD Card Association Card Formatter tool. Install this tool from the following site (see Figure2.3):

https://www.sdcard.org/downloads/formatter/

Figure 2.3 Install the SD card formatter • Insert your micro SD card into the card reader of your PC and run the card formatter application. Give a volume name (e.g. RPI or NOOBS) and select Quick Format and click Format (see Figure 2.4) after making sure that you have selected the card. The formatting process should take only a few seconds.

● 20


Chapter 2 • Setting Up the Raspberry Pi 4

Figure 2.4 Format the micro SD card • We can now install the operating system on the card. Double click the operating system file in folder C:\RPI and unzip it into the same directory. • Select all the files (except unzipped file NOOBS_v3_1_1) in folder C:\RPI and drag them to your micro SD card (Figure2.5).

Figure 2.5 Select files and drag to the SD card • When the files are all copied, remove the SD card from your PC and insert into your Raspberry Pi. 2.3 Powering Up your Raspberry Pi 4 Connect the power supply cable, keyboard, mouse, and display to your Raspberry Pi and apply power. After a short while you will see the startup menu on the display as shown in Figure 2.6. Select the Raspbian operating system and click the Install button. Click YES to

● 21


Learning Python with Raspberry Pi

confirm to install the Raspbian operating system. You should see the message Raspbian Full: Creating filesystem at the bottom of the display (Figure 2.7). Wait for 5 to 10 minutes until the operating system has been installed and ready on the SD card (See Figure 2.8). You should see a progress bar at the bottom of the display as the installation process continues.

Figure 2.6 Raspberry Pi 4 installation startup menu

Figure 2.7 Installing the operating system

â—? 22


Chapter 2 • Setting Up the Raspberry Pi 4

Figure 2.8 End of installation At this point, you are given the option of selecting a Wi-Fi network. In Figure 2.9, BTHomeSpot-XNH is selected as the Wi-Fi network and the password id entered.

Figure 2.9 Select a Wi-Fi network Move the mouse pointer over the blue Wi-Fi icon at the top right-hand side of the display and you should see the IP address assigned to the Raspberry Pi. As shown in Figure 2.10, the address 192.168.1.202 was assigned to the Raspberry Pi.

Figure 2.10 Raspberry Pi IP address The IP address of the Raspberry Pi could also be displayed by entering command ifconfig to a terminal session as shown in Figure 2.11.

â—? 23


Learning Python with Raspberry Pi

Figure 2.11 Result of command ifconfig 2.4 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 and the VNC 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 P2 SSH (see Figure 2.12) and enable SSH. Click <Finish> to exit the menu.

Figure 2.12 Enable SSH You should also enable VNC so that the Raspberry Pi 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.

â—? 24


Chapter 2 • Setting Up the Raspberry Pi 4

At this stage, you may want to shutdown your Raspberry Pi by clicking the Applications Menu on Desktop and selecting the Shutdown option as shown in Figure 2.13.

Figure 2.13 Shutting down the Raspberry Pi The program we will be using on our PC to access the Raspberry Pi is called Putty, where it can be downloaded from the following link:

https://www.putty.org/

Putty is a standalone program and there is no need to install it. 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.14). The message shown in Figure 2.15 will be displayed the first time you access the Raspberry Pi. Click Yes to accept this security alert.

â—? 25


Learning Python with Raspberry Pi

Figure 2.14 Putty startup screen

Figure 2.15 Click Yes to accept You will be prompted to enter the username and password. After a successful login, you should see the command prompt as in Figure 2.16.

Figure 2.16 Successful login

â—? 26


Chapter 2 • Setting Up the Raspberry Pi 4

2.4.1 Configuring the Putty By default, the Putty screen background is black with white foreground characters. The author prefers to have a white background with black foreground characters, with the character size set to 12 points bold. The steps to configure the Putty with these settings are given below. Notice that in this example these settings are saved with the name RPI4 so that they can be recalled whenever the Putty is re-started: • Restart Putty • Select SSH and enter the Raspberry Pi IP address • Click Colours under Window • Set the Default Foreground and Default Bold Foreground colours to black (Red:0, Green:0, Blue:0) • Set the Default Background and Default Bold Background to white (Red:255, Green:255, Blue:255) • Set the Cursor Text and Cursor Colour to black (Red:0, Green:0, Blue:0) • Select Appearance under Window and click Change in Font settings. Set the font to Bold 12. • Select Session and give a name to the session (e.g. RPI4) and click Save. • Click Open to open the Putty session with the saved configuration (see Figure 2.17) • Next time you re-start the Putty, select the saved session and click Load followed by Open to start a session with the saved configuration

Figure 2.17 Starting Putty with the saved configuration 2.5 Remote Access of the Desktop If you are using your Raspberry Pi with local keyboard, mouse, and display you can skip this section. If on the other hand, you want to access your Desktop remotely over the network, you will find that SSH services cannot be used. The easiest and simplest way to access your Desktop remotely from a computer is by installing the VNC (Virtual Network Connection) client and server. The VNC server runs on your Pi and the VNC client runs on your computer. The steps to install and use the VNC are given below: • Connect to your Raspberry Pi using the Putty as explained earlier. Then, enter the following command to install a program called TightVNC server on your Pi computer. You will see many lines of messages and make sure that there are no error messages:

● 27


Learning Python with Raspberry Pi

pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo apt-get install tightvncserver

• Run the VNC server on your Raspberry Pi computer by entering the following command: pi@raspberrypi:~ $ vncserver :1

• You will be prompted to enter and verify a password. This will be the password you will be using to access the Desktop remotely (see Figure 2.18).

Figure 2.18 Enter a password for the VNC server • The VNC server is now running on your Raspberry Pi. The only command you need to enter on your Pi computer to start the VNC server is: pi@raspberrypi:~ $ vncserver :1

• We must now setup a VNC client on our laptop (or desktop). There are many VNC clients available, but the recommended one which is compatible with TightVNC is the TightVNC for the PC which can be downloaded from the following link:

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.19) followed by :1. Click Connect to connect to your Raspberry Pi.

● 28


Chapter 2 • Setting Up the Raspberry Pi 4

Figure 2.19 Start the TightVNC and enter the IP address Enter the password you have chosen earlier. You should now see the Raspberry Pi Desktop displayed on your PC screen as shown in Figure 2.20.

Figure 2.20 Raspberry Pi Desktop on your PC screen 2.6 Summary In this chapter we have seen how to install the Raspbian operating system on our Raspberry Pi 4. We have also learned how to access our Raspberry Pi remotely. In the next chapter we shall be looking at some of the commonly used commands.

â—? 29


INDEX

INDEX A Active filter Array operations Array multiplication Attenuator design

Expression operators 214 155 156 100, 103

B Bar chart 129 Bitwise operators 55 Blank lines 52 Bluetooth 262 BJT transistor 203 break statement 69 Button 173 C cal 36 Check box 185 chmod 33 Collector feedback biasing 203 comments 52 continue statement 68 Copying arrays 157 cp 36 CPU fan 16 CPU temperature 148 Critically damped mode 136 CSI 14 D Data types 53 date 36, 113 df 42 Dialogs 187 Dictionary variables 63 dpkg 40 DSI 14 E echo 37 Entry 179 Escape sequences 59 Ethernet 14 Exceptions 106

55

F Files 140 Flask 255 float 54 for statement 66 frequency response 146 G GPIO 222 Grid 180 H head 38 HDMI 14 High-pass filter 219 I I2C 229 Ifconfig 23 Indentation 53 Inductance 200 int 54 K Keyboard input Keyboard interrupt

64 109

L Label 170 LCD 230 Linear algebraic equations 157 List variables 61 Logical operators 65 long 54 low-pass active filter 214 ls 30 M man 35 matplotlib 117 Matrix operations 155 Mathematical functions 55

â—? 283


Learning Python with Raspberry Pi

MCP3002 235 Menu 192 Message 178 Mesh analysis 160 mkdir 31 modules 115 N nano 44 Node analysis 165 NOOBS 19 O Object oriented programming Overdamped mode

279 138

P pass statement 69 Pie chart 127 Pin numbering 223 print statement 60 ps 42 Putty 25 pwd 30 PWM 227 R Radio button 181 RC transient circuit charging 130 RC transient circuit discharging 132 Recursive functions 105 Resonance 196 Remote access 24 Reserved words 52 Resistive potential divider 76 Resistor colour code 70, 72 RLC transient circuit 136 rm. 37 rmdir 37 S Series and parallel resistors 74 Serial port 238 SPI 233 Slider 190 SSH 24

â—? 284

Strings 57 String functions 58 T tail 38 TCP/IP 242 TCP client 244 TCP server 244 Temperature sensor 234 Thonny 49 time 36, 113 Tkinter 169 Transient RL circuit 135 Transistor amplifier 208 Trigonometric functions 78 try/finally 108 Tuple variables 62 U UDP 251 Underdamped mode 136 USB keyboard 15 USB memory stick 150 USB port 14 User defined function 79 V Variable names 51 vncserver 28 Voltage divider biasing 205 W Web server 257 while statement 68 whoami 39 wxPython 269 Z Zener diode

108


For Electronics Engineers

Learning Python with Raspberry Pi

Prof. Dr. Dogan Ibrahim has a BSc. in Electronic Engineering, an MSc. in Automatic Control Engineering, and a Ph.D. in Digital Signal Processing. He worked in many industrial organisations before returning to academia. Prof. Ibrahim is the author of over 60 technical books and over 200 technical articles on microcontrollers, microprocessors, and related fields. He is a Chartered Electrical Engineer and a Fellow of the Institution of Engineering Technology.

ISBN 978-1-907920-80-6

Elektor International Media BV

This book is about teaching the Python programming language using the Raspberry Pi 4 computer. The book makes an introduction to Raspberry Pi 4 and then teaches Python with the topics: variables, strings, arrays, matrices, tuples, lists, dictionaries, user functions, flow of control, printing, keyboard input, graphics, GUI, object oriented programming and many more topics. The book is aimed for beginners, students, practising engineers, hobbyists, and for anyone else who may want to learn to program in Python. The book includes many example programs and case studies. All the example programs and case studies have been tested fully by the author and are all working. The example programs aim to teach the various programming concepts of Python. The case studies cover the use of Python in the analysis and design of electronic circuits. Some of the case study topics are: • Resistor colour code identification • Resistive potential divider circuits • Resistive attenuator design • Zener diode voltage regulator design • RC and RLC transient circuits • Circuit frequency response • Saving data on external memory stick • Mesh and node circuit analysis using matrices • Resonance in RLC circuits • Transistor Biasing analysis • Transistor amplifier design • Design of active filters • Interfacing hardware with GPIO, I2C and SPI • Using Wi-Fi with Python and TCP/IP and UDP programs • Using Bluetooth from Python Full program listings of all the programs used in the book are available at the Elektor website of the book. Readers should be able just to copy and use these programs in their Raspberry Pi projects without any modifications.

www.elektor.com

LEARN DESIGN SHARE

● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEA IGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● D HARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN N ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEA SIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● D


Turn static files into dynamic content formats.

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