I started my professional journey after engineering school in the world of microcontrollers and printed circuit board design and fabrication. It was a self funded research with a lot of learning with a close friend but did not evolve into a full business idea at the time. The telecom sirens called shortly after, and I took on a role with Alcatel in the Mobile Networks Division in Cairo.

Recently, I have been thinking about a DIY project for water leak sensors. Homes in North America are made with wood frames and drywall sheets, a water leak beside being wasteful can be devastating to the home structure and comfort. several companies have sensor pods that connect to a central hub by Wifi, Zigbee or Bluetooth and can alert and also possibly control an actuated main water valve to shutoff water if a leak is detected. Several commercial products exist with costs ranging from 500$ (self install) up to 3,000$ (full service including insurance certificate to reduce home insurance premiums). The cost of making a single water leak detection sensor could not exceed 20$, and there were a lot of home automation platforms around, with some open source options as well. It was time to take out the breadboard and the soldering iron!

Atmel AVR !T89S52 microcontroller

Looking back at the type of microcontrollers I started using a little over 20 years ago, it was a mix of Atmel 8051, Atmel AVR (Atmel was acquired by Microchip in 2016), and Microchip PIC 16F and 18F devices. I designed the programmer boards and most communications were through RS-232 (serial port) with the PC. Programming them was initially in assembly language (The nightmare of getting anything done with that).

Then C compilers for these microcontrollers started to become available and created a leap in productivity. although less efficient in size and control compared to assembly, it allowed quick prototyping and re-usable code. Since then I have worked with Arduino Uno in a limited fashion but it was clearly not the right form factor for the water leak sensor pod.

Microcontroller platform selection

I started researching some other products, the key criteria being:

  • Cost effective
  • Small form factor
  • Low power consumption
  • Has wireless communication (ideally Wifi) with an integrated antenna
  • Available for mass consumers

My quick research led me to these potential candidates

  • Arduino nano at around 14$
  • Arduino nano at 33 IoT 27$
  • Arduino nano at ESP32 21$
  • ESP32 at around 10$

ESP32 System On a Chip (SOC) overview

The ESP32 microcontroller stands out as an exceptionally relevant and cost-effective platform. Developed by Espressif Systems, the ESP32 combines powerful features with affordability, making it an ideal choice for makers seeking to build smart home devices without breaking the bank.

ESP32 Development Board
ESP32 Development Board

Key reasons why the ESP32 is so well-suited for DIY home automation include:

  • Integrated Wi-Fi and Bluetooth connectivity: This allows seamless wireless control of lights, thermostats, sensors, and other appliances, enabling remote access and automation through smartphones or voice assistants.
  • Dual-core processing power: The ESP32’s dual-core Tensilica processor supports multitasking and efficient execution of complex tasks, which is crucial for managing multiple sensors and devices simultaneously.
  • Rich peripheral support: With numerous General Purpose Input Output GPIO pins and support for protocols like SPI, I2C, UART, and PWM, the ESP32 can interface with a wide variety of sensors and actuators common in home automation projects.
  • Low power consumption: Its advanced power management and sleep modes make it suitable for battery-powered or energy-efficient applications, extending device longevity.
  • Security features: The ESP32 offers secure boot, flash encryption, and hardware cryptographic accelerators (AES, SHA, RSA), ensuring data protection and safe operation in connected environments.
  • Cost-effectiveness and accessibility: Modules like the ESP32-C3 provide a budget-friendly option with essential features, including Wi-Fi, Bluetooth 5.0 LE, and a compact form factor, making it accessible for hobbyists and scalable for larger projects.
  • Strong community and development support: Compatibility with popular frameworks such as Arduino, ESP-IDF, MicroPython, and FreeRTOS means both beginners and advanced users can find ample resources and libraries to accelerate development.

If you have bought a WIFI/BLE connected device or small appliance in the past couple of years, chances are that it is powered by ESP32 or a variant of it.

The ESP32 platform offers a broad spectrum of abstraction levels for programming, ranging from low-level C/C++ development to highly abstracted, configuration-based frameworks. One of the most significant advancements in this space is ESPHome, which introduces a unique, user-friendly abstraction layer tailored for home automation and IoT projects.

Development environments for the ESP32

  • Low-Level (Register/Hardware Access): Using the ESP-IDF’s LL (Low Level) or HAL (Hardware Abstraction Layer), developers can write code that interacts directly with hardware registers for maximum control and efficiency.
  • Driver Layer: The ESP-IDF also provides peripheral drivers (UART, SPI, I2C, etc.) that abstract much of the hardware complexity, offering a balance between control and ease of use.
  • Arduino Framework: For many DIYers, the Arduino IDE offers a high-level, C++-based abstraction with a vast ecosystem of libraries, making it easy to get started with ESP32 development.
  • MicroPython, nanoFramework, NodeMCU: These environments allow programming in Python, C#, or Lua, respectively, providing even higher-level abstractions for rapid prototyping and educational projects.

Configuration-Based Abstraction (ESPHome)

ESPHome stands out by taking abstraction to the next level. Instead of traditional programming, ESPHome enables users to define device behavior and integrations using simple YAML configuration files. This is similar to cloud native declarative infrastructure concepts brought to a Microcontroller level. This approach eliminates the need to write code for most common tasks:

  • No Programming Required: Users specify sensors, actuators, Wi-Fi credentials, and automation logic in YAML. ESPHome generates and compiles the firmware automatically, handling all the underlying C++ code and device initialization.
  • Seamless Home Assistant Integration: Devices configured with ESPHome appear automatically in Home Assistant, enabling easy control and automation within a unified smart home ecosystem.
  • Over-the-Air (OTA) Updates: Firmware can be updated wirelessly, streamlining maintenance and upgrades.
  • Extensibility: Advanced users can still inject custom C++ code if needed, but most projects can be completed without leaving the YAML configuration.
  • Security and Local Control: ESPHome supports encrypted communications and operates without mandatory cloud dependencies, ensuring privacy and reliability.

Micropython on ESP32

For the purpose of this project I have opted to use Micropython.

After some quick searches I decided to use uPyCraft as the IDE and to also flash the Micropython firmware the first time using this guide:

In the next parts, I will go over the sensor hardware and the initial codes “hello world” for sensors and output and we will evolve this into a connected sensor with notification to a smart device.


Discover more from Wasla

Subscribe to get the latest posts sent to your email.

2 responses to “Home automation / IOT sensor projects – water leak detection – Part 1”

  1. Home automation / IOT sensor projects – water leak detection – Part 2 – Wasla Avatar

    […] the first part of this series, I explained the Microcontroller hardware system choice which is the ESP32 provides […]

    Like

  2. Home automation / IOT sensor projects – water leak detection – Part 3 – Wasla Avatar

    […] selecting the hardware platform in part 1, selecting the sensor and beginning to interact with it through MicroPython in part 2, now […]

    Like

Leave a comment