Skip to content

Instantly share code, notes, and snippets.

@aufi
Last active February 8, 2021 07:32
Show Gist options
  • Save aufi/53fae462bd15d83e9946947f4e3bf604 to your computer and use it in GitHub Desktop.
Save aufi/53fae462bd15d83e9946947f4e3bf604 to your computer and use it in GitHub Desktop.
RH Learning day - IoT ESP32 ultrasonic radar

Learning basics of Microcontrollers programming

Intro

I'm interested in IoT stuff, so far I've used Raspberry Pi mini computers and their GPIO. I'd like find out if microcontrollers can replace "heavy" Raspberry Pi with Linux for IoT sensors&controls use cases.

Hardware

I ordered three microcontrollers: Rapsberry Pi Pico, NodeMCU ESP8266 board and M5Stack Atom lite box with ESP32. Will find which one fits HW&SW to the needs.

Software

An ideal state would be to program it with golang, however not sure how capable are go-based environments. Possible choices are Gobot, TinyGo or MicroPython.

Gobot looks quite nice, but with less common firmware setup (Firmata protocol) and supports only ESP8266. TinyGo doesn't support WiFi on ESP controllers, so it is out. MicroPython supports everything, requires firmware update, but not complicated. However firmata looks as a promising technology, The MicroPython seems to be save and reasonable choice for (hopefully) smoothly working setup.

What to build

An example project is needed to verify HW&SW capabilities. Something related to self-driving vehicles looked to be ineteresting, so why not start with an Ultrasonic Radar distance measure with servo moving it to different directions (max.range few meters).

Software environment setup

See Makefile.

Hardware setup

I've missed that the ulrasonic measure unit (HY-SRF05) uses 5V voltage, so the M5Stack Atom with ESP32 seems to be only suitable controller with Wifi. ESP itself works with 3,3V, RPi Pico doesn't have embedded Wifi and reading the Radar output remotely is a nice-to-have feature.

Ultrasonic distance measure

Measure works on ping-pong scheme and reflection roundtrip time is measured, so the distance can be calculated. The ultrasonic unit has specified to have 15° receiving angle, howeer it seems to be more in practical usage.

Threads are needed (and luckily are supported on the ESP chips) for handling measures capture and providing it via e.g. network socket. For time reasons I deffered providing measures nearly real-time via Wifi, but it is a first improvement I'd do next.

Conclusion

I learned that (at least) ESP-based microcontrollers are nice and it is easy to start working with it. It is reasonable use "bundled" boards which make easier integration with peripherals (different voltage, less accessible pins, but can supply more power) and such boards can replace full Raspberry Pi in some of my home automation use cases.

The MicroPython is well supported, simple and quite smoothly working. I'm looking forward to see golang microcontrollers tooling more mature.

Check today's work result in action at https://youtu.be/TSi9DF0nxwA

Resources

https://docs.micropython.org/en/latest/esp32/quickref.html

https://micropython.org/download/esp32/ esp32-idf4-20210202-v1.14.bin

DEV ?= /dev/ttyUSB0
setup:
sudo dnf install -y wget esptool micropython picocom ampy
wget https://micropython.org/resources/firmware/esp32-idf4-20210202-v1.14.bin
flash:
esptool --port ${DEV} erase_flash
esptool --chip esp32 --port ${DEV} --baud 115200 write_flash -z 0x1000 esp32-idf4-20210202-v1.14.bin
program:
killall picocom || true
ampy --port ${DEV} put main.py
# picocom -b 115200 ${DEV}
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment