MicroPython Cookbook
Over 110 practical recipes for programming embedded systems and microcontrollers with Python
Marwan Alsabbagh
BIRMINGHAM - MUMBAI
MicroPython Cookbook
Copyright 2019 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Richa Tripathi
Acquisition Editor: Chaitanya Nair
Content Development Editor: Ruvika Rao
Technical Editor: Romy Dias
Copy Editor: Safis Editing
Project Coordinator: Vaidehi Sawant
Proofreader: Safis Editing
Indexer: Darshana Jain
Graphics: Alishon Mendonsa
Production Coordinator: Alishon Mendonsa
First published: May 2019
Production reference: 1170519
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-83864-995-1
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Packt.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.
At www.packt.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
Marwan Alsabbagh has been coding in some form or other since before the web existed and has continued to develop software, with a particular passion for Python, his preferred programming language, for over a decade. He has been a speaker at a number of global Python conferences, where he has been known to present microcontroller projects with a healthy dose of humor and stage theatrics. The snow globe intruder alert system, which he created with his creative and curious daughters, was one of his favorite MicroPython projects. His research interests include software engineering, microcontrollers, and 3D printing.
I could not have reached this far in my life or in creating this book if it was not for the support of my loving family. Thank you for putting up with all the bananas I stole to make a whole chapter on programming microcontrollers with bananas.
About the reviewers
Arunkumar NT is 43 years of age, has completed his M.Sc. (Phy) and MBA (Finance), and is currently pursuing his CMA and CS qualifications. He has over 20 years' corporate experience and 2 years' experience of teaching MBA students. He is an entrepreneur and has previously worked for Airtel, Citi Finance, and ICICI Bank. He has also worked on the Python for Finance book.
I would like to thank my parents (my father for his support as a backup and filtering system, and my mother for her trust despite my repeated failures), my brothers (Anand NT for his critical acknowledgement and Prabhanjan NT for his love and support), my wife (Bharathi K), and my children (Vardhini AT and Charvangi AT). I would also like to thank my gurus (Prof. Badwe (deceased) and Prof. Sundararajan) and, of course, not forgetting my friendsDr. Sreepathi B and Anand Karnawat.
Bhaumik Vaidya is an experienced computer vision engineer and mentor. He has worked extensively on OpenCV and the TensorFlow library to solve computer vision problems. He is a University gold medalist at the master's level, and is now doing his PhD on the acceleration of computer vision algorithms built using OpenCV and deep learning libraries, such as TensorFlow and Keras, on GPUs. He, along with his PhD mentor, has also received an NVIDIA Jetson TX1, which is an embedded development platform, as a research grant from NVIDIA. He has previously worked in the VLSI domain as an ASIC verification engineer. He has published numerous research papers in reputable journals, has filed two provisional patents, and has written one book on computer vision and GPU programming.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
MicroPython is a lean implementation of the Python 3 programming language that is capable of running on a wide range of microcontrollers. It provides the majority of features in the Python programming language, such as functions, classes, lists, dictionaries, strings, reading and writing files, list comprehensions, and exception handling to these microcontrollers.
Microcontrollers are tiny computers that usually include a CPU, memory, and input/output peripherals. Even though they will have more limited resources compared to a PC, they can be made to a much smaller dimension, with less power consumption and at a lower cost. These strengths make it possible to use them in a wide range of new applications that werent possible before.
This book will cover a number of different features in the MicroPython language, as well as a number of different microcontroller boards. The initial chapters will provide simple and easy to understand recipes to get these boards to interact with people and their environment. Topics ranging from reading temperature, light, and motion data from sensors to interacting with push buttons, slide switches, and touchpads will also be covered. Producing output on these boards with audio playback and LED animations will also be covered in the early chapters. Once this foundation is in place, we will build more involved projects, such as interactive two-player games, electronic musical instruments, and an
Next page