Aquarium water quality TDS sensor

I was struggling with same problem in my shrimptank. And I found some simply workaround… just control the VCC of TDS meter by GPIO and turn it on for 200ms every half hour. This is causing small jump in pH readings, but since I’m using median template these “jumps” values are rejected.
Here is my code:

# DS18B20 Temperature Sensor
dallas:
  - pin: D2

#Multipexer
cd74hc4067:
  - id: cd74hc4067_1
    pin_s0: D5
    pin_s1: D6
    pin_s2: D7
    pin_s3: D8
  
sensor:
  - platform: dallas
    address: 0x853C86F6485F0328
    name: "Shrimptank Temperature"
    id: shrimptank_temperature

  - platform: adc
    id: adc_sensor
    pin: A0
    update_interval: 5s
  - platform: cd74hc4067
    name: "Shrimptank pH Sensor"
    id: shrimptank_pH
    number: 0
    sensor: adc_sensor
    update_interval: never
    unit_of_measurement: pH
    filters:
      - multiply: 3.1
      # Measured voltage / 3.1 -> Actual pH (buffer solution)
      - calibrate_linear:
          - 2.244 -> 9.18
          - 2.487 -> 6.86
          - 2.783 -> 4.00 
  - platform: cd74hc4067
    name: "Shrimptank TDS RAW Sensor"
    id: shrimptank_tds_raw_v
    number: 1
    sensor: adc_sensor
    update_interval: never
    unit_of_measurement: V
    filters:
      - multiply: 3.1
    accuracy_decimals: 3
    internal: true

# Temperature Compensated Voltage  
  - platform: template
    name: "Shrimptank TDS TCV"
    id: shrimptank_tds_tcv
    unit_of_measurement: V
    accuracy_decimals: 3
    lambda: return ((id(shrimptank_tds_raw_v).state) / (1 + (0.02 * ((id(shrimptank_temperature).state) - 25.0))));
    update_interval: never
    internal: true
  
# Temperature Compensated TDS
  - platform: template
    name: "Shrimptank TDS"
    id: shrimptank_tds
    unit_of_measurement: 'PPM'
    accuracy_decimals: 0    
    update_interval: never
    lambda: return (133.42*(id(shrimptank_tds_tcv).state)*(id(shrimptank_tds_tcv).state)*(id(shrimptank_tds_tcv).state) - 255.86*(id(shrimptank_tds_tcv).state)*(id(shrimptank_tds_tcv).state) + 857.39*(id(shrimptank_tds_tcv).state))*0.5;

# TDS to μS
  - platform: template
    name: "Shrimptank μS"
    id: shrimptank_us
    unit_of_measurement: 'μS'
    accuracy_decimals: 0    
    update_interval: never
    lambda: return (id(shrimptank_tds).state)*2;

output:
  - platform: gpio
    pin: D0
    id: tds_power

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      # Every half hour at 5th sec:
      - seconds: 5
        minutes: 0,30
        then:
          - output.turn_on: tds_power
          - delay: 100ms
          - component.update: shrimptank_tds_raw_v
          - delay: 100ms
          - output.turn_off: tds_power
          - component.update: shrimptank_tds_tcv
          - component.update: shrimptank_tds
          - component.update: shrimptank_us
      # Every 10 sec:
      - seconds: /10
        then:
          - component.update: shrimptank_pH
2 Likes

Do you have an example of pinout and setup? I’m trying to replicate this!

Appreciate you!

I’m assuming you are using the multiplexer because you are using and esp8266 and not an esp32?

Do you use a separate sensor for your temperature? I purchased the same sensor and I cant get it to pick up on the temperature setting at all

Hello hello,

I’ve applied the configuration you posted @ontdamian

But when looking in Home Assistant it appears that sometimes got a spike

Is it a known problem ?

To clarify:

I’m using cd74hc4067 multiplexer to read analog values from pH meter and TDS meter with one esp. Esp has only one analog input A0, that’s why I had to use cd74hc4067 multiplexer which allows to read up to 16 analog sources. As mentioned above for correct TDS reading you also need temperature sensor. I’m using simplest DS18B20. TDS meter can’t be powered whole the time because it distorts pH readings.

Here is my pinout:

  • Multiplexer SIG to A0 on esp
  • Multiplexer S0 to D5 on esp
  • Multiplexer S1 to D6 on esp
  • Multiplexer S2 to D7 on esp
  • Multiplexer S3 to D8 on esp
  • pH meter signal to C0 on multiplexer (if your signal is 5V make sure to use voltage divider to 3.3V)
  • TDS meter signal to C1 on multiplexer
  • TDS VCC to D0 on esp
  • DS18B20 to D2 on esp

Here it as it works:

  1. Signal from multiplexer (A0) is updated every 5 sec.
  2. Every 10 sec multiplexer reads pH meter signal (C0)
  3. Every half hour at 5th sec TDS is powered on by D0 pin of esp for 200 ms and multiplexer reads TDS raw signal (C1)
2 Likes

Thanks for this helpful reply :slight_smile:

I’ve already :

Could you please tell me which pH meter you have ?
Can I use the 3.3v of the ESP to connect the pH meter ? If not do you have any link to buy voltage divider ?

I will buy both of Multiplexer, pH meter and voltage divider

Thanks

2 Likes

Thanks for the detailed instruction! I built it but I’m struggling that my PH sensor measure constant 1V. I recabled, changed the USB cable and the power supply, tried with 5V with 3.3V converter, with 3.3V, still the same. 1V. Anyway the TDS/EC working fine and get the correct value. I created a config without the multiplexer and in this case the PH sensor measuring the correct values so I’m totally lost.

Hey,
How do you stop biofilm from ruining the results?
Cheers

I want to do a TDS sensor for my organic hydroponic system and I am wondering how you are liking your setup??? Would you do anything differently??

I am Wondering if a more expensive ($200) TDS sensor from Atlas is worth it or if a cheaper one is just fine

so i followed your calculations;


since I don’t have a temperature sensor connected to my wemos, i’ve swapped any temperature bits with “25”. While testing the sensor by dumping it in a bottle of lemonade-syrup, i’ve reached a ppm of 15. I then put the sensor in my hydro bucket where it started around 3.39 without adding any nutrients. The readings have been swinging a little bit, rising ever so slightly. Then today I put in fresh water and i’m reading 2.44 ppm. I’m guessing I’m going wrong somewhere in the formula calculation.
My code is below, mind giving it a look and telling me where i’m being stupid?

sensor:
  - platform: adc
    pin: A0
    name: "tds raw voltage"
    id: tds_raw_v
    update_interval: 5s
    unit_of_measurement: "V"
    icon: "mdi:water-percent"
    internal: true

# Temperature Compensated Voltage  
  - platform: template
    name: "TDS TCV"
    id: tds_tcv
    unit_of_measurement: 'v'
    accuracy_decimals: 3
    lambda: 'return ((id(tds_raw_v).state) / (1 + (0.02 * (25) - 25.0)));'
    update_interval: 5s
    internal: true
  
# Temperature Compensated TDS
  - platform: template
    name: "TDS"
    id: hydro_tds
    icon: "hass:water-opacity"
    unit_of_measurement: 'PPM'
    accuracy_decimals: 2    
    update_interval: 5s
    lambda: return ((133.42*(id(tds_tcv).state)*(id(tds_tcv).state)*(id(tds_tcv).state) - 255.86*(id(tds_tcv).state)*(id(tds_tcv).state) + 857.39*(id(tds_tcv).state))*0.5) * -1;
    
  - platform: template
    name: "EC Reading"
    id: ec
    unit_of_measurement: 'EC'
    accuracy_decimals: 2
    update_interval: 5s
    lambda: return (((id(hydro_tds).state) * 2) / 1000);

I know i should up the update_interval because it has no reason to be at 5 seconds, but this was mostly for testing purposes. i’ve already put them back to 120seconds to give the esp a lil break now and again.

Sorry for my late response, I’m not visiting this forum so often…

Finally I had to get rid of TDS meter, because even if was powered by esp every half hour, pH meter readings were constantly distorted. I removed multiplexer and TDS meter and now pH readings looks much constant. There are still big differences between max and min readings, but after using median it looks good and credibly:

I’m using simplest pH probe and board/module from AliExpress:
pH_probe
pH_module

It’s connected like here:

but without lcd and using 3.3V diverter based on 2k2 and 3k3 ohm resistors to be more stable.

After setting this up, pH V readings on A0 need to be calibrated with voltmeter and ph buffers:

filters:
      - multiply: 3.1
      # Measured voltage / 3.1 -> Actual pH (buffer solution)
      - calibrate_linear:
          - 2.244 -> 9.18
          - 2.487 -> 6.86
          - 2.783 -> 4.00

I think your formula is incorrect. After substituting 25 for the temperature value it should look like:

((id(tds_raw_v).state) / (1 + (0.02 * (25 - 25.0))))

but I’m not sure if you can simple use 25…

I have a CQRobot CQRADS1115 TDS sensor. Trying to get it integrated into Home Assistant. Will this code work for that? I also have dallas temp sensors already configured and working for temp comp.

### TDS 01
# Raw TDS Reading
  - platform: ads1115
    multiplexer: 'A0_GND'
    gain: 6.144
    name: "TDS 01 Raw"
    ads1115_id: ads1
    id: tds01_raw
    update_interval: 10s
    unit_of_measurement: "v"
    accuracy_decimals: 3
    internal: true

# Temperature Compensated Voltage
  - platform: template
    name: "TDS 01 TCV"
    id: temp01_comp_v
    unit_of_measurement: 'v'
    accuracy_decimals: 3
    lambda: 'return ((id(tds01_raw).state) / (1 + (0.02 * ((id(probe01).state) - 25.0))));'
    update_interval: 10s
    internal: true

# Temperature Compensated TDS
  - platform: template
    name: "55G TDS 01"
    id: tds01_55g
    icon: "hass:water-opacity"
    unit_of_measurement: 'PPM'
    accuracy_decimals: 0    
    lambda: return (133.42*(id(temp01_comp_v).state)*(id(temp01_comp_v).state)*(id(temp01_comp_v).state) - 255.86*(id(temp01_comp_v).state)*(id(temp01_comp_v).state) + 857.39*(id(temp01_comp_v).state))*0.5;

# Temperature In °C
  - platform: dallas
    address: 0x6f01205fd42aa428
    name: "Probe 01"
    id: probe01
    unit_of_measurement: "°C"
    internal: true

# Temperature In °F
  - platform: template
    name: "55G TEMP 01"
    id: temp01_55g
    lambda: |-
      return id(probe01).state * (9.0/5.0) + 32.0;
    unit_of_measurement: "°F"
2 Likes

Yes that should do it

I made a youtube tutorial (in Spanish but with english subs) about how use TDS sensor and integrate with HA:

4 Likes

Would it still be connected like this? (On A0 instead of A1, and Im using a nodemcu)…Can’t get it to read the sensor.

Will this work with the ADS1115 as well?

Yes, I’m using “board: nodemcu-32s”, and dual sensors on both “A0_GND” & “A1_GND”

Almost certainly yes, although you may have to adjust the gain, you can check the RAW voltage with a multimeter

I know this is an older thread, however I am trying to do something similar here to monitor the TDS in an RODI filter. With that I will not be using a temperature probe and understand the significance of requiring temperature compensation.

I am new to ESPHome so am unsure how to essentially set the temperature compensation to 25.0 as a pre-defined value rather than one calculated by a probe.

I also have 3 TDS sensors and noticed above someone mentioned they have 4, so would love for someone to shed some light.

This is what I have so far but it wont compile because I am obviously using the wrong syntax/code

dallas:
  - pin: GPIO23
  
sensor:
  - platform: dallas
    address: 0xb7000006c34eb728
    name: "Aquarium Temperature"
    id: aquarium_temperature
    icon: "mdi:water-percent"
    unit_of_measurement: "°C"
    device_class: "temperature"
    state_class: "measurement"
    state: 25
    filters:
      - offset: -2.0
      
  - platform: adc
    pin: GPIO0
    name: "Tap TDS Raw Voltage"
    id: tapTDS_raw
    update_interval: 120s
    unit_of_measurement: "V"
    icon: "mdi:water-percent"
    internal: true

# Temperature Compensated Voltage  
  - platform: template
    name: "tapTDS TCV"
    id: taptds_tcv
    unit_of_measurement: 'v'
    accuracy_decimals: 3
    lambda: 'return ((id(tapTDS_raw).state) / (1 + (0.02 * ((id(aquarium_temperature).state) - 25.0))));'
    update_interval: 120s
    internal: true
  
# Temperature Compensated TDS
  - platform: template
    name: "Tap TDS"
    id: tapTDS
    icon: "hass:water-opacity"
    unit_of_measurement: 'PPM'
    accuracy_decimals: 0    
    update_interval: 120s
    lambda: return (133.42*(id(taptds_tcv).state)*(id(taptds_tcv).state)*(id(taptds_tcv).state) - 255.86*(id(taptds_tcv).state)*(id(taptds_tcv).state) + 857.39*(id(taptds_tcv).state))*0.5;

  - platform: adc
    pin: GPIO4
    name: "Pre-DI TDS Raw Voltage"
    id: prediTDS_raw
    update_interval: 120s
    unit_of_measurement: "V"
    icon: "mdi:water-percent"
    internal: true

# Temperature Compensated Voltage  
  - platform: template
    name: "prediTDS TCV"
    id: preditds_tcv
    unit_of_measurement: 'v'
    accuracy_decimals: 3
    lambda: 'return ((id(prediTDS_raw).state) / (1 + (0.02 * ((id(aquarium_temperature).state) - 25.0))));'
    update_interval: 120s
    internal: true
  
# Temperature Compensated TDS
  - platform: template
    name: "Pre-DI TDS"
    id: prediTDS
    icon: "hass:water-opacity"
    unit_of_measurement: 'PPM'
    accuracy_decimals: 0    
    update_interval: 120s
    lambda: return (133.42*(id(preditds_tcv).state)*(id(preditds_tcv).state)*(id(preditds_tcv).state) - 255.86*(id(preditds_tcv).state)*(id(preditds_tcv).state) + 857.39*(id(preditds_tcv).state))*0.5;

  - platform: adc
    pin: GPIO13
    name: "Output TDS Raw Voltage"
    id: outTDS_raw
    update_interval: 120s
    unit_of_measurement: "V"
    icon: "mdi:water-percent"
    internal: true

# Temperature Compensated Voltage  
  - platform: template
    name: "outTDS TCV"
    id: outtds_tcv
    unit_of_measurement: 'v'
    accuracy_decimals: 3
    lambda: 'return ((id(outTDS_raw).state) / (1 + (0.02 * ((id(aquarium_temperature).state) - 25.0))));'
    update_interval: 120s
    internal: true
  
# Temperature Compensated TDS
  - platform: template
    name: "Output TDS"
    id: outTDS
    icon: "hass:water-opacity"
    unit_of_measurement: 'PPM'
    accuracy_decimals: 0    
    update_interval: 120s
    lambda: return (133.42*(id(outtds_tcv).state)*(id(outtds_tcv).state)*(id(outtds_tcv).state) - 255.86*(id(outtds_tcv).state)*(id(outtds_tcv).state) + 857.39*(id(outtds_tcv).state))*0.5;

Hi I have an issue with the TDS using ESP32 and ADS1115 and the code used above. I get an error on compiling that there is no state member meaning it fails to call the ADS channel raw value. I just don’t get that. Any help appreciated.
sensor:

  • platform: dallas_temp
    #address:
    name: “Mixing tank water temperature”
    id: mixing_tank_temperature
  • platform: ads1115
    ads1115_id: ec_raw_v
    update_interval: 120s
    unit_of_measurement: V
    accuracy_decimals: 3
    multiplexer: ‘A0_GND’
    gain: 6.144
    name: “ADS1115_EC Channel A0-GND”

Temperature Compensated Voltage

  • platform: template
    name: “EC TCV”
    id: ec_tcv
    unit_of_measurement: ‘v’
    accuracy_decimals: 3
    lambda: ‘return ((id(ec_raw_v).state) / (1 + (0.02 * ((id(mixing_tank_temperature).state) - 25.0))));’
    update_interval: 120s
    internal: true