Published on

A dedicated HC-SR04 library for MicroPython

Authors
  • avatar
    Name
    lalugue
    Twitter

I wanted a MicroPython library for the HC-SR04 module, so I ported one 🚢

What is an HC-SR04?

The HC-SR04 is a module for measuring distances via ultrasonic waves. It's designed for easier use with microcontrollers and is rather inexpensive too.

Why a library?

I wanted a reusable library or driver for my use in other projects and to help me focus more on the application side of these projects. I checked if there were popular libraries for controlling the HC-SR04 in MicroPython.

One of the libraries I found was Adafruit's CircuitPython library (repo). It looked great, plus it comes from Adafruit, a well-known source for user-friendly hardware, software, and documentation.

However, I wanted to specifically use MicroPython with my Raspberry Pi Pico/W devices. This gave me the idea to port such a popular library into MicroPython!

Technically, one could check the datasheet then directly script control for an HC-SR04, but I wanted a reusable library based on an established source. Additionally, it would be my small but hopefully helpful contribution to the MicroPython ecosystem and FOSS.

The library

The CircuitPython library was clean and well-documented, which were a great help while porting! Diving into the library was also a fun hands-on exercise on learning a bit more about the HC-SR04 📚 The port is here: link

What's next

With a MicroPython port, I now have a dedicated library that I could use with my own MicroPython projects. This could also open doors to potential optimizations for MicroPython-based HC-SR04 control!

As for what these projects do, stay tuned to my blog 🥂

Acknowledgements

I would like to acknowledge Mike Mabey, Jerry Needell, and ladyada, as this port stands on the shoulders of their work 🙇‍♂️