Pages

Saturday, 7 October 2017

Programming with MicroPython by Nicholas Tollervey

I've just finished reading the final version of Programming with MicroPython by Nicholas Tollervey.

I love MicroPython - it helps me to develop working code faster, and it's fun to use!

MicroPython has been around for a while. I first encountered it when I met the original author, Damien George, at an Open Technology seminar at Cambridge.

More recently, I've been exploring MicroPython on the BBC micro:bit, and in the last few days I've been working on a doorbell extender project (more details soon!) using the Adafruit Feather Huzzah ESP8266, a small, low-cost board with WiFi capability. 

All these and more are covered in Tollervey's book.

 

Programming with MicroPython: contents


The book starts with a forward by Damien George, and continues with an introduction to the language. MicroPython is an almost complete implementation of the popular Python Language. It comes with its own set of libraries which allow you to access the features of the micro-controller it's running on.

(The documentation lists a few missing language features; for example, you can't use MetaClasses. The omissions won't bother the majority of developers, and they certainly won't prevent you from creating fun, interesting and useful projects.)

Next comes one of my favourite passages. Tollervey describes the excitement he and many others felt when the original BBC Micro arrived back in the 1980s.

MicroPython can generate the same excitement, but it runs on hardware that is more capable and costs a fraction of the price of the original BBC Micro.

 

MicroPython Hardware


The next four chapters cover four of the hardware platforms on which MicroPython runs. Each chapter has an overview of the hardware and its capabilities, and then gives detailed instructions for setting up a development environment.

 

The PyBoard


Tollervey starts with the PyBoard. The PyBoard is the original platform created by Damien George.

It launched after a very successful kickstarter campaign, and is now widely available.

The image is of the original version, which has been replaced by version 1.1

 

The micro:bit


The next platform is the BBC micro:bit. It's the BBC's re-invention of their 1980s microcomputer, which captured the imagination of Tollervey, Eben Upton (of Raspberry Pi fame) and a whole generation of British Schoolchildren.

The micro:bit is doing the same thing for schoolchildren world-wide. It's enjoyed great success in the UK but there are flourishing communities in countries as far apart as Croatia and Sri Lanka.

The micro:bit is small, fun and inexpensive, and it has a variety of programming environments available. There are several reasons to choose MicroPython and the Mu editor:
  • The Python language was created with educational use in mind. It's easy to learn and has a large, world-wide supportive community
  • The Mu editor is simple to use, and you can deploy your program almost instantly. Other environments involve a fiddly code/compile/copy process that can slow down development. It's easy to make an error - the last thing you want when you're learning a new skill.
  • Mu gives you easy access to the Python REPL, so you can type code in and see the results immediately.
Tollervey describes how to set up and use the Mu editor and gives a sample program. He also gives a link to the online documentation and tutorial.

You can also get a free workbook which covers most of the micro:bit MicroPython capabilities. Disclosure - I wrote it :)

 

Adafruit Circuit Playground Express


The next hardware platform is the recently updated Adafruit Circuit Playground Express. (There is an earlier version which uses a different processor that doesn't support MicroPython).

This board is overloaded with goodies:
  • 10 x mini NeoPixels, each one can display any color
  • 1 x Motion sensor (LIS3DH triple-axis accelerometer with tap detection, free-fall detection)
  • 1 x Temperature sensor (thermistor)
  • 1 x Light sensor (phototransistor). Can also act as a color sensor and pulse sensor.
  • 1 x Sound sensor (MEMS microphone)
  • 1 x Mini speaker with class D amplifier (7.5mm magnetic speaker/buzzer)
  • 2 x Push buttons, labeled A and B
  • 1 x Slide switch
  • Infrared receiver and transmitter - can receive and transmit any remote control codes, as well as send messages between Circuit Playground Expresses. Can also act as a proximity sensor.
  • 8 x alligator-clip friendly input/output pins
  • Includes I2C, UART, 8 pins that can do analog inputs, multiple PWM output
  • 7 pads can act as capacitive touch inputs and the 1 remaining is a true analog output
  • Green "ON" LED so you know it's powered
  • Red "#13" LED for basic blinking
  • Reset button
  • ATSAMD21 ARM Cortex M0 Processor, running at 3.3V and 48MHz
  • 2 MB of SPI Flash storage, used primarily with CircuitPython to store code and libraries.
  • MicroUSB port for programming and debugging
  • USB port can act like serial port, keyboard, mouse, joystick or MIDI!
 Best of all, you can control this with MicroPython! The book tells you how, and introduces CircuitPython - Adafruit's fork of MicroPython, designed to give you a consistent API across all the Adafruit MicroPython platforms.

The fourth chapter on hardware covers two powerful processors from Shanghai-based manufacturer, Espressif Systems; the ESP8266 and ESP32.

 

ESP8266 / ESP32


The ESP8266 has generated a huge amount of interest in the maker community. It has a processor that's powerful enough to run MicroPython, it has a low-power mode, and it has on-board WiFi. That's a perfect set of features for low-cost IoT (Internet of Things) projects.

Adafruit has a Feather Huzzah ESP8266 board for around $17. (You can get similar boards from China for as little as $1). The feather means that there is a family of add-on Adafruit wings - compatible boards like motor controllers, GPS modules and OLED display drivers.

Best of all, the ESP8266 has an easy-to-use browser-based development environment called WebREPL. The book has very clear instructions telling you how to set it up; I had mine going in just over three minutes! (It may take you a little longer - you are probably less impetuous than me.)

The ESP32 is even more powerful but the software is currently less mature. Tollervey guides you through the steps you'll need to take in order to set up a development environment.

 

Thinking Embedded


The next chapter has great advice about how to approach the design, specification and development of embedded projects by focusing on user needs and usefulness.

It's followed by a series of chapters which cover the main ingredients of an embedded system:

  • Visual Feedback
  • Input and Sensing
  • GPIO
  • Networking
  • Sound
 Code examples are sprinkled throughout the text.

Robots


I love building robots, and this book covers two: the DIY TrundleBot and the Bit:Bot from 4Tronics. There are complete, detailed MicroPython programs for both, and plenty of suggestions for experiments of your own.

Tollervey is a keen exponent of good Pythonic coding, so the next chapter should be no surprise.

Idiomatic MicroPython

The chapter on Idiomatic MicroPython is recommended reading for Python novices and veterans alike. It describes a set of qualities sought after by the most experienced Python developers, and explains some of the particular challenges of programming in a resource-constrained environment.

Next Steps


The final chapter encourages readers to participate in the Python community, and lists additional resources.

It ends with great advice:

get stuck in and remember...

Code,
Hack it,
Less is more,
Keep it simple,
Small is beautiful,
Be brave! Break things! Learn and have fun!
Express yourself with MicroPython.
Happy hacking! :-)


—The Zen of MicroPython

About the Author

Tollervey is well-qualified to write about MicroPython. He's the author of the excellent, Open Source, minimalist Mu IDE, and was the original instigator of the effort to port it to the micro:bit. He's a Python Software Foundation Fellow, and has recently been recognised by Google for his contributions to the Open Source community.

An excellent book


I strongly recommend this book. If you're a novice, keen to explore the fun and power of programming micro-controllers with MicroPython, this forms an excellent introduction. If you're a grizzled veteran like me you can still expect to find new and useful material in every chapter.


Availability - and a chance to meet the author!


The book is available to pre-order on Amazon. It's scheduled for release on 21st October in the US and 31st October in the UK.

If you're in or near London on the evening of 6th November, there's a chance to meet the author. Nicholas Tollervey is going to speak at the London MicroPython Meetup at the Barclays Eagle Labs in Notting Hill Gate. You can find details here.