WaterTime

A Wireless Soil Moisture Monitor

This project is a wireless soil moisture monitor for gardening. It is a battery-operated unit that can be left in the soil and transmits an indication of the moister in the soil. In addition, a light sensor reading, a temperature reading, and the battery charge information is transmitted. An optional soil temperature sensor can be added to the circuit board for data logging.

The data is sent to a server using a standard Lora transceiver. This range should be several kilometers but was not tested. The unit just sends raw measured data, and the server should interpret the data as necessary.

The Hardware

Shown below is a picture of one unit before it is fully assembled.  It is comprised of a circuit board that is 50 mm wide.  The pointed end of the circuit board protrudes from the enclosure and is inserted in the ground.  The upper portion, where the electronic circuitry resides, is inside the enclosure along with a LiPo battery. There is a hole in the top of the enclosure into which a photoresistor is glued.   The TO92 package in the center of the lower section is the optional soil temperature sensor.

Figure 1. Unit before final assembly.  The box at the bottom has the light sensor glued into the top.  The box has a slot cut into the bottom through which the lower part of the circuit board will go.  The cover at the top of the picture has a velcro strap that holds the battery.

Soil Moisture Measurement

WaterTime used the capacitance method of measuring soil moisture.  The lower part of the circuit board has two large copper-filled areas that act as capacitive plates to the spoil.  They are not directly connected.  When the soil is moist, the capacitance measured between the copper-filled areas is high.  As the soil moisture decreases, the capacitance reduces.

In the schematic, you will see an oscillator built out of two op amps that is used to measure the capacitance.  The left op amp is an integrator, and the right op amp is a comparator with hysteresis.  When the integrator is integrating up and crosses the upper threshold, comparator output flips which causes the integrator to integrate down.  When the lower threshold on the comparator crosses the comparator’s lower threshold, the output goes low, causing the integrator to integrate up again.

The rate of integration and thus the frequency of oscillation is controlled by R6 and the sensor capacitor, which are the copper-filled areas mentioned earlier.  When the soil is moist, and the  capacitance is high, the oscillator will run at a lower frequency.  The frequency will increases as the soil becomes drier.  I typically see about a 2 to 1 frequency difference between wet and too dry.  The actually frerencies will depend on the depth that the devices is inserted into the soil.

The output of the comparator is connected to a GPIO input on the Tiny2040 microcontroller.  That GPIO is configured as an input to the PWM counter.  The frequency is measured by resetting the counter and then enabling it to count for 1 second.  The resulting count is the frequency.

Two things to note about the copper areas that make up the sense capacitor.

  1. The length of the sensor area in the supplied board layout is longer than it needs to be.  On some of the sensors, I cut several inches off the bottom to have it fit properly in some plant pots.
  2. The green solder mask on the circuit board is not durable enough to withstand forcing the circuit board into the ground without expoing some of the copper.  Any exposed copper will corrode quickly.  So before final assembly, I applied two coats of marine varnish to the copper areas, including the soil temperature sensor.

Light Sensor

The light sensor is a photoresistor that is in a resistor voltage divider with a potentiometer and a fixed resistor.  The voltage at the node between the photoresistor and the pot is a function of the amount of light hitting the photoresistor, higher when there is more light and thus lower resistence, and lower when there is less.  That node is tied to one of the analog inputs on the TIny2040 microcontroller.  The ADC reading is converted to a percentage between 100% for full brightness and 0.

I adjust the pot so that it just hits maximum in full sunlight.  The sensor is not meant to give an exact amount of sunlight hitting the plants, only an indication of when the plants are in full sunlight, in the shade, or in the dark.

Battery Level

There is a resistor divider from Vsys to ground, the center of which is connected to another analog input.  A resistor divider is needed because the input voltage range of the analog input is 0 to 3.3V, but Vsys could be 5.2 volts if connected to USB.  SInce the voltage divider is 50%, the resulting measurement needs to be multiplied by 2.

Experiments have shown that the LiPo battery needs to be recharged when the measured voltage is below 3.0 volts.

Soil Temperature

The schematic shows a DS18B20 thermal sensor.  That sensor is placed between the copper areas such that it will be in the soil, a few inches below the surface.   The SW for the Tiny2040 microcontroller skips making the measurement if the sensor is not present.  I have no idea whether soil temperature is useful, but it was easy to add.

Power Reduction

The soil moisture circuit, light sensing circuit and soil temperature circuit are all on a switched ground.  The Tiny2040 wakes up periodically to take the measurements.  It turns on the transistor to ground to power up those circuits, then power down the measurement circuits before going back to sleep.  The measurement time is on the order of 1 to 2 seconds.  The current period between measurements is 15 minutes.  The expected time is about 6 or 7 weeks between charges of a 4000 mahr LiPo battery.

Wireless Transmission

This design uses the RYLR896 LoRa transceiver to send data back t the server.  The server is a Raspberry Pi with a similar RYLR896.  The interface to the transceiver is implemented using the UART interface built into the Tiny2040 and an additional GPIO to issue a reset.  When put to sleep, it is also very low power.

The LoRa wireless transmission was chosen because it is low power and, the data rate required is small.  It also has a greater range than something like WiFi.

Microcontroller

This project uses the TIny2040 version of the RaspberryPi Pico from Pimoroni.  It was chosen because it has all the required interfaces, ADC, UART, GPIOs, and can be easily programmed to implement the OneWIre interface for the DS18B20.  The fact that it uses 3V3 IO instead of 5V is also an advantage for the components I’m using.

The other reason for choosing the Pi Pico is a personal preference.  It can be programmed in C and comes with an extensive SDK.  I avoid using python, micro or otherwise, whenever possible.

Its only disadvantage is that it is not very low power in deep sleep mode.  In its lowest power mode, the Pi Pico still draws about 2 to 3 ma on the USB 5 volts supply.

Software

The uploaded SW consists of the following files.

waterTIme.c

waterTime.c is the main program.  As with standard microcontroller SW, main() consists of a number of initialization steps followed by an infinite loop.   Between measurements, the microcontroller is put into a deep sleep, and the real-time clock is used to wake it back up for the next measurement.

Blink.c & .h

bink.c contains some utility functions to blink the onboard LED.  The LED is mostly used to indicate an error status if a failure is detected.

OneWire.c & h  and DS18B20.c & h

OneWire programs one of the PIO state machines to implement the OneWIre protocol on GPIO 7.  DS18B20 uses OneWire interface to read the temperature from one or more DS18B20 temperature sensors. Only one in this project.

CMakeLists.txt

CMakeLists.txt is used to build waterTime.uf2 that is uploaded to the tiny2040.