Humidity Sensor

posted at 2025-11-05 in coding and electronics

Idea

For work we needed a method to measure the relative humidity of plant soil in a quick way. Multiple hundred samples were supposed to be measured in a feasible time.

Implementation

I had an unused EPS with OLED display from Lilygo laying around, so I used this to write a bit of C++ code, that will store the output of a capacitive humidity sensor in a csv file on a SPIFFS file system and prints this file via serial when connected to a computer.

Since the Lilygo board contains it’s own charging circuit, it was easy to add a battery. A more or less ergonomic 3d printed casing finishes this project.

The microcontroller takes the 12 bit output of the ADC where the capacitive humidity sensor is connected, and uses linear regression to output the corresponding relative humidity.

For each type of soil and for each sensor, the factors of the linear regression curve needs to be recalculated. Unfortunately I couldn’t come up with a good way to do this calibration inside of the microcontroller. Instead the factors are hard coded and the code needs to be freshly flashed on the controller, each time. Maybe in future I can solve this problem, or at least create a file on the SPIFFS, where old regression factors are stored and can be loaded back.

Results

the first practical test showed that the device was working okay. The linear regression showed an R² of above 0.98 which was very satisfying. We realized, that the results of the sensor also depend on the compactness of the soil, probably by changing the amount of air around the sensor. If you push looser soil a bit towards the sensor, the relative humidity rises by 2 – 3 %. For our purposes of comparing plants with and without draught stress, this precision is high enough. For a more precise approach, the capacitive sensor will not be sufficient, though.