Magnus Thomé Turns to a Capacitive Sensor for Corrosion-Free Rain Logging in Home Assistant

Tired of losing resistive rain sensors to corrosion, Thomé switched to capacitive versions — and shows you how to do the same.

Maker Magnus Thomé has come up with a way to interface a low-cost capacitive rain sensor with Home Assistant and other MQTT-compatible home automation platforms — using an Espressif ESP32 microcontroller and a 3D-printed housing to keep things neat and tidy.

"I've previously used those common resistive sensors that have copper traces that the rain falls on. The problem is that these boards tend to deteriorate after a while due to oxidization," Thomé explains of his move to a capacitive sensor. "A capacitive sensor is totally electrically isolated from the rain and damp. By having no external electrically connected sensor parts the main problems with wear in an outdoor environment are removed. A bonus I found with the capacitive sensor I use is that it reacts much faster to changes in rain intensity compared to the resistive one I had previously."

Housed in a smart 3D-printed case, this capacitive rain sensor avoids the corrosion problems of resistive versions. (📷: Magnus Thomé)

The sensor in question is the RC-SPC1K from Italy's Radiocontrolli SRL, a low-cost device which combines the sensor itself with a built-in heater — a bonus for deployment in Thomé's home of Sweden, where things often get snowy and frosty. What the sensor doesn't have is any smarts of its own — which is why Thomé put one under the control of an Espressif ESP32 microcontroller, writing code to keep the sensor ticking over and to communicate its findings to Home Assistant over MQTT messages.

"When the sensor detects rain or snow the code keeps the sensor at around 45 degrees Celsius [113°F] by sensing the NTC resistor value and turning the heater on and off (with a transistor) quite quickly around a small temperature hysteresis," Thomé explains. "The measurement of the capacitance is done by first making sure the capacitor/sensor is fully discharged by pulling the GPIO pin connected to it to ground.

"Then that pin is turned to a high impedance input and another pin goes high and starts charging the capacitor slowly through a 1Mohm resistor while the first pin is measuring the rising voltage directly on the capacitor. When the voltage reaches 63 per cent of the full voltage the time between the start and stop of this charging sequence is saved. The larger the capacitance, when raining, the longer the charging time."

The only complication — bar keeping the electronics dry — is the need to drive the sensor's heater with 12V but an ESP32 with 5V. (📷: Magnus Thomé)

The parts require for the build are relatively limited — just the sensor, an NPN transistor, two 1kohm resistor and one 1Mohm resistor, plus an ESP32 board — but there's one catch: "The sensor's heater runs on 12V, so that is the voltage you need to supply to your build," Thomé writes. "You normally need a DC-DC buck converter to power the ESP32 at 5V, [but] I happened to have an ESP32 board that can run on 12V natively so I just had to add a few components to interface it with the rain sensor."

A wiring diagram, source code, and STL/STEP files for printing the sensor housing and its mount are available on Thomé's GitHub repository under an unspecified license.

ghalfacree

Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.

Latest Articles