Programming with MicroPython
by Nicholas H. Tollervey
Copyright 2018 Nicholas H. Tollervey. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editors: Susan Conant and Jeff Bleiel
- Production Editor: Colleen Cole
- Copyeditor: Amanda Kersey
- Proofreader: Marta Justak
- Indexer: WordCo Indexing Services
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- October 2017: First Edition
Revision History for the First Edition
- 2017-09-25: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491972731 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Programming with MicroPython, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-97273-1
[LSI]
Foreword
In late 2012, I had an idea to build a tiny, low-powered computing machinethat could run scripts directly on the bare metal of the underlyinghardware. The aim was to make it extraordinarily easy for anyonenomatter his or her background nor technical skill levelto be able to make adigital device that uses 1s and 0s to control the real world.In my particular case, I wanted an easier way to program the little robotsthat I was building at the time.
At first I didnt have any particular scripting language in mind for thisdevice, but after investigating what would be a good choice for the language,and what people would like writing in and would find easy to learn, I chosePython.
I was also interested in running a Kickstarter campaign, to see if peopleliked the idea and to perhaps begin to build a community around the project.The first Kickstarter campaign that ran at the end of 2013 really did kickstart MicroPython: it raised funds to build 3,000 pyboards upon whichMicroPython would run; it brought nearly 2,000 people into the initialcommunity; and it attracted some very talented programmers who volunteeredtheir time and expertise to contribute to and improve MicroPython.
My background is theoretical physics, so I approach the design anddevelopment of MicroPython from a much more academic and research-orientedpoint of view, compared to simply engineering a solution to a problem. Ibelieve this has been part of the reason for the success of MicroPython.From the outset it was not obvious that MicroPython would even work, thatPython could at all be shrunk down to such a small size and run on tinymicrocontrollers with very little memory. I treated the creation ofMicroPython like a research project and I used analysis combined with manycoding tricks, ignoring a lot of software development principles, in orderto just get it working.
My general philosophy with MicroPython is to remain minimal yet usable. Andthe name embodies this: micro makes reference to microcontrollers andembedded systems, but also gives the feeling of minimalism. Python is thelanguage and is a very pragmatic and usable language, designed sohumans can enjoy programming. On the one extreme, making everything toominimal hurts (the ultimate minimalism is nothing!), but on the other extreme,adding masses of usability features leads to over burden and excessiveresource usage. These ideas about balancing minimalism with pragmatism applynot just to the design and development philosophy of MicroPython but also tothe wider computing industry and even beyond.
Since the first Kickstarter MicroPython has seen fantastic growth, and I,along with many others in the community, have put an immense amount of workinto MicroPython, on both the hardware and software fronts. Today MicroPythonstays true to its original goal of making it easy for anyone to digitallycontrol the real world, and is widening its scope and seeing use by hobbyistsand professionals, in education and research, as well as commercial products.MicroPython has actually formed its own little industry (soon to be muchbigger!), and its very humbling to see ones creation grow and attract interestfrom so many diverse people around the world, and for so many diverseapplications.
MicroPython is constantly improving, the community of users is growing everyday, and the future looks very bright indeed. I hope that MicroPythoncontinues to inspire people, continues to help people learn, and continues tobe used to build and control systems, from coffee makers to space satellites.Digital computing devices need to be programmed and MicroPython provides anefficient and enjoyable way to do just that.
Nicholas has been a great proponent of MicroPython since the early days and hascontributed immensely to its development, most notably from the community andeducation side, and was key to the development and success of the micro:bitplatform. He knows MicroPython very well, particularly from the point of viewof an end user, and is well qualified to teach you about it.
So dive into it, read and enjoy this fantastic initiation into the world ofMicroPython, and go out and build some amazing digital devices!
Damien P. George
Creator of MicroPython
July 2017
Preface
This is a book about MicroPython.
MicroPython is a reimplementation of Python 3 for microcontrollers andembedded systems. Its also a staggering feat of engineering to have areimplemention of Python that works under such constrained circumstances.Why is this important? MicroPython empowers people: it brings one of the mostbeautiful, easy-to-learn, and expressive programming languages to the world ofembedded development.
This book is intended for anyone who wants to learn how MicroPython is usedfor embedded development. I expect you to already knowPython, but Idont expect you to be an expert. For example, much of the material found inthis book works well in an educational context; so rather than being aprofessional programmer, you might be a teacher or autodidact.
I aim to give you the understanding, pointers, and ideas you need so youfeel confident when working on your own projects.
Most importantly, I want this book to be both fun and inspiring. Afterreading it, I hope you have enough enthusiasm that you dive in, feet first,to making your own MicroPython-based doohickey, thingamabob, or enchantedobject.
This book will provide an overview of the sorts of devices that runMicroPython, prompt you into thinking about how best to develop and executeembedded projects, examine how MicroPython uses and interacts with hardware in order to fulfil various common outcomesand behaviours, and explore idiomatic MicroPython development. It will close withsuggestions for next steps.
As with any book, it contains compromises. Some may find it too technical,others not technical enough. Alternatively, some may feel I spend too much timeon certain subjects and gloss over others. No matter the compromises Ive hadto make, my intention has been to write something that is easy to read andaccessible while providing enough context and signposts to resources forfurther study. My ultimate aim is simple: to help bring more people to theremarkable technology that is MicroPython.