Logging Capacitive Soil Moisture Sensor
by Jimbo Rand in Circuits > Sensors
303 Views, 3 Favorites, 0 Comments
Logging Capacitive Soil Moisture Sensor
This self-contained, capacitive soil moisture sensor is designed for long-duration deployment hidden deep (up to 30cm so far) within soils. It logs soil moisture data at 15-minute intervals for approximately 1 month between each battery change.
This has been designed for repeated deployments by the University of Bath, GP4Streets team to monitor the vertical and horizontal hydraulic performance of rain gardens and other urban greening initiatives.
The novel aspect of this sensor, compared to other similar Instructables, is that power to the sensor blade is only supplied when measurements take place - this ensures long sensor life and better accuracy as the blade does not actively corrode in the soil.
The sensors (40+ deployed to date) have provided a consistent soil moisture measurement response over 6 months (6 x 1 month, back-to-back deployments) and are estimated to be accurate to about 2%WW soil moisture (weight of water/weight of soil)
Supplies
For each sensor:
Waveshare ESP32-c3 microcontroller development board
Capacitive soil moisture sensor blade (check out the image - they are widely available)
2N2222A/PN2222 transistor (or similar)
3.3kΩ resistor
Vero board (or similar circuit board material)
Female and male header pins (straight and 90°)
30mm diameter plastic pipe (for plumbing)
4 x AA battery holder
4 x AA batteries (zinc type - very important - see Step 9 for details)
3D printer - endcaps and blade housing (see Step 4 for the .stl files)
Potting Resin
Silicon Sealant
Set Up the ESP32-c3 Board
Even if you have no experience with ESP32 boards, do not worry - they are fairly simple, are really flexible, have onboard memory and (for the price) are powerful. If you have used a Raspberry Pi or Arduino board, then you will have more than sufficient experience to use one.
There are plenty of examples available on the web on how to set up an ESP32 board and get micro-Python (a scaled-down version of Python) up and running (eg. https://docs.micropython.org/en/latest/esp32/tutorial/intro.html).
However, in short (see image): Download the programme Thony (https://thonny.org/), connect the ESP32 to a PC with usb cable, wait whilst it all does its stuff, select your board type from the dropdown file menu (bottom right of the screen), select micro-python for ESP32 and the suggested com port (accept any other default stuff), press burn, and then you can use the board.
Solder 5 straight male header pins to the ESP32 board on pins 5v, Gnd, 3.3v, Pin 0 and Pin 1 (eg. the first 5 pins - see the image)
Assembly of the Circuit
In this step, the circuit board with the header pin connections to attach the ESP32 and sensor blade will be constructed.
First, cut the vero board (or whatever you are using) into the size as shown in the image, and cut 2 of the copper tracks/strips as shown in the images (and indicated on the diagram by the white blocks).
Place the female header (block of 5), 90° header (block of 3), transistor (ensure correct orientation - see image and diagram), resistor and battery pack leads into the vero board (all as indicated by the image) and solder. Clip off tails neatly (hopefully your soldering is better than mine).
Cut the Sensor Housing to Size
Cut the 30mm plastic pipe to size. It needs to contain the battery pack, the ESP32, the circuit board and a bit of the wire. With the components used, this came to 110 mm (but you are advised to check the length required for the components you are using).
Using a Dremel tool, hacksaw blade, file or similar, cut about a 9-10mm slot into the body (this will allow the sensor wires to exit) into the housing (see image).
3D Printing
Using the attached .stl files (modify them (a) depending upon how many sensors you are building, and (b) if your 30mm pipe diameter is slightly different (these end caps files fit a pipe outer diameter of 42.9mm - measure the pipe outer diameter and check against the .stl drawing - it should be a snug fit), 3D print (using PLA) one blade enclosure and two end caps for each sensor you are making.
The images show the production line approach to manufacturing the many sensors needed for the GP4Streets project.
Waterproofing the Endcaps and Soil Moisture Blade
The electronics and connectors at the top of the soil moisture sensor blade need to be made waterproof. This is done by placing the top of the blade in the 3D-printed blade enclosure, plugging the bottom with plasticine or sticky tape (to stop any leakage) and then filling the enclosure with potting resin.
Similarly, the insides of the endcaps need to have a layer of potting resin poured into them to make them waterproof (3d prints are not waterproof !!!).
An alternative is to use silicon sealant rather than potting resin - both methods work but the resin option is probably better.
If using silicon, care needs to be taken to fill any holes and gaps, as it does not "flow and fill" like potting resin does.
In addition, silicon sealant does not always become bound to the 3d print material, so it can become detached after use.
Before the resin dries and hardens, one end cap should be attached to the housing body. Take the 30mm pipe plastic housing, coat the end without the slot with some resin and attach into one of the endcaps.
Leave the housing/endcap upright and allow all components to dry/harden.
The image shows how a cardboard box was used as a stand during this resin filling process.
Micro-python Code
The attached micro-Python code boot.py and main.py should be loaded onto the ESP32.
Similar to an Arduino, when power is applied to the ESP32, the microcontroller first executes the instructions in the boot.py file, then runs the main.py file.
The boot.py file sets the clock speed of the ESP32-C3 to its minimum (reduces power consumption - current draw reduced by about 5mA).
Note: If you are an experienced ESP32 user and the board has been used for other projects, ensure that WiFi and Bluetooth are switched off (to minimise power consumption) in the boot.py file also (there is no external communication with this sensor - it will be buried underground, so none of these will work in anycase).
It should be noted that (a) the ESP32 clock is not a Real-Time Clock (RTC) and only measures elapsed time from the board being powered up (noting the start time is therefore vital - write it on the casing with permenant marker !), and (b) the clock is not particularly accurate, but for 15-minute monitoring is more than good enough.
The main.py file reads the analogue soil moisture sensor via an ADC pin (converting it to a digital value), writes the value and a time-stamp to the onboard memory (4MB) keeps the sensor powered for 5 seconds (5 seconds of normal sleep) before (using the transistor as a switch) powering off the moisture sensor (if this is not done,: power is consumed needlessly, the sensor blade can actively corrode driven by the voltage, and the sensor measurement become unreliable/inconsistant as a result) and entering the ESP32 into a deep sleep. After 15 minutes, the board wakes, powers up the moisture sensor, and the measurement process repeats.
When running, the ESP32 (with reduced clock speed) uses about 15mA of current. When in a deep sleep, it uses only about 5µA !!!! The deep sleep is therefore used achieve significant power savings and extend battery life.
However, deep sleep can be slightly tricky in micro-Python, and it is possible to put an ESP32 into a sleep from which it may never wake (or rather you cannot interrupt from sleeping). The 5-second (normal) sleep in the code is a window of time between taking a soil moisture measurement and a deep sleep that you can use to break into the sleep/wake cycle, stop the ESP32 from running, and so download data, update code, etc. Stopping the ESP32 from running is achieved by selecting a different micro-Python version from the dropdown menu in Thony within 5 seconds after powering up (or pressing the reset button) the board (see red circled detail in image). Just select any Python option within 5 seconds, and the ESP32 will stop running and not enter deep sleep.
If you like a challenge, please feel free to reduce or remove this 5s sleep (but if you discover how to break into a deep sleep, please let me know how - that would be really useful).
Testing
To check the sensor functions, change the deep sleep time in the script to 5 seconds (see image) and save to the ESP32. Power it up (unplug and plug back in the usb cable is probably best - you will hear the laptop ping every time the board starts to sleep and deep sleep), and then stop the board after a bit (see step 6 on how to break into the sleep/wake cycle) and you should be able to see and open a text file: moisture_log.txt with the time since power up and the corresponding raw digital mositure value.
If things don't work, have the ESP32 and sensor blade connectors been inserted correctly - see images.
Don't forget to change the Python script back to 15 minutes before you deploy the sensor.
Calibration
The combination of different sensor blades, electronic components, and ESP32 means that any 2 sensors built will not give the same number value for the same raw soil moisture value. Each sensor, therefore, needs to be calibrated.
The easiest way is to make up a known weight of dry sand/soil, add a known % of water, and then record the average of the raw moisture values. Once done with no added water, add 5% of the soil's weight in water to the sand/soil, mix well and take another set of average moisture values. Repeat until you have saturated sand/soil (about 35% for clay soils - it will be clear that the soil will not absorb any more water, and the raw moisture value will remain almost static regardless of how much more water is added). Each sensor will then have a calibration curve that can be used to convert the raw moisture value into a %WW (% weight of water/weight soil).
Note that some soil moisture measurements are reported using %VV (% volume of water/volume of soil) - instructions as above, but use volumes.
Add Batteries, Seal and Deploy
Once happy that the sensor functions and is calibrated, the batteries can be installed.
The first step is always to unplug the ESP32 board from your computer.
DO NOT LEAVE THE ESP32 CONNECTED TO YOUR COMPUTER AND INSTALL BATTERIES (you will, in effect, be banging 6 volts back into your computer via the USB port - not what it is designed for) - WHILST THIS PROBABLY WILL NOT KILL YOUR COMPUTER, IT WILL CAUSE YOU TO WORRY AND COULD RESULT IN WEIRD AND POTENTIALLY DAMAGING THINGS HAPPENING.
The Waveshare ESP32-C3 board has an internal DC/DC converter that operates when the board is powered via the 5v pin (which this project does). The voltage of 4 x Zinc type AA batteries (NOT Alkaline, NOT LiPo, only zinc type) is just at the ragged edge of the onboard DC/DC range (just outside, according to the data sheet, but with over 40 sensors powered this way, no ESP32 board has been killed using zinc type batteries). By using zinc type batteries, no additional voltage conversion is needed, this save significant power and allows about 4weeks of operation of the sensor.
That said, the moisture sensor will work if you use 4 x alkaline AA batteries, just not for very long.
Once batteries have been installed, it only remains to push the batteries, board and ESP32 into the sensor housing. The sensor blade cable should be arranged to exit the housing via the slot (see image), and then seal the end cap and slot using plenty of silicon sealant.
Note the time that the batteries were inserted (write it down on the casing and also note alongside your calibration data) and allow the silicon to dry before deployment.
If you forget to note down the time but have sealed the sensor end cap, you generally get a second chance. When the sensor blade is inserted into damp/wet soil, you will see a step change in the % moisture change data - this step can be used to work out the exact time of deployment.
To deploy, dig a hole and push the sensor blade into undisturbed soil. There is guidance from the Meter Group website on moisture sensor deployment that is worth reading to ensure consistent placement and readings.
Tips
To open the sensor to get to the ESP32 and your data, push a thin-bladed knife up into the end cap, run the knife around (without cutting the wires !!!!) and push the end cap off.
A top tip is to note down all times (batteries in, sensor in the ground, etc) and note the exact location sensors were buried, and take lots of photos.
The sensors can (even if you take lots of photos) be difficult to find after they have been buried in soil for 4 weeks (and very, very, very difficult to find if buried under grass). Plants can grow quickly, and the photos you took at deployment can be difficult to interpret to help you find the buried sensors. Attaching a nice bright tag to the sensor body (see image), which is laid out and then buried in the soil, can sometimes help your search as you probe around looking for a lost sensor.