DIY Water Quality Testing: ESP8266 NodeMCU and TDS Sensor

by AKP in Circuits > Arduino

167 Views, 0 Favorites, 0 Comments

DIY Water Quality Testing: ESP8266 NodeMCU and TDS Sensor

esp8266-nodemcu-tds-water-quality-sensor.jpg

Within this manual, you will acquire knowledge on employing a TDS Sensor ESP8266 NodeMCU board. The TDS meter gauges the aggregate of dissolved solids, such as salts, minerals, and metals, within a solution. This metric serves as a means to assess water quality and facilitate comparisons between water obtained from various origins. A primary application of the TDS meter is the monitoring of aquarium water quality.

We will utilize the TDS meter from keystudio and provide a straightforward illustration demonstrating how to measure TDS in parts per million (ppm) using the Arduino IDE.

Introducing the TDS Meter

TDS-meter-keystudio.jpg

A TDS meter is designed to quantify the total dissolved solids, including salts, minerals, and metals, present in water. As the quantity of dissolved solids in water rises, so does the water’s conductivity, enabling us to compute the total dissolved solids in parts per million (ppm) or milligrams per liter (mg/L).

While this serves as a valuable gauge for monitoring water quality, it’s crucial to note that it doesn’t assess contaminants in the water. Therefore, relying solely on this indicator is insufficient to determine the suitability of water for consumption.

TDS meters find utility in various applications such as monitoring water quality in pools, aquariums, fish tanks, hydroponic systems, water purifiers, and more.

In this tutorial, we will employ the keystudio TDS meter, which includes an interface module and an electrode probe (as depicted in the image above).

For additional details about the TDS meter, we recommend consulting the official documentation.

Features and Specifications

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 Needles: 2
  • Total Length: 60cm
  • Connection Interface: XH2.54-2P
  • Color: White
  • Waterproof Probe

ESP8266 TDS (Water Quality) Reading – Code

ESP8266-TDS-Sensor.jpg

As mentioned earlier, the sensor generates an analog signal that can be translated into TDS in parts per million (ppm). We are utilizing the code outlined in the sensor documentation with certain adjustments.

For enhanced precision, it is advisable to calibrate your sensor against a solution with a known TDS value. Nevertheless, calibration may not be necessary if your focus is on a qualitative assessment of TDS rather than specific values.

Proceed to upload the subsequent code to your ESP8266.

#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");
}
}
}

Presentation

TDS-meter-ESP32-ESP8266-arduino-testing.jpg

Once you have copied the code into the Arduino IDE, proceed to upload it to your board. Remember to choose the correct board under Tools > Board and the appropriate COM port under Tools > Port.

After the upload, launch the Serial Monitor with a baud rate of 115200 and press the ESP8266 RST button to initiate the code.

If the probe is not submerged, it will display a value of approximately 0. Place the probe in a solution to assess its TDS. You can experiment with tap water and introduce some salt to observe an increase in values.

I conducted a TDS measurement for tap water in my residence, yielding a value of around 100ppm, indicating good drinking water quality.

Testing with tea resulted in a TDS value of approximately 230ppm, a reasonably expected outcome.

Lastly, I measured the TDS value of bottled water, registering around 25ppm. Do you have one of these sensors? What values did you obtain for bottled water?

See Full Article Here