A Salt Level Sensor for the Water Softener

A water softener needs salt every so often, and the only way to know how much is left is to lift the lid and look. This project puts a sensor in the lid so Home Assistant can tell me instead. It has been running for a few months now and working well.

How it works

An HC-SR04 ultrasonic sensor sits at the top of the brine tank, pointing down. It sends out a pulse of sound and times the echo coming back off the salt. More distance means less salt.

Two blue HC-SR04 sensor boards, each with two round ultrasonic transducers and four pins, beside two clear mounting brackets and two ribbons of jumper wires
The kit I bought: two HC-SR04 sensors with brackets and jumper wires. Photo from the product listing.

An ESP8266 board (a D1 Mini) reads the sensor and reports to Home Assistant over WiFi, using ESPHome.

A D1 Mini: a small blue circuit board, 3.3 cm by 2.5 cm, with a metal-shielded WiFi chip, shown beside three strips of header pins
The D1 Mini, about 3.3 cm by 2.5 cm, with the header pins it comes with. Photo from the product listing.

Parts

  • HC-SR04 ultrasonic distance sensor
  • D1 Mini ESP8266 board
  • Two resistors, for a voltage divider
  • A small piece of perfboard, jumper wires and a case
  • A USB power supply (an old phone charger works)

The one catch: a 5 volt sensor on a 3.3 volt board

The sensor runs on 5 volts and sends its echo signal back at 5 volts. The ESP8266's pins are built for 3.3. So the echo wire goes through a voltage divider: two resistors that drop the signal before it reaches the board. The trigger wire doesn't need one, because the sensor accepts a 3.3 volt signal without complaint.

  • Sensor VCC goes to 5V on the D1 Mini
  • Sensor GND goes to GND
  • Sensor TRIG goes to D5
  • Sensor ECHO goes through the voltage divider to D6

Pin choice matters a little on these boards. D3, D4 and D8 are involved in how the chip boots, and it can fail to start if a sensor is holding one of them high or low. D5 and D6 are safe.

Turning a distance into a percentage

I measured my tank: 30 inches from the sensor to the bottom, and 10 inches from the sensor to the top of a full load of salt. ESPHome does the rest. Here is the sensor part of the config:

sensor:
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    id: salt_distance
    update_interval: 60s
    unit_of_measurement: "cm"
    internal: true
    filters:
      - multiply: 100
      - median:
          window_size: 5
          send_every: 5
          send_first_at: 3

  - platform: template
    name: "Salt Level"
    id: salt_level_percent
    unit_of_measurement: "%"
    accuracy_decimals: 0
    lambda: |-
      float empty_distance = 76.2;  // 30 inches: sensor to the bottom
      float full_distance = 25.4;   // 10 inches: sensor to a full load of salt
      float distance = id(salt_distance).state;
      if (distance < full_distance) distance = full_distance;
      if (distance > empty_distance) distance = empty_distance;
      return ((empty_distance - distance) / (empty_distance - full_distance)) * 100.0;
    update_interval: 60s

binary_sensor:
  - platform: template
    name: "Salt Level Low"
    device_class: problem
    lambda: |-
      return id(salt_level_percent).state < 20.0;

It takes a reading every minute and reports the median of five, which smooths out the occasional odd echo. Anything under 20% turns on a "Salt Level Low" sensor.

From breadboard to something permanent

It went in three stages: test it on a breadboard, calibrate it against the real tank, then build a version that can live in the utility room.

For the permanent version, the D1 Mini sits on a small piece of perfboard with the two resistors lying flat underneath it. They had to be soldered first, because there is no reaching them once the board is on top. Three of the four sensor wires are ordinary jumper wires that plug onto the D1 Mini's pins. Only the echo wire is soldered to the perfboard, since it is the one that has to pass through the divider.

Before it got power, a multimeter check: the echo path is connected end to end, and 5V and ground are not touching anywhere. The board lives in a small case on top of the tank lid, with the sensor looking down through the lid and the electronics outside, away from the humidity.

How it's going

It has been live for a few months and it works well. In Home Assistant it shows up as a salt level percentage and a "Salt Level Low" sensor, which is enough for:

  • A gauge on the dashboard showing the salt level
  • A notification when the level has been under 20% for an hour, sent only during the day

Two things I'm keeping an eye on:

  • These sensors reportedly last only 6 to 12 months in a humid tank, so I have a spare.
  • Salt doesn't settle flat. Chunks and bridging can bounce the sound oddly, which is part of why the readings are averaged.

Resources I leaned on