Tutorial MicroPython ESP32 ESP8266 HC-SR04 BMP280 WiPy MPU9255
#Tutorial: Getting started with MicroPython: Sensors - Part 1!
One of the most visited articles on my blog is the Getting started with MicroPython! tutorial. If you still don't know what is MicroPython and you want to start programming microcontroller boards (ESP32, ESP8266 etc.) using a small version of Python 3, you should look at that article . Don't expect to use machine learning or/and other big libraries (the available RAM and microcontroller performance is far away for that aim), but you can do some projects like these:
- Micropython: Merry Xmas & Happy New Year!
- White Hacking: Wemos & Captive Portal using MicroPython
- MicroPython: Geolocation using WLAN
- MicroPython: WeMos and Amazon Echo (Alexa) - Switching LED Colors!
- MicroPython: Vu meter using WS2812B and SPW2430
- MicroPython: Weather Report using NEO-M8N
- MicroPython: Christmas lights!
- MicroPython: Camera stabilisation application! (NEW! 16.06.18)
In this post I'll be extending that tutorial to add some sensors to these boards. It's a work in progress; I'll be updating the sensor support frequently. I'll be using the ESP8266, the ESP32 and the new WiPy 3.0. They are all MicroPython compatible system on a chip microcontrollers, but there are many differences related to the functions that the boards support because of the hardware itself and the companies that produce/design them.
ESP8266, ESP32 & WIPY3.0
Let's start with an overview of the available hardware and the differences. Amica.io twittered a table showing the main hardware differences between the ESP8266 and the ESP32 which are expected to coexist in the market, with ESP8266 used in the lower cost segment (only WiFi support), and ESP32 in applications requiring WiFi and Bluetooth.
|Fig. 1: Main differences between the ESP8266 and the ESP32|
If you don't have any of these boards, in the following table you find the links to buy them.
|WeMos WiFi ESP32 Development Tool||x 1|
|WiPy 3.0||x 1|
You can usually get the following error using MicroPython:
MemoryError: memory allocation failed, allocating xxxx bytes. The RAM is very limited, especially on the ESP8266. If you run into this issue, you can cross-compile the python files that you want load to the board to reduce the RAM use.
To do that, you need to use Linux (sorry) and clone the repositories:
Then, go to the folder
mpy-cross using a terminal, and type
make. Wait a couple of seconds/minutes. Then, you can compile the python files using
./mpy-cross <filename.py> to get the
Then, load only the
.mpy files not the
.py files! Otherwise, the board tries to load the
.py and you get the same error again.
On the ESP32 (WeMos) or the ESP8266, it is possible to install packages (libraries etc.) using the upip package manager. The WiPy doesn't have the package. If you want to know more about that, look at this post:
GPS Sensor: Ublox NEO-M8N & Ublox NEO-6M
I've already written two posts for these GPS Modules:
The SPW2430 is a small, low-cost MEMS mic with a range of 100 Hz - 10 kHz, good for just about all general audio recording/detection. I've already written a post using it:
Proximity Sensor: HC-SR04
The HC-SR04 emits an ultrasound at 40 kHz which travels through the air and if there is an object or obstacle on its path it will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.
If you haven't heard about fritzing, you should take a look at its website! The official website says "Fritzing is an open-source hardware initiative that makes electronics accessible as a creative material for anyone". The following connection diagrams were created using fritzing:
|Fig. 2: ESP8266 & HC-SR04||ESP32 & HC-SR04||WiPy 3.0 & HC-SR04|
A sample program can be found on:
The sample program loads the driver and connects to a Wi-Fi router allowing the user to see the distance using a browser. You need to rename the files
boot.py depending on your hardware (ESP or WiPy) and modify the SSID and wpa-password variables in the file. Use Atom and the plugin Pymakr to upload the program to the board.
Inertial Measurement Unit (IMU): MPU9255 (
Coming soon click here!)
The MPU9255 is a 9-axis sensor which packages the accelerometer, gyroscope, magnetometer together each provides data for x, y and z axes and it has also a temperature sensor.
Barometro: BMP280 (Coming soon)
The BMP280 is an absolute barometric pressure sensor specially designed for mobile applications.
This precision sensor from Bosch is the best low-cost, precision sensing solution for measuring barometric pressure with ±1 hPa (± 10 cm) absolute accuracy, and temperature with ±1.0°C accuracy.
10DOF-Sensor: GY-91-MPU9250-BMP280 (Coming soon)
This board combines the MPU9250 (a lot of people are saying that it is the MPU9255, I'll check that!) and the BMP280. Combining the two sensors, you get 10DOF! ;).