Wemos micropython min

#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:

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.

ESP8266 vs. ESP32
Fig. 1: Main differences between the ESP8266 and the ESP32

The Wipy 3.0 is designed and produced by pycom and it is based on a ESP32 dual core with the following specifications. It has more RAM (4MB) than the ESP32.

If you don't have any of these boards, in the following table you find the links to buy them.

WeMos WeMos WiFi ESP32 Development Tool x 1
WeMos D1 Mini ESP8266 x 1
WiPy 3.0 WiPy 3.0 x 1

MemoryError issues

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 .mpy files.

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.

Installing dependencies

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:


Python Code: https://github.com/lemariva/uPySensors

GPS Sensor: Ublox NEO-M8N & Ublox NEO-6M

I've already written two posts for these GPS Modules:

MIC: SPW2430

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.

HC-SR04HC-SR04 x 1


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:

ESP8266 & HC-SR04 ESP32 & HC-SR04 Wipy3.0 & HC-SR04
Fig. 2: ESP8266 & HC-SR04 ESP32 & HC-SR04 WiPy 3.0 & HC-SR04


A sample program can be found on:

Python Code: https://github.com/lemariva/uPyDistance

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_esp32.py or boot_wipy.py to 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.

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.

MPU9255 MPU9255 x 1

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.

BMP280 BMP280 x 1

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! ;).

GY-91 GY-91 x 1

{{ message }}

{{ 'Comments are closed.' | trans }}