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)

Updating the CircuitPython Library

In addition to updating the firmware, there is also a rich set of Python libraries called the CircuitPython Library that can also be updated with the latest supported features.

Getting ready

Any of themethods used in the previous recipes can be used hereto obtain a REPL.

How to do it...

Let's have a look at how to do this:

  1. Open the REPL through your preferred application.
  2. Download the latest CircuitPython Library Bundle release (https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest). The name of the bundle file is adafruit-circuitpython-bundle-3.x-mpy-20190212.zip. Since our firmware is using the 3.x release, we must select the bundle that is also for the 3.x release. Always use the mpy version, as this is optimized to use less disk space and has reduced memory usage.
We are using the latest auto-release version of the CircuitPython Library Bundle in this recipe, which is version 20190212 of the 3.x series.
  1. Extract the .zip file to a location on your computer.
  2. If the CIRCUITPY drive does not contain a lib folder, then create one now.
  3. Copy the contents of the extracted lib folder into the lib folder on the device.
  4. Perform a soft reboot in the REPL by pressing Ctrl + D.
  5. Run import simpleio in the REPL.
  6. If it has executed successfully, then the libraries have been successfully loaded, as the simpleio module is not part of the firmware and was imported from the library folder.

How it works...

The lib path that was created is one of the standard paths the CircuitPython will look in when importing Python packages. By adding Python packages to this folder, this makes it available for import by any scripts running on the device.

The mpy files are built from the original source py files and bundled all together in one package to make it easier to install.

There's more...

The CircuitPython Library is under constant development, so it's important to know how to update the library on the board so that you can get the latest features. As you experiment with code from projects you find on the internet, you might occasionally find examples that don't work on your board because you are running an outdated version of the CircuitPython Library. Keep your board to the latest version, as this can help prevent this from happening.

See also