Introduction: MicroPython on the Complex Arts Sensor Board
One of the most amazing aspects of the ESP32 microcontroller is its ability to run MicroPython. This can be done in two ways: running full Python programs, or interactively through a console application. This Instructable will demonstrate how to use MicroPython is both ways using the Complex Arts Sensor Board. We’ll first run an example application that gathers accelerometer data from the BNO_085 IMU, then we’ll use a serial program to interactively program in Python.
Level: This tutorial assumes some knowledge of Python, and that Python is installed. It also assumes knowledge of basic terminal commands.
Tools: The only tools we’ll need will be the Sensor Board, a terminal program, and a serial console program. On a Mac, you can simply use terminal. On a Windows machine, you’ll need to download and install a terminal program. For the serial console. Putty is always a good choice.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Getting the Firmware and Libraries
To get started, we’ll need to download the custom firmware provided by Complex Arts and then flash it to the Sensor Board. The firmware can be found here: https://github.com/ComplexArts/SensorBoardPython/...
Download the firmware.bin file and place it in the folder of your choosing. You’ll also eventually need the Complex Arts example program, so might as well do that now; go to: https://github.com/ComplexArts/SensorBoardPython and git clone or download to the location of your choosing.
Once you’ve got the files, we’ll need a few packages for interfacing with the ESP32. The first package we’ll need is esptool.py. To install it, simply type
pip install esptool
in the terminal.
Once esptool is installed, we can erase then re-flash the chip. To do so, enter
esptool.py --chip esp32 --port COM4 erase_flash
for the port, enter the serial port that coincides with the Sensor Board. On a Mac, that would look something like --port /dev/ttyUSB0
Once this is done, we’ll flash the chip with:
esptool.py --chip esp32 --port COM4 --baud 460800 write_flash -z 0x1000 firmware.bin
Again, change the port accordingly.
Step 2: Working With MicroPython
If we check the serial output of the Sensor Board at this point, we’ll see the Python REPL (the read-eval-print loop: >>>) In order to do this, we’ll need a serial console program. Putty is a good option as it provides options for SSH and telnet, but also simple serial communication as we’ll be doing here. putty.org. Once you’ve installed that, open it and select “Serial” under “Connection type:” You’ll need to type in the same serial port name you entered for the esptool above, then a baud rate of 115200 for the Speed. Go ahead and click “Open”. And there’s Python!
Now we’ll want to upload and run our example code. To do that, go to the folder where you earlier saved the SensorBoardPython examples. We’ll need Adafruit’s awesome ampy package. You can install that with:
pip install adafruit-ampy=0.6.3
Once you have it, use ampy to upload the accelerometer example to the board:
ampy -p COM4 put accelerometer.py
(changing the port accordingly, of course). Now reset your board with the reset button. We’ll go back to Putty at this point and at the >>> prompt, type
Viola! You’re now running the accelerometer.py code on the Sensor Board! The code will run for 20 seconds, then halt. Notice that while the accelerometer code is running, the blue LED on the board blinks. For those more familiar with Python, you’ll notice this is done without threading and without delay(). That is due to the use of Python’s asyncio library, which provides great methods for running tasks simultaneously and is super useful on embedded platforms such as the ESP32. If you’re not familiar with it, it’s worth checking out; there’s a great tutorial here: https://github.com/peterhinch/micropython-async/b... (but be warned, its’ a little heady).
Step 3: MicroPython Over Serial
Back to the task at hand! When the accelerometer code halts, you’ll again see the Python >>>. Now we can interactively program using the Sensor Board as our Python interpreter. To do so, enter
>>>x = 10
>>>y = 11
>>>x + y
While this is the most basic of examples, we can easily begin to create more complex code using the Complex Arts libraries as a starting point. This enables you to run positional measurements and motion on the fly, with real-time control. With the GPIO pins available on the Sensor Board, you can readily connect servos, lights, sensors, motors, or any number physical devices, using them either interactively, or through a Python program. Have fun!
For more information, check out these other resources: