In June 2013, we had the unfortunate luck of a basement flood, caused by a tripped electrical breaker connected to our sump pump. There are so many things that can go wrong with a sump pump. You always are on guard for power outages, blown breakers, sump pump failures, clogged pipes and all manner of issues that can arise, which ultimately can end with a flooded basement. I needed a way to alert me of issues when I was not at home. Audible alarms are fairly cheap and are great when you are physically in the house. They fail miserably when you are ten miles away at work. I had a Raspberry Pi that I had tinkered with periodically but for which I never had a real purpose. I decided to try to put the Pi to work as a dedicated sump pit monitoring device. Hopefully, the Pi could send me SMS alerts if a problem arose while I was away.

Since I did not have a programming background, I started to look for an existing project I could install on the Pi that could act as a sump pit monitor. There are other projects that can monitor sump pump activity; however, it seemed that everything I came across looked overly complicated or didn't have the features I required. I needed something simple that monitored the water level in the sump pit at regular intervals and sent me a text if there was a problem. If it also could display pretty graphs of sump pit activity that I could access easily, that would be a bonus.

Although I had written many scripts through the years at work, I never learned object-oriented programming. I made the decision to learn Python, and a few months later, set myself to work on a monitoring system. I chose Python because it has an active community, and many Raspberry Pi enthusiasts use it as their main scripting language. The Raspberry Pi uses Raspbian Linux, which is based on Debian, so that already was familiar ground. With these tools in hand and in true Linux and Raspberry Pi spirit, I decided to build my own and called it Raspi-Sump.

Raspi-Sump is a sump pit water-level monitoring system written in Python. It uses a Raspberry Pi and an HC-SR04 ultrasonic sensor to monitor the water level in a sump pit, log the readings and send SMS e-mail alerts if the water rises above a predefined level.

In this article, I show the methodology I used to create Raspi-Sump. I also describe the physical setup of the monitor and the scripts that make it work. If you choose to do something similar, the source code and install instructions are available on GitHub. It is free to use and modify as you wish (see Resources).

I determined that the features I required in a monitor included the following:

  • Regular one-minute-interval readings of the water depth in my sump pit.

  • Logging of readings to a comma-delimited file for processing graphs and historical pump activity.

  • Automated SMS e-mail alerts if the water exceeds a predefined level.

  • Off-site graphical reports of the current water level to a Web site.

  • Web-based historical information on sump pump activity.

  • Automatic restart of the raspisump.py process after an unexpected failure.

The Physical Setup

The complete list of components for Raspi-Sump includes:

  • Raspberry Pi Model B and case.

  • Raspbian Linux.

  • HC-SR04 ultrasonic sensor.

  • Five feet of Cat5 wire (four 24AWG strands needed).

  • Two resistors (one 470R Ohm and one 1K Ohm).

  • Heat-shrink tubing to protect soldered connections.

  • Plastic bracket to hold the sensor.

  • One two-foot piece of wood strapping to mount the plastic bracket in the pit.

  • One floppy drive four-pin power connector salvaged from an old PC.

  • Two case-fan power connectors, also salvaged from the same PC.

Total cost for materials, including a couple spare sensors, was $80.

The ultrasonic sensor I chose is the HC-SR04, which has four connections that are wired to the GPIO pins of the Raspberry Pi. With the help of a Python script, the sensor, which is mounted inside the sump pit facing the water, sends a sound pulse that reflects off the water and back to the sensor. The script monitors the amount of time it takes for the sound pulse to bounce back to the sensor. It calculates the distance by measuring the time required for the pulse to return at the speed of sound. This gives you a reading of the distance between the sensor and the water. The distance is used to calculate the water depth and log a time-stamped result to a CSV file.

Figure 1 shows a closer look at the connections.

Figure 1. Wiring Diagram

The four pins on the sensor are wired to the Raspberry Pi as follows:

  • Pin 1 VCC connects to the 5V pin 2.

  • Pin 2 Trig connects to GPIO17 pin 11.

  • Pin 3 Echo connects to GPIO27 pin 13.

  • Pin 4 Ground connects to pin 6 Ground.

I chose GPIO17 and 27, but you can use any available GPIO pins on the Pi as long as they are identified properly in the Python script.

Pin 1 provides 5V of power to the HC-SR04 sensor. A command is initiated on GPIO17 (Trig) that sets the value of the pin to True for 10 micro seconds. This causes the sensor to initiate a series of sound pulses toward the water for that short amount of time. The Echo pin connected to GPIO27 listens for a return pulse. The difference between the send and the return of the pulse gives a time measurement. The measurement is used to calculate the distance of the water.

This causes a small problem as Raspberry Pi GPIO pins are rated only for 3.3V. The sensor sends a 5V current back toward GPIO27. A way is needed to throttle the current to 3.3V, which won't damage the Pi. To protect the Pi from damage, simply insert a voltage divider on the Echo line between the sensor and the Pi.