ESP8266 NodeMCU with TDS Sensor (Water Quality Sensor)

In this guide, you’ll learn how to use a TDS meter (Total Dissolved Solids) with an ESP8266 NodeMCU board. A TDS meter indicates the total dissolved solids like salts, minerals, and metals, in a solution. This parameter can be used to give you an idea of water quality and compare water from different sources. One of the main applications of a TDS meter is aquarium water quality monitoring.

ESP8266 NodeMCU with TDS Sensor Water Quality Sensor Arduino IDE

We’ll use the TDS meter from keystudio and show you a simple example to measure TDS in ppm units using Arduino IDE.

Table of Contents

In this tutorial, we’ll cover the following topics

Introducing the TDS Meter

A TDS meter measures the number of total dissolved solids like salts, minerals, and metals in the water. As the number of dissolved solids in the water increases, the conductivity of the water increases, and that allow us to calculate the total dissolved solids in ppm (mg/L).

Total dissolved solids TDS meter

Although this is a good indicator to monitor the quality of the water, note that it does not measure contaminants in the water. Thus, you can’t rely solely on this indicator to determine if the water is good for consumption or not.

A TDS meter can be useful to monitor water quality in many applications like pools, aquariums, fish tanks, hydroponics, water purifiers, etc.

In this tutorial, we’ll use the TDS meter from keystudio that comes with an interface module and an electrode probe (see picture above).

For more information about the TDS meter, we recommend taking a look at the official documentation.

Features and Specifications

This tutorial refers to the TDS Meter V1.0 from keystudio. Here are the sensor parameters:

TDS Meter:

  • Input Voltage: DC 3.3 ~ 5.5V
  • Output Voltage: 0 ~ 2.3V
  • Working Current: 3 ~ 6mA
  • TDS Measurement Range: 0 ~ 1000ppm
  • TDS Measurement Accuracy: ± 10% F.S. (25 ℃)
  • Module Interface: XH2.54-3P
  • Electrode Interface: XH2.54-2P

TDS Probe:

  • Number of Needle: 2
  • Total Length: 60cm
  • Connection Interface: XH2.54-2P
  • Color: White
  • Waterproof Probe

Where to Buy TDS Sensor?

TDS sensor

You can check the TDS sensor on Maker Advisor to find the best price:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Interfacing the TDS Meter with the ESP8266

The TDS meter outputs an analog signal that can be measured using the ESP8266 analog pin A0.

Wire the sensor as in the following table:

TDS SensorESP8266
GNDGND
VCC3.3V
DataA0

Note: learn more about the ESP8266 ADC: ESP8266 ADC – Read Analog Values

Reading TDS (water quality) with the ESP8266 – Code

As we mentioned previously, the sensor outputs an analog signal that can be converted to TDS in ppm. We’re using the code provided by the sensor documentation with some modifications.

ESP8266 NodeMCU with TDS sensor water quality

To get more accurate results, you’ll probably need to calibrate your sensor against a solution with a known TDS value. However, it might not be needed if you are not concerned about specific values but about a qualitative value of TDS.

Upload the following code to your ESP8266.

// Original source code: https://wiki.keyestudio.com/KS0429_keyestudio_TDS_Meter_V1.0#Test_Code
// Project details: https://RandomNerdTutorials.com/esp8266-nodemcu-tds-water-quality-sensor/

#define TdsSensorPin A0
#define VREF 3.3              // analog reference voltage(Volt) of the ADC
#define SCOUNT  30            // sum of sample point

int analogBuffer[SCOUNT];     // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0;
int copyIndex = 0;

float averageVoltage = 0;
float tdsValue = 0;
float temperature = 23;       // current temperature for compensation

// median filtering algorithm
int getMedianNum(int bArray[], int iFilterLen){
  int bTab[iFilterLen];
  for (byte i = 0; i<iFilterLen; i++)
  bTab[i] = bArray[i];
  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++) {
    for (i = 0; i < iFilterLen - j - 1; i++) {
      if (bTab[i] > bTab[i + 1]) {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }
  if ((iFilterLen & 1) > 0){
    bTemp = bTab[(iFilterLen - 1) / 2];
  }
  else {
    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  }
  return bTemp;
}

void setup(){
  Serial.begin(115200);
  pinMode(TdsSensorPin,INPUT);
}

void loop(){
  static unsigned long analogSampleTimepoint = millis();
  if(millis()-analogSampleTimepoint > 40U){     //every 40 milliseconds,read the analog value from the ADC
    analogSampleTimepoint = millis();
    analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin);    //read the analog value and store into the buffer
    analogBufferIndex++;
    if(analogBufferIndex == SCOUNT){ 
      analogBufferIndex = 0;
    }
  }   
  
  static unsigned long printTimepoint = millis();
  if(millis()-printTimepoint > 800U){
    printTimepoint = millis();
    for(copyIndex=0; copyIndex<SCOUNT; copyIndex++){
      analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
      
      // read the analog value more stable by the median filtering algorithm, and convert to voltage value
      averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;
      
      //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0)); 
      float compensationCoefficient = 1.0+0.02*(temperature-25.0);
      //temperature compensation
      float compensationVoltage=averageVoltage/compensationCoefficient;
      
      //convert voltage value to tds value
      tdsValue=(133.42*compensationVoltage*compensationVoltage*compensationVoltage - 255.86*compensationVoltage*compensationVoltage + 857.39*compensationVoltage)*0.5;
      
      //Serial.print("voltage:");
      //Serial.print(averageVoltage,2);
      //Serial.print("V   ");
      Serial.print("TDS Value:");
      Serial.print(tdsValue,0);
      Serial.println("ppm");
    }
  }
}

View raw code

How the Code Works

Let’s take a quick look at the code. You can also skip right away to the Demonstration section.

The TdsSensorPin variable saves the GPIO where you want to get the readings. The ESP8266 only has one analog pin, A0.

#define TdsSensorPin A0

Then, insert the analog voltage reference for the ADC. For the ESP8266 is 3.3V, for an Arduino, for example, it is 5V.

#define VREF 3.3              // analog reference voltage(Volt) of the ADC

Before getting a measurement value, we’ll apply a median filtering algorithm to get a more stable value. The SCOUNT variable refers to the number of samples we’ll filter before getting an actual value.

#define SCOUNT  30            // sum of sample point

Then, we need some arrays to store the readings as well as some index variables that will allow us to go through the arrays.

int analogBuffer[SCOUNT];     // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0;
int copyIndex = 0;

Initialize the averageVoltage variable and tsdValue as float variables.

float averageVoltage = 0;
float tdsValue = 0;

The temperature variable saves the current temperature value. The temperature influences the readings, so there is an algorithm that compensates for fluctuations in temperature. In this example, the reference temperature is 25ºC, but you can change it depending on your environment. For more accurate results, you can add a temperature sensor and get the actual temperature at the time of reading the sensor.

float temperature = 25;       // current temperature for compensation

The following function will be used to get a stable TDS value from an array of readings.

// median filtering algorithm
int getMedianNum(int bArray[], int iFilterLen){
  int bTab[iFilterLen];
  for (byte i = 0; i<iFilterLen; i++)
  bTab[i] = bArray[i];
  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++) {
    for (i = 0; i < iFilterLen - j - 1; i++) {
      if (bTab[i] > bTab[i + 1]) {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }
  if ((iFilterLen & 1) > 0){
    bTemp = bTab[(iFilterLen - 1) / 2];
  }
  else {
    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  }
  return bTemp;
}

In the setup(), initialize the Serial Monitor at a baud rate of 115200.

Serial.begin(115200);

Set the TDS sensor pin as an input.

pinMode(TdsSensorPin,INPUT);

In the loop(), get new TDS readings every 40 milliseconds and save them in the buffer:

static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 40U){     //every 40 milliseconds,read the analog value from the ADC
  analogSampleTimepoint = millis();
  analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin);    //read the analog value and store into the buffer
  analogBufferIndex++;
  if(analogBufferIndex == SCOUNT){ 
    analogBufferIndex = 0;
  }
}   

Then, every 800 milliseconds, it gets the latest readings and gets the average voltage by using the filtering algorithm created before:

static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 800U){
  printTimepoint = millis();
  for(copyIndex=0; copyIndex<SCOUNT; copyIndex++){
    analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
      
    // read the analog value more stable by the median filtering algorithm, and convert to voltage value
    averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;

Then, it calculates a temperature compensation coefficient and calculates the TDS value taking that value into account:

//temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0)); 
float compensationCoefficient = 1.0+0.02*(temperature-25.0);
//temperature compensation
float compensationVoltage=averageVoltage/compensationCoefficient;
      
//convert voltage value to tds value
tdsValue=(133.42*compensationVoltage*compensationVoltage*compensationVoltage - 255.86*compensationVoltage*compensationVoltage + 857.39*compensationVoltage)*0.5;

Finally, it prints the TDS value in ppm:

Serial.print("TDS Value:");
Serial.print(tdsValue,0);
Serial.println("ppm");

Demonstration

After copying the code to the Arduino IDE, upload the code to your board. Don’t forget to select the right board in Tools > Board and the right COM port in Tools > Port.

After uploading, open the Serial Monitor at a baud rate of 115200 and press the ESP8266 RST button so that the code starts working.

TDS meter testing ESP32 ESP8266 Arduino

It will show a value of approximately 0 if the probe is not submerged. Put the probe on a solution to check its TDS. You can try with tap water and add some salt to see the values increase.

TDS Sensor Arduino IDE Serial Monitor

I measured the TDS value for tap water in my house, and I got a value of around 100ppm, which is a good value for drinking water.

I also tested tea, and the TDS value increased to about 230ppm, which seems a reasonable value.

Finally, I also measured the TDS value of bottled water and I got a value of around 25ppm. Do you have one of these sensors? What values did you get for bottled water?

Wrapping Up

A TDS meter can measure the total dissolved solids in a solution. It can be used as an indicator of water quality and allows you to characterize the water. The meter returns the TDS value in ppm (parts per million—mg/L). The TDS value has many applications but it cannot be used by itself to determine if the water is drinkable or not.

A great application of this type of sensor is an aquarium water quality monitor. You can use this sensor alongside a waterproof DS18B20 temperature sensor to monitor your fish tank, for example.

Are you interested in an Aquarium Water Quality Monitor? I was thinking about creating a web app to monitor and control your aquarium temperature and water quality and additionally, also be able to control a pump via an output pin of the ESP8266. What do you think?

We hope you found this tutorial useful. We have tutorials for other popular sensors that you may like:

Learn more about the ESP8266 with our resources:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

6 thoughts on “ESP8266 NodeMCU with TDS Sensor (Water Quality Sensor)”

    • Hi.
      No, I can read your comments just fine.
      They just need to be approved first.
      To measure nitrites you need a specific test.
      Basically, you need to add a specific chemical component (a chromophore that connects to nitrites) to a water sample and then, read the absorbance at a specific wave length.
      I don’t think something like this is available for these boards.
      Regards,
      Sara

      Reply
  1. Very interesting TDS if you want to calibrate the sensor you can use steam distilled water and that should be zero.

    I use the above to test my handheld TDS meter, and it works, thanks for the article

    Reply
  2. Hi
    Yes i would be interested in a web based aquarium monitor. I have learned by building and modifying your ESP8266 web server, but I still consider myself a newbie and am keen to learn more.
    Regards
    Wayne

    Reply
  3. The TDS sensor is interesting. The interface that the probe connects to uses a CD4060 to generate a signal of approx 2700 Hz (if my quick, top of my head calculation is right) (1/(2.310k1nF)/16 that is amplified and fed to the probe.
    The probe, basically being 2 elecrodes to be dipped in water, forms a voltage divider with an 1M ohm resistor. The output of this voltage divider is then fed to 3 amplifiers, a low pass filter and is rectified as it appears as an analog signal on the output of the interface board.

    So if we simplify it’s workings, it is not much different from the probes we use to measure soil moisture. Those actually do not measure moisture either, but just the amount of ions in the soil

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.