Can a TDS sensor read more than value? if someone did it before can you please direct me?

I have successfully programmed the TDS sensor with ESP8266 but it only reads 1 value which is the ppm of water, but I know that some of you guys had done great work and achieved more results with the TDS and i hope someone would help me to let the TDS read more than a mineral value if it could read it, such as:

Magnesium
Sodium
Calcium
Sulphate
Chloride
Potassium
pH

This is what i have achieved through my code which is just the ppm:

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

If you have done the code more simpler there is no problem i'm open to learn more because this is the first time for me working with the TDS sensor.

Thank you guys i look forward for your answers

A wild guess here as I had to google what a TDS sensor was ( = Total Dissolved Solids? ), but maybe the "Total" part of the name implies you cannot get individual readings from this type of sensor.

1 Like

Great, if it reads solids then we can get those values but the question now how can we get those value in code🤔

Looks like the same code would measure any of those solids, but it can't distinguish which type of solid, I think.

Looks like these are simple conductivity probes and would not be able to distinguish between different ions in solution.

You can get a partial answer with an "NPK" (Nitrogen, Potassium, Phosphorus) sensor:

The TDS sensor simply measures conductivity.

If you know the ion types and fractions by weight, a scale factor can be used to convert the conductivity into PPM units.

"TDS" means total dissolved solids. It is not possible to use the TDS measurement to know which particular solids are present in solution.

BTW the "NPK sensor" linked in post #6 above is a scam. It is just another conductivity sensor.

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