Book Image

MicroPython Cookbook

By : Marwan Alsabbagh
Book Image

MicroPython Cookbook

By: Marwan Alsabbagh

Overview of this book

MicroPython is an open source implementation of Python 3 that runs in embedded environments. With MicroPython, you can write clean and simple Python code to control hardware instead of using complex low-level languages such as C and C++. This book guides you through all the major applications of the MicroPython platform to build and program projects that use microcontrollers. This MicroPython book covers recipes that will help you experiment with the programming environment and hardware programmed in MicroPython. You'll find tips and techniques for building a variety of objects and prototypes that can sense and respond to touch, sound, position, heat, and light. This book will take you through the uses of MicroPython with a variety of popular input devices and sensors. You'll learn techniques to handle time delays and sensor readings, and apply advanced coding techniques to create complex projects. As you advance, you'll deal with Internet of Things (IoT) devices and integration with other online web services. In addition to this, you'll use MicroPython to make music with bananas and create portable multiplayer video games that incorporate sound and light animations into the gameplay. By the end of this book, you'll have mastered the tips and tricks to troubleshoot your development problems and take your MicroPython project to the next level.
Table of Contents (17 chapters)

Flashing the microcontroller firmware

In this recipe, we will show how to flash the firmware on the Circuit Playground Express with the latest CircuitPython firmware. There are two reasons to this before you start working with this device. First, the device also supports the Microsoft MakeCode programming environment and flashing the device with the CircuitPython firmware prepares it for use with the Python language.

Second, the CircuitPython language is under constant development, with a release every few months, so it is a good idea to update the firmware from time to time to load the latest release of the language onto the board.

Getting ready

This chapter's introduction gives us directions on how to buy the Circuit Playground Express, which will be required for all the recipes in this chapter. A USB micro B cable and a computer running macOS, Windows, or Linux will also be required.

How to do it...

Let's look at the following steps:

  1. Download the latest CircuitPython Circuit Playground Express UF2 file (https://github.com/adafruit/circuitpython/releases/latest). The name of the UF2 file for version 3.1.2 of CircuitPython is adafruit-circuitpython-circuitplayground_express-3.1.2.uf2. For each release of CircuitPython, there are many different uf2 files for different supported microcontrollers. Make sure that you download the file for the Circuit Playground Express device.
We will use the latest stable version of CircuitPython in this recipe, which is currently 3.1.2.
  1. Connect the USB cable to the Circuit Playground Express and the computer.
  2. Double-click the reset button located at the center of the board. If all goes well, you will see all the LEDs turn green; otherwise, there is most likely an issue with the USB cable being used. In some instances, if a double-click doesn't work, try a single click of the reset button.
  1. You will see a new disk appear called CPLAYBOOT:
  1. Copy the UF2 file into this drive.
  2. Once the UF2 file has been fully written to the device, the firmware will be updated and a new drive will appear, called CIRCUITPY:

Now, our Circuit Playground Express can be used.

How it works...

Traditionally, special software has had to be installed and used to handle the delicate process of flashing a microcontroller. Microsoft developed the UF2 method, which greatly simplifies the process by not requiring any special software or command-line execution to flash the microcontroller.

Once the board is placed into the bootloader mode, it will then expect a UF2 file to be saved to it. When the UF2 is copied to the drive, the microcontroller will detect that the file copy has been completed and then automatically proceed to flash the microcontroller and restart the device, at which point the device is reattached and ready to be used.

The UF2 file format can be found athttps://github.com/Microsoft/uf2.

There's more...

The UF2 approach to flashing microcontroller firmware makes the process easier and faster compared to previous approaches. Not all MicroPython boards support the UF2 method and so require the more involved approach of installing special software to do the firmware flashing. The exact process and software required varies between different boards and manufactures.

When you use this flashing software, it will frequently require that you know the exact name of the serial device that the device appears as on your computer. The naming of these devices varies between Windows, Linux, and macOS. This type of software is usually required to be run in the Terminal, so you'll have to have some command-line knowledge to inter with it. For all these reasons, the use of UF2 with supported devices such as the Circuit Playground Express is the preferred way of starting your experimentation with MicroPython.

See also