LoRaWAN Connected Water Level Sensor for Tanks

This project is designed to measure and report the water level in a water tank with a closed top – typically as used for water storage on a farm.  These tanks are generally made from PVC and are filled by a windmill or electric pump from an underground aquifer or other water source.  Tanks range in size, but are typically 2 metres high and 4 metres in diameter.

The following design requirements have been considered.

  • Must operate unattended for long periods (target at least one year) using only batteries.  This is intended to be the sole power source, so low power is a key requirement.
  • Ideally it should report the water level every hour, but if power proves to be an issue, this could be relaxed to every few hours.  At worst case recordings could be made once per day.
  • The LoRaWAN must be used for the wireless uplink.  This constrains the actual payload volume based on the duty cycle of LoRaWAN.  Connection will be to The Things Network.
  • Related metadata such as enclosure temperature and humidity and battery status must also be reported.
  • Data storage and visualisation will be via InfluxDB and Grafana (as are used in other projects).

General Operation

The project uses a PyCom LoPy4 module connected to a MaxBotix ultrasonic distance sensor.  The sensor is mounted in the top of the tank and points vertically at the water surface.  The ultrasonic sensor measures the distance from the sensor to the top of the water.  This is used to calculate the water level.  The sensor used can (according to the specification sheet) measure distances from 300 mm to 5000 mm.  This sensor was chosen as tanks are generally about 2000 mm high, so fit in the mid range for this sensor type.

The tank top is generally peaked, with a support pole up through the centre.  The sensor is mounted close to the centre of the top, in an attempt to maximise the distance between the water level when the tank is full, and the sensor itself.  Ideally this distance should exceed 300 mm.  The sensor should not be mounted too close to the centre of the top to ensure the internal support pole does not interfere with the ranging.

Data is collected by querying the sensor to retrieve the measurement.  This measurement along with related metadata is transmitted via LoRaWAN.  The related metadata includes temperature and humidity reading from inside the enclosure, light level inside the enclosure and battery voltage.

The LoPy4 module is placed in deep sleep once the measurements are taken and transmitted.  It wakes from deep sleep based on a timer.

The PyCom Module

This project uses the LoPy4 module.  This module supports multiple wireless communication stacks.  This project utilises only the LoRaWAN stack. The other communications stacks are deliberately disabled to conserve battery power.

The pin numbers on the module are referenced using both hardware module pin numbers and Pxx numbers.  The Pxx numbers are Pin Names.  They should not be confused with actual pin numbers.  Both are shown in the Module Connections table below.

The following image show the assembled LoPy4 module mounted on the custom PCB.  The PCB mounts on the insert frame before being lowered into the housing.  The mounting bracket and housing are 3D printed.  The exterior of the housing is painted with UV protective paint and clear lacquer.

Internal components

The Enclosure Hardware

The electronics is contained within a small 3D printed cylindrical enclosure mounted on top of the tank.  A horizontal divider separates the enclosure into two chambers.  The lower chamber contains the sensor and is directly exposed to the tank and is therefore subject to higher humidity.  The upper chamber contains just the electronics and is intended to be ambient humidity.  The sensor mounting plate (the white layer in the above image) uses a rubber seal on the bottom to help restrict the humidity level in the upper electronics enclosure.

The flange on the enclosure is screwed to the top of the tank.  A commercially sourced press fit lid is used to weatherproof the enclosure.  A hole (approximately 50 mm diameter) must be cut (using a hole saw) through the top of the tank to permit the sensor to point vertically downwards through this hole.  The angle of the flange is made to match the slope on the tank top to ensure the sensor points vertically.

Photo of the complete assembly screwed to the top of the water tank.
The sensor assembly mounted on the top of the tank

The LoRaWAN Data Payload

As well as the water level measurement, a number of other measurements are taken and reported.  As the data payload using LoRaWAN has to be minimised, the various numeric values are stored as hex values rather than ASCII strings.   The various data elements are as follows.

  • A transmission sequence number.
  • Water level.  This is the distance in millimetres from the sensor to the top of the water.
  • Temperature (in degrees Celsius) inside the electronics enclosure.
  • Relative Humidity inside the electronics enclosure.  The humidity level may affect the electronics, so this must be monitored.
  • Battery voltage.  The battery level is monitored as an indicator when to replace the batteries.
  • Ambient light level inside the electronics enclosure.  The internal ambient light level is used to confirm the enclosure remains intact and closed.  Any light entry suggests the lid has been removed, or the enclosure has been otherwise damaged.  This is simply a raw value from the sensor mapped to an 8 bit value.

The following table details the message payload.

Element NoDataData TypeData Length
1Sequence NumberSequence number increments on each entry to deep sleep.16 bit
2Water levelDistance reported by Maxbotix sensor in mm. (Valid range is 300 to 5000).16 bit
3Enclosure TemperatureTemperature in degrees Celsuis. Realistic values range from just below zero to about 40. To retain granularity and minimise the size, the reading is incremented by 100 and then multiplied by 10. 16 bit
4Enclosure HumidityRelative humidity in the range 0-100. Realistic values around 60-90.8 bit
5Battery VoltageBattery voltage from measurement divider, mapped to a range 1-256. Calculation required to determine actual voltage.8 bit
6Enclosure Ambient Light ReadingRaw sensor reading mapped to range of 1-256. Only intended to indicate light (enclosure open) or dark (enclosure intact).8 bit

Maxbotix Sensor Connections

The MaxBotix ultrasonic sensor provides several ways to retrieve the measurement data.  These include analog output, digital pulse width and serial data.  This project uses serial data which is received by one of the UARTs on the PyCom module.

The following table shows the pin connections for the MaxBotix sensor harness.  This harness connects the sensor to the interface board.  Click here for the Maxbotix data sheet showing pin details.

Maxbotix Pin NumberTermWire ColourP1
Interface Board Pin Number
1Temperature Sensor ConnectionNCUnused. No connection in harness
2Pulse Width OutputWhite3Unused in this project.
3Analog Voltage OutputGreen5Unused in this project.
4Ranging Start/StopPurple4Unused in this project.
5Serial Data (Sensor output)Yellow2Connects to RX2 on PyCom module.
6VccRed6The sensor is driven by 3.3V from a digital I/O line (P21) on the PyCom module (rather than from the 3.3 V supply.

Power Management

This project is intended to operate from batteries and to run for (optimistically) a year or more from a single set of batteries.  This necessitates minimal power consumption.  

The following power consumption has been measured.

  • LoPy4 module in normal operation  About 120 mA
  • Maxbotix sensor  About 2.5 mA (while transmitting serial data).
  • When the LoPy4 module is put into deep sleep (using something like “machine.deepsleep(int(time_to_sleep))” the power consumption drops to about 20 uA.  This is measured in series with the power input delivered by three AA batteries in series to provide about 4.5 V.
  • Interesting things happen when the USB to Serial debug console is connected to the LoPy4 module.  Despite taking power from the USB connection, this device impacts power consumption on the module itself.  When both TX and RX are connected, the deep sleep power only drops to about 18 mA. When either the TX or RX leg is connected, deep sleep power sits at about 40 mA.
  • Sensors (including the Maxbotix sensor) are powered from digital I/O pins on the micro controller.  This allows each sensor to be powered up in sequence as needed to perform the required measurement.  Low power sensors have been chosen so this capability can be used.  After a sensor is powered up, some stabilisation time (usually about one second) is allowed in case any stabilisation is needed.  As each measurement is taken, the sensor is again powered down.

 PyCom LoPy4 Module Connections

The following pin connections are used on the LoPy4 module.  This shows all external connections as used by the sensors, the power supply and the serial console which may be connected for debug purposes.

Module Pin NoPin NameTermUsage
5P3TX1Serial console. Connects to USB Serial module for debug output.
6P4RX1Serial console. Input not currently supported, but connected anyway.
10P8TX2Serial TX for UART2. Not used in this project.
11P9SDAi2c for SHT31 sensor.
12P10SCLi2c for SHT31 sensor.
13P11RX2Serial RX for UART2. Used as RX for serial data from Maxbotix sensor.
17P15LDR Sense (from LDR voltage divider)
18P16Battery Sense (from battery voltage sense voltage divider)
22P20Battery sense switch enable. This controls the switch in the MAX4645 analog switch IC.
23P21p_maxbotix_powerPower (3.3v) to Maxbotix sensor.
24P22p_sht31_powerPower (3.3v) to SHT31 sensor.
25P23Power (3.3v) to LDR voltage divider.
26+3.3v regulated outputProvides permanent power to the MAX4645 analog switch.
28Voltage inputInput supply to the LoPy4 board from external voltage source. (Battery pack)

Environmental Monitoring

The following environmental parameters are monitored by the LoPy4 micro-controller.  This metadata is transmitted as part of the LoRaWAN payload.

  • Enclosure temperature and humidity. 
  • Ambient light. 
  • Battery voltage.

To minimise power consumption, these sensors are powered up only when needed.  As these are generally low power devices, they are driven directly by a digital I/O pin from the module.  The battery voltage sensing uses a simple voltage divider with an analog switch used to complete the circuit to ground.  This ensures the battery voltage sensing circuit is only drawing power when a measurement is needed.  The analog switch itself remains powered.

Sensors Used

The following sensors are used in this project.

Sensor FunctionSensor ModelDetails
Water level sensorMaxBotix HRXL 7380This ia an ultrasonic device which measures the distance (300 mm - 5000 mm) between the sensor and the water surface. The serial interface is used.
Temperature and humidity sensorSensirion SHT31 breakout module (generic module)This connects via I2C to the controller.
Ambient light sensorGeneric LDR with a resistance range between about 100 ohms in sunlight and 100K ohms in darkness.A voltage divider is used to create about 3.0 V under full light.
Battery voltage sensorThis is another simple voltage divider setup to create about 3.0 V when the battery supply is at the peak value of 4.5 V.This circuit uses an analog switch to complete the circuit through the voltage divider to GND. The analog switch is controlled by a digital I/O pin from the PyCom module. To prevent continuous battery drain, the voltage divider circuit is only enabled when the sensor reading is made.



Future Enhancements

Some Future Enhancements are proposed for this project.