DfRobot TDS Sensor Faulty Reading

Hello! I have a project, Arduino-Based Water Quality Monitoring Device. I've got temperature, pH, turbidity, and TDS Sensor. Here is the code:

#include <OneWire.h>
#include <DallasTemperature.h>
#include "DFRobot_PH.h" //added from PH Code 
#include <EEPROM.h> //Added from PH Code
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 column and 4 rows

#define ONE_WIRE_BUS 7
#define TdsSensorPin A1 //verify plugged in here 
#define PH_PIN A2 //
#define VREF 5.0     // analog reference voltage(Volt) of the ADC 0ther option is 3.3/5.0
#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;
int sensorPin = A0; //added from turbidity
float averageVoltage = 0,tdsValue = 0,temperature = 25;
float voltage,phValue; 
float Celcius=0;
float Fahrenheit=0;
float volt; //added from turbidity
float ntu; //added from turbidity

DFRobot_PH ph;

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

namespace pin {
const byte tds_sensor = A1;
const byte one_wire_bus = 7; // Dallas Temperature Sensor
const byte ph_sensor = A2;
}

void setup()
{
    Serial.begin(115200);
    ph.begin(); //added from ph 
    sensors.begin();
    pinMode(TdsSensorPin,INPUT);
  lcd.init(); // initialize the lcd
  lcd.backlight();
    
    
}

void loop()
{
 volt = 0; //beginning of added from turbidity
    for(int i=0; i<800; i++)
    {
        volt += ((float)analogRead(sensorPin));
    }
    volt = volt;
    if(volt > 777750){
      ntu = 1;
    }else{
      ntu = -0.303*(volt)+235654;
    
    } //end of added from turbidity 
  
   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 timepoint = millis();       //added from ph
    if(millis()-timepoint>1000U){                  //time interval added from ph
        timepoint = millis();           //added from ph
        voltage = analogRead(PH_PIN)/1024.0*5000;  // read the voltage
        phValue = ph.readPH(voltage,temperature);   //added from ph
    } 
   
   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("voltage:");
     // Serial.print(averageVoltage,2);
      //Serial.print("V   ");
      sensors.requestTemperatures(); 
      Celcius=sensors.getTempCByIndex(0);
      Fahrenheit=sensors.toFahrenheit(Celcius);
      //Serial.print(compensationCoefficient);
      //Serial.print("compensationCoefficient");
      //Serial.print(temperature);
      //Serial.print("temperature");

      delay(1500);
      Serial.print(millis()); 
      Serial.print("time since start in milliseconds");    
      Serial.print(" C  ");
      Serial.print(Celcius);
      Serial.print(" F  ");
      Serial.println(Fahrenheit);
      Serial.print(volt );
      Serial.print(" V ");
      Serial.print(ntu );
      Serial.print(" NTU ");
      Serial.print("pH:");
      Serial.print(phValue);
      Serial.print(" TDS Value:");
      Serial.print(tdsValue,0);
      Serial.println("ppm");
   }
     ph.calibration(voltage,temperature);           // calibration process by Serail CMD
 
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp:");

  lcd.setCursor(5,0);
  lcd.print(Celcius);
  lcd.print(" C   ");

  lcd.setCursor(17,0);
  if(Celcius>=25)                    lcd.print("BAD");
  if(Celcius<25 & Celcius>50)       lcd.print("OK");

  lcd.setCursor(0,1);
  lcd.print("Turb:");

  lcd.setCursor(5,1);
  lcd.print(ntu);
  lcd.print(" FTU ");

  lcd.setCursor(17,1);
  if(ntu<0.5)                        lcd.print("OK");
  if(ntu>=0.5)                       lcd.print("BAD");

  lcd.setCursor(0,2);
  lcd.print("pH:");

  lcd.setCursor(5,2);
  lcd.print(phValue);

  lcd.setCursor(17,2);
  if(phValue>=6.5 & phValue<=8.5)                   lcd.print("OK");
  if(phValue<6.5)                    lcd.print("BAD");
  if(phValue>8.5)                    lcd.print("BAD");

  lcd.setCursor(0,3);
  lcd.print("TDS:");

  lcd.setCursor (5,3);
  lcd.print(tdsValue, 0);

  lcd.setCursor(12,3);
  lcd.print("ppm");

  lcd.setCursor(17,3);
  if(tdsValue<1000)                lcd.print("OK");
  if(tdsValue>=1000)               lcd.print("BAD");



}

 
float round_to_dp( float in_value, int decimal_place ) //begin 
{
  float multiplier = powf( 10.0f, decimal_place );
  in_value = roundf( in_value * multiplier ) / multiplier;
  return in_value;
}

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;
}

now, i'm pretty confident with my other sensors, and my problems is the TDS sensor

the tds sensor reads 87 ppm although it is not submerged in water, and when I put the probe in water mixed with soy sauce and salt, it only shows around 94 ppm, which I think should be far more than that.

can someone help me with this?

When starting out with a new sensor, it is a good idea to test it with a simple example program, and make sure it works correctly without the added complications of other sensors. Most manufacturers provide such an example, along with a wiring diagram.

Thanks but where are the datasheet links to "[quote="xevioso, post:1, topic:1241872"]
temperature, pH, turbidity, and TDS Sensor.
[/quote]

"?

All according to the advice in this link: How to get the best out of this forum - Using Arduino / IDE 1.x - Arduino Forum

Hello, sir! Thank you for your response.

Our group already tried to use it on a separate code, just the TDS sensor alone, and the reading is just the same (87 ppm when not submerged to any liquid, 96 ppm to soy sauce).

Hello, sir! Here it is:

Temperature Sensor: Interfacing DS18B20 1-Wire Digital Temperature Sensor with Arduino

pH sensor: Gravity__Analog_pH_Sensor_Meter_Kit_V2_SKU_SEN0161-V2-DFRobot

TDS Sensor: Analog TDS Sensor Meter for Arduino / ESP32 / Raspberry Pi - DFRobot Wiki

Turbidity Sensor: Turbidity_sensor_SKU__SEN0189-DFRobot


Here is the circuit diagram for our project

It's a picture that is not much useful. Schematics tells the names of all pins involved. A picture only shows the physical positions, useless for reading the logic in the wiring.

Perhaps the sensor is defective.

Reading the TDS link You posted I'll say You're fine. The sensor inaccurasy is 10%. Check up the reference voltage for the AD conversion in the controller. Often the USB "5 volt" is the reference, and it's NEVER 5.000 volt.

Make Yourself a calibration factor that gives You the top value You want to reach.

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