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)

Using the auto-reload feature

The following recipe shows how to use auto-reload so that the cycle of editing and running code can become much faster and more fun.

Getting ready

Any of the methods used in the previous recipes can be used here to obtain a REPL.

How to do it...

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

  1. Open the main.py file and save the print('hi there') statement in the file.
  2. Open the REPL through your preferred application. With the REPL open, press Ctrl + D. The following output should appear:
Adafruit CircuitPython 3.1.2 on 2019-01-07; Adafruit CircuitPlayground Express with samd21g18
>>>
>>>
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
hi there

Press any key to enter the REPL. Use CTRL-D to reload.
  1. Edit the main.py file and change the contents to print('hi there again'). The following output should be automatically displayed:

soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
hi there again

Press any key to enter the REPL. Use CTRL-D to reload.

How it works...

By pressing Ctrl + D, the board will enter into auto-reload mode. In this mode, you can open the main.py file in your text editor of choice, and, the moment you save the file, the board detects that a change has happened and performs a soft reboot.

The soft reboot can be seen in the Screen output and then the new version of the code is executed with its output displayed immediately.

There's more...

It is quite common to start a script with a few basic lines of code to get the initial part of a script functioning. Once you have your first basic version running, you will go through many iterations to tweak and enhance it so that it behaves just the way you want it to. Beyond these tweaks, the inevitable bugs will appear in your code as you wrangle it into submission. The auto-reload feature will become your best friend during these intensive coding sessions as it will let you get results much faster and in an intuitive way.

See also