How I use a microcontroller and ultrasonic sensors to monitor the salt level for our water softener.

I used an Arduino Maker WiFi 1010 and some ultrasonic sensors to build a “salt level checker” for our water softener system. In this post, I’ll cover the what, why, and how of the project.

What is a water softener?

A water softener is a filtration system that removes calcium and magnesium from water. This process helps improve the efficiency and lifetime of household items that use water, like water heaters and pipes. It also helps make the water taste better.

As part of this system, we need to periodically add salt to a brine tank. This salt is important for the filtration process, and forgetting to refill the tank can gunk up the water.

Before (left) and after (right) we updated our water softener system.

An outside and inside view of our brine tank.

How Can Arduino Help?

I have forgotten to set a timer to refill the salt tank a few times. To help with this, I wanted to create a solution that reminded me automatically. My goals for the project were:

Send a notification to my phone when the tank needs refilling. Notify me if the lid isn’t properly attached to the tank.

Implementation

I had some leftover HC-SR04 ultrasonic rangefinder sensors from a previously abandoned project. These sensors had the added benefit of being no-contact, which, in this case, helps tremendously because salt can be harmful to electronics.

I decided to use multiple sensors because the salt is frequently poured in unevenly.

These sensors send out a sound pulse and then measure the time it takes for the pulse to return, similar to sonar in submarines. I planned to mount these sensors to the top of the salt tank, facing downwards, after drilling some holes in the lid.

Because I used more than one ultrasonic sensor, I needed to control the timing of the sound pulses. If you try to read from all three sensors at once, you’ll find they interfere with each other.

I used an Arduino Maker WiFi 1010 so the device could communicate the reading through our WiFi network.

The Arduino starts a HTTP web server when it boots with a hardcoded IP address. When it receives a request, it reads each of the sensors and responds with JSON.

I added logic to discard potentially invalid readings. If a sensor returns a 0 value, it is discarded and retried (up to a maximum number of attempts). Each sensor is read several times and then averaged together.

If the standard deviation of these readings is too high, then all of the readings are thrown away and retried (also up to a maximum number of attempts). The average sensor value and standard deviation are returned in the JSON so the requestor can determine their confidence in the data.

An example response from the device.

After ensuring everything worked properly on the breadboard, I soldered all of the components together.

A Note About Interfacing with the HC-SR04

Connecting a HC-SR04 to an Arduino is very simple. Here are the connections I made:

HC-SR04 Pin Arduino Vcc 5V Trig Any GPIO Pin Echo Any GPIO pin (See note) Gnd Gnd

Note: If your Arduino expects a 3.3V signal for its GPIO pins but your HC-SR04 outputs 5V, you’ll need to use a voltage divider on the Echo pin. I implemented this using a 1kΩ and 2kΩ resistor.

Then, I mounted the device to the top of the salt tank by drilling some holes in the lid with a Dremel. To prevent any dust from getting in the small gaps between the sensors, I added some cardboard and tape. The Arduino is zip-tied to prevent it from moving around when the lid is removed.

Server

I created a bare-bones Ruby on Rails app to query the device periodically and store the data. There’s a dashboard that visualizes the data using Chartkick.

The dashboard graph after 18 hours of readings. My computer went to sleep for some of the readings in the middle.

I use the whenever gem to schedule cron jobs that fetch the sensor values periodically. A rake task then puts these sensor values into a local database.

If the salt level is getting low, or if the server thinks the lid was accidentally left off the device, the Ruby on Rails app will trigger a push notification to my phone through the Pushover service.

At some point, I’d like to move this dashboard to a Raspberry Pi. For now, I’m happy with it running on my Mac Mini during the day. I don’t keep the Mini running longer than I normally would, so it doesn’t add to the electric bill.

So far, I’m happy with the result. If you have questions, comments, or suggestions, shoot me a line!

Parts List

Resources

If you liked this project, you might also be interested in the RFID keepsake box I made for my girlfriend (now wife).

Previous Post From 2019 to 2020 Some reflection on the past and goals for the future. Next Post HoloLens Voice Control for Users with Movement Disorders How to implement phrase recognition using HoloLens and Unity.