Introduction: Capacitive Moisture Sensor for Bonsai Tree (Wemos With 555 Timer Based Sensor)
A friend recently left town and gave me his prize bonsai tree as a gift, leaving strict instructions on how to water and feed the plant. Needless to say, 2 days went by and the plant was looking very sad from a lack of water. This was the perfect time to put together a quick moisture sensor and connect it to my home cloud and reporting service.
What started out as a simple exercise turned out to be quite a challenge, especially given the behaviors of water in soil and its related chemical composition.
I started with 2 different resistive sensors and quickly worked out that these were inaccurate, fluctuated quite a bit, and were even influenced by temperature (the bonsai spent 4 hours in the sun and the readings changed dramatically during this time). Experience from other readers attempts to do this also indicate that the copper plating corrodes within 3 months therefore rendering it useless.
I therefore turned to a capacitive sensor, and given that a commercial version would take 5 days to deliver (JHB, South Africa), I went about trying to create my own sensor. My journey was not easy, and the different options I tried failed dismally, until after much reading and research, I stumbled across a version that works really well.
Let's start with what failed. This is important because it may help you understand the challenges of working with a capacitive sensor, and also assist you come to your own conclusion using my results as evidential input.
- Resistive sensors are easy to implement but are not reliable:
- They are prone to wild fluctuations and inaccurate readings.
- The range of readings returned is very wide (using A0 as the voltage reading pin)
- The sensor corrodes with time.
- The sensor is affected by temperature quite considerably
- Capacitive sensor with Wemos signal generator
- First attempt was to make a single sided copper coated capacitive sensor
- Using "pulseIn", I attempted to measure the pulse time (frequency) of a square wave generated by the Wemos D1 Mini Pro, the logic being that the longer the pulse time the higher the capacity of the sensor, the higher the moisture in the water).
- The sketch was able to read the pulses, but the readings never changed, indicating an issue with the capactive plate or internal issues with the wemos pulse and voltage readings.
Research into this problem indicated that either the single sided plate was not able to provide readings that were sensitive enough to the change in moisture, or the Wemos signal generator coupled with the input pin, could not correctly read the changing pulse width.
Enter the 555 timer. The research highlighted that many capacitive sensors have been based on the 555 timer. The timer (using an astable mode) is able to generate a square wave whose width changes proportionately to the increase / decrease in the soil moisture. By using the width of the pulse, we can calculate frequency (1 / pulse duration in seconds) and therefore determine the relative moisture against a frequency range and ideal moisture value.
After reading many instructables and other web inputs, I came to the conclusion that no one resource provided a comprehensive explanation on how to create all the aspects of this sensor and its processing. Whilst some Instructables give great insight into the copper patterns on the sensor stick or describe the connections on the 555, there is a gap on how to put it all together.
This instructable is therefore an attempt to provide a comprehensive end-to-end view of how to create a capacitive sensor for an Arduino based MCU, and how to publish that data to a web service in the cloud for storage and later viewing.
Step 1: The Probe
The first round of developing the solution involved using commercial resistive probes / sensors. For the reasons described above, these did not provide the accuracy and value I required (probes shown in picture 1 and 2 above).
My first attempt at a capacitive probe involved a single sided PCB that had the middle cut out so as to create a positive and a negative plate. The difference I got in my readings from dry to pure water were very small and did not suit the sensitivity I required. Further reading of this instructable (Capacitive Soil Moisture Measuring (with I2C)) provides excellent insight into probe designs.
With the input from this Instructable, I created a rudimentary double sided probe with opposite poles on each leg (see pictures)
It must be remembered that a capacitive probe must not be directly exposed to the soil / moisture as this will short the capacitor (soil / water mixture), so it is important to seal the probe. I chose to you a non-conductive epoxy which had a 30 minute curing time (See picture of probe coated in epoxy, hanging whilst it cures).
For testing purposes I put the prove into a plastic bag before inserting it into the soil mixture.
Step 2: The 555 Timer Control Unit
The 555 timer control unit was used to replace the square wave generated by the Arduino MCU. A significant difference between the two approaches is:
- The Arduino pin generates a fixed frequency square wave (I was using a range of 600 to 1000 hz), therefore you need to measure the pulse duration of the capacitor output in order to determine capacitance and by implication moisture. I could NOT get this right!
- The 555 timer generates a square wave whose duration is influenced by the size of the capacitance received from the probe by the threshold / trigger pins. As the capacitance of the soil increases / decreases, so the timing of the trigger on the 555 changes proportionately.
Using option 2, we are able to measure the time it takes for a set number of pulses to occur. If the soil is dry, the capacitance is low, and therefore the time taken to complete the set number of pulses will be low. If the soil is moist, the capacitance is higher, and therefore the time take to complete the set number of pulses will be higher. By comparing the values against the readings taken for air (~100% dry) and a glass of water (~100% wet) we are able determine the relative moisture of the soil.
The circuit for the 555 timer is shown in the Fritzing above.
A prototype was developed on a breadboard. This proved to be a painless affair and worked first time.
For the final build in order to keep the probe controller as small as possible and as close to the probe as possible, I chose to solder all the components directly onto the 555 chip, and seal this in an epoxy. This allowed me to bury the timer circuit under the soil together with the probe.
A picture of the complete sensor and sensor being cured in the epoxy is shown.