Introduction – Brian Brandaw
Note: Brian Brandaw won the PiOT: Maker Famous that Initial State hosted in partnership with MagPi. You can see the contest website here.
We live on a 5 acre property outside of Austin, Texas, which gives us a chance to keep horses. To help provide quality drinking water for them, we took advantage of the metal roof on the barn and existing rain gutters and installed 2 rainwater collection tanks. To these we added standard float valves to keep the troughs full for the horses. This arrangement worked great until we ran out of water in the tanks! There is no easy means to track tank level. Traditional approaches are all visual, including sight glasses, float and pulley mechanisms or floating rods that can be seen at a distance, etc.
Being a Development Manager for an IT shop, I searched for solutions pertaining to water tank levels. Many solutions were costly, involving specialized sensors or stand-alone commercial systems. I thought it would be better to put my Raspberry Pi experiments to work. After exploring online, I went with an option that measures the pressure formed by the column of water in the tank and taking into account atmospheric pressure. This approach was documented in Practical Arduino in 2009, but I found very little solid data out there beyond that.
For me, the big advantage to this approach was that I did not have to modify the tank to facilitate the addition of the sensor. I simply had to add a tee to the existing PVC outlet pipe of the tank.
Since I had reasonable wifi signal at the barn and already had 2 Pi Zeros, I went with that approach. Granted it is a quite a bit of processing power for a primitive data collection chore, but it is what I had on hand and it has been working nicely.
In addition to simply tracking water storage, I should be able to estimate how much water was consumed from each tank every day. If there is rainfall, I can back out the increases in tank volume and still get a reasonable estimate of usage.
We do have freezing temperatures here in Central Texas and while the fuel line that connects to the sensor to the water tank will be mostly filled with air, there will be small quantities of water in an uninsulated box which will be a freeze risk. I also had to run power over a good distance– almost 50′ for one tank. Using a step down converter allowed me to use higher than 5v over the long distance while avoiding having to make modifications to the line voltage at the barn. I also get the side benefit of a heat source inside the box to help prevent freezing.
Since the ADS1015 supports 4 analog inputs, it was easier to add the analog TMP36 temperature sensor to monitor temps inside the box than use another sensor. I have been using DS18B20 sensors for my other Raspberry Pi applications in the barn and pumphouse, but that would have involved another interface and more code.
Choosing the pressure sensor took some time. My selection criteria included how tall the water column could be, plus atmospheric pressure, to come to an upper limit to the readings, input voltage, form factor, etc. I opted for a differential pressure sensor so I can use a single sensor to take the reading of the tank pressure as well as account for atmospheric pressure. Using two individual sensors would also work, but this adds wiring and takes up space in the box.
Pi Zero running Raspbian Jessie
EdiMax USB WiFi Adapter with Female USB to MicroUSB cable
Pressure sensor (I used the NXP MPX53DP for my project)
Adafruit ADS1015 Analog-Digital Converter
TMP36 Analog temperature sensor to monitor temperatures inside the enclosure
0.01uF capacitor for the TMP36
USB 5v step down converter with Male USB to MicroUSB cable to provide power from the converter to the Pi.
1/2″ threaded bulkhead
PG9 cable gland
1/2″ NPT to 1/4″ female NPT PVC reducing bushing
1/4″ barb to 1/4″ male NPT brass fitting
1/2″ PVC nipple to connect bulkhead to existing plumbing
3/4″ to 1/2″ NPT reducing tee to plumb the box onto the existing tank outlet.
1/4″ fuel line to connect the barb fitting to the sensor
Enclosure suitable for the environment (while not officially rated for outdoor use, I used a Hammond RS5305S box, measuring 6 1/2″ x 4 1/8″ x 3″)
1/8″ plastic sheet for backing board
Miscellaneous nylon standoffs for mounting circuit boards to the backing board
12 volt power supply
Landscape lighting wire (suitable for outdoor use)
It is easier to do this up front, where you can use a monitor and keyboard. Setup your Pi Zero and get it operational. This includes your distro (I used Raspbian Jessie) and wifi configuration. Once the Pi is installed and in the field, it is more difficult to work on.
Plumbing and Enclosure
Cut backing board to fit and drill for mounting into the box. My approach here was to cut a piece of printer paper to fit inside the box, then with it lying in the bottom of the box, I could poke holes into the paper to align to the standoff screw locations in the bottom of the box. I could then use the paper as the template for cutting and drilling the board.
Layout backing board with standoffs for mounting boards. Don’t forget to account for where your wiring connectors, bulkhead, and gland will be so you can have room where its needed.
Drill holes in enclosure for gland and bulkhead. Install the gland and test fit the bulkhead. This was another key challenge; how to get plumbing access inside the box while maintaining some degree of weatherproofing. The answer was a bulkhead– basically a fitting that provides a threaded hole in a box. Bulkheads come in threaded and slip fittings. Either will work, provided the other pieces all match. In my case, I used all threaded joints. This allows me to swap out any of the plumbing parts without throwing away the bulkhead and box. I chose to install the gland in the bottom of the box. The thought that would be the least waterproof point so locating it on the bottom provided some protection from the rain. I altered the shape of the backing board to accommodate.
Solder the jumpers and components on the protoboard. I’ve included the wiring diagram I used for the project (hacked together using JPGs of the bare protoboard I grabbed from the net and MS Paint… crude but effective!)
Assemble electronics and install onto backing board, then install the board into the enclosure. The power converter is mounted first on short standoffs, then the Pi Zero with its protoboard. Be sure to use a stand off to secure the pressure sensor to the mounting board. The connectors are frail and the fuel line will be a snug fit on the sensor. I have broken connectors on two sensors now and have been experimenting with the use of wiring pigtails to eliminate that stress point. I may even go to Dupont connectors and pins on the protoboard
Install bulkhead, barb fitting and PVC adapter. We wait until this step because installing the board after the plumbing is tricky.
Connect your barb fitting to the sensor using a loop of fuel line. The ends can warmed using hot water to make it more pliable and easier to push on.
Install your reducing tee on the water tank if necessary. In my case it is 3/4″ PVC with a ball valve as a shutoff. I added the tee downstream of the valve so I could easily install and remove the box without losing water from the tank. I have a 1/2″ NPT threaded plug for times when I need to remove the box for an extended period of time.
Screw the 1/2″ nipple into the external side of the bulkhead and attach the enclosure to the water tank at the newly installed tee.
Insert your power leads through the gland and connect to the input lugs on the step down converter. I used pigtails and wire nuts because it was difficult to thread the heavier landscape wire thru a crowded enclosure. The pigtail allows for easier access to the connections. Secure the gland to ensure a weatherproof connection.
With the box lid off, open the water valve and check for leaks. The sensor should never be exposed to water. By connecting the plumbing first, then pressurizing the system, we should have a substantial air bubble between the water and the sensor itself.
All Python here and nothing too fancy.
The math involved is:
Pressure = mass density of the water * acceleration due to gravity * height of the fluid column
Important to note that diameter of the water column has no bearing on the pressure formed.
My approach was:
- Settle on an average density value (since it’s temperature dependant, not linear, and rather low range of values)
- Calculate pressure formed by one cm water column (http://www.engineeringtoolbox.com/hydrostatic-pressure-water-d_1632.html)
- Using the measurements of my tank, calculate the volume of 1 cm of water in my tank.
This way, once I get the final pressure reading, it’s as simple as:
(pressure measurement / pressure per cm) * volume per cm of water in the tank.
To get that pressure, there are a few steps:
- Use the differential measurement feature of the ADS1015 to take the difference between the water column pressure and atmospheric pressure. We don’t want to read the weight of the air on top of the water column, just the pressure formed by the water.
- Account for the offset (observed output of the sensor with zero pressure applied.) Measure this by doing the read_adc_difference call with both inlets to the sensor open to the atmosphere and observing the reading.
- Pressure = mv * sensitivity (from the MPX53DP datasheet)
The TMP36 reading is easier. Use the adc_read method to read a single channel and follow the sample code from the ads1015 to calculate temperature.
You can see my python script here:
Each Pi Zero posts its readings to Initial State directly. I have had numerous architectures for this… local storage in Graphite with an Apache server, RRD and Apache, Sqlite using mqtt for messaging, you name it, I’ve tried it.
More about Brian
Brian is a 25 year veteran in the IT industry. Brian is highly experienced in Linux and enjoys playing around with this Raspberry Pi at home. Brian was also the winner of the PiOT: Maker Famous competition. You can see his article in print here in issue 53 on pages 38 – 39: https://www.raspberrypi.org/magpi-issues/MagPi53.pdf