# Problem with my TDS&pH meter project

**URL:** <https://forum.arduino.cc/t/problem-with-my-tds-ph-meter-project/991461>\
**Category:** Science and Measurement\
**Created:** [May 13, 2022, 12:00pm UTC](https://forum.arduino.cc/t/problem-with-my-tds-ph-meter-project/991461 "2022-05-13T12:00:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevKevin](https://avatars.discourse-cdn.com/v4/letter/k/6bbea6/32.png) [@KevKevin](https://forum.arduino.cc/u/KevKevin)\
**Post date:** [May 13, 2022, 12:00pm UTC](https://forum.arduino.cc/t/problem-with-my-tds-ph-meter-project/991461/1 "2022-05-13T12:00:51Z")

</div>

Hi folks. I am trying to making my own water quality monitoring system with Arduino UNO just like those YouTube videos. This system has a TDS probe, a pH probe and a LCD1602 with I2C to show the result. Since I know little about Arduino, I found different pieces of code for each function from GitHub and put them together. With the help of notes, I can understand most part. The pH part is running smoothly, but the TDS has some problem:

1. TDS result display is not updated correctly when I took the TDS probe out of liquid. It supposed to be 0, but it was showing like circle in the pic:
2. The TDS value update has a significant lag. In the picture, the red boxed part shows when I took the probe out, it took this huge lag to update the decrease in TDS value  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/e/b/0eb5b5991e9715364000c784142d2731ecd4c346.jpeg)

Here is my code:

```cpp
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define SensorPin 1 // the pH meter Analog output is connected with the Arduino’s Analog
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10],temp;

#define TdsSensorPin A0
#define VREF 5.0 // 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,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;

void setup()
{
    Serial.begin(9600);
    lcd.init(); 
    lcd.begin(16, 2);
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print(" Welcome to ");
    lcd.setCursor(0, 1);
    lcd.print(" Water Tect "); //for fun
    delay(2000);
    lcd.clear();
    pinMode(TdsSensorPin,INPUT);
}

void loop()
{

  //TDS parameter
   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];
      averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
      float compensationCoefficient=1.0+0.02*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
      float compensationVolatge=averageVoltage/compensationCoefficient; //temperature compensation
      tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5; //convert voltage value to tds value
     
      Serial.print("TDS Value:");
      Serial.print(tdsValue,0);
      Serial.println("ppm");

  //pH parameter

      for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++) //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++) //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue; //convert the millivolt into pH value
  

  lcd.setCursor(0, 0);
  lcd.print("TDS : "); 
  lcd.print(tdsValue,0);
  lcd.setCursor(0, 1);
  lcd.print("pH Val:");
  lcd.setCursor(8, 1);
  lcd.print(phValue);
  delay(2000);
   }
}
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;
}
 

```

I tried to figure the problem out, but it's just not working.

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [May 13, 2022, 12:05pm UTC](https://forum.arduino.cc/t/problem-with-my-tds-ph-meter-project/991461/2 "2022-05-13T12:05:57Z")

</div>

It looks like the display has some characters left over from when TDS was non zero. You'll need to overwrite with spaces before you print the new value.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 9, 2022, 12:06pm UTC](https://forum.arduino.cc/t/problem-with-my-tds-ph-meter-project/991461/3 "2022-11-09T12:06:13Z")

</div>

Closed on November 9, 2022, 12:06pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
