Problem with getting proper outputs using sensors

#define TdsSensorPin A1
#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;
int copyIndex = 0;

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

float calibration_value = 21.34;
int phval = 0; 
unsigned long int avgval; 
int buffer_arr[10],temp;

int sensorPin = A2;
float volt;
float ntu;
 

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

void loop()
{
  tds();
  ph();
  tur();
}

void ph() {
 for(int i=0;i<10;i++) 
 { 
 buffer_arr[i]=analogRead(A0);
 delay(30);
 }
 for(int i=0;i<9;i++)
 {
 for(int j=i+1;j<10;j++)
 {
 if(buffer_arr[i]>buffer_arr[j])
 {
 temp=buffer_arr[i];
 buffer_arr[i]=buffer_arr[j];
 buffer_arr[j]=temp;
 }
 }
 }
 avgval=0;
 for(int i=2;i<8;i++)
 avgval+=buffer_arr[i];
 float volt=(float)avgval*5.0/1024/6;
 float ph_act = -5.70 * volt + calibration_value;
  Serial.println(ph_act);
 delay(1000);
}

// 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 tds()
{
    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");
      delay(10);
    }
  }
}

void tur()
{
    
    volt = 0;
    for(int i=0; i<800; i++)
    {
        volt += ((float)analogRead(sensorPin)/1023)*5;
    }
    volt = volt/800;
    volt = round_to_dp(volt,2);
    if(volt < 2.5){
      ntu = 3000;
    }else{
      ntu = -1120.4*square(volt)+5742.3*volt-4353.8; 
    }
    Serial.println(ntu);
    delay(1000);
}

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

Good day to all my friends here, i am a newbie to arduino trying to get the value from analog pH sensor, turbidity sensor and tds sensor. The code above i tried was able to get the input from sensors, but the output is rather abit peculiar. I tested my sensors with only water (should be pH7, 0-5NTU and small value of tds). I have tried calibrating each of them and tested them with their own code individually, everything worked just fine until I put all the individual codes together. The output from serial monitor went unstable. The NTU was supposed to be within 5 now has exceed 170+, pH has lowered from 6-7 to 3-4 and the tds is not working finely (the value is 0 when it is not in contact with water and it stayed at 75ppm). So, im thinking that maybe something is wrong with my code arrangement but i have no clue on what is wrong. Could someone help me out?

When reading analog inputs, it is often necessary to ensure an accurate read. That is usually done by reading each input successively, back to back. That ensures that even if the input is being driven by a high-impedance source, the input gets the 'correct' input signal value. In your case, I'd simply do each read twice.

The reason is, the input is being read by a multiplexer with a finite input capacitance, resulting in a bit of charge transfer from one input to the next, which disturbs the value being read.

If that isn't it, then you've likely mucked up the readings. Schematic, please.
This:

I have tried calibrating each of them and tested them with their own code individually, everything worked just fine until I put all the individual codes together.

Is why I'm requesting schematic - to see what you've done for other reasons.

The best way forward is to get each sensor working alone and make sure that the results are as expected, using the example code. Then combine the sensors, one at a time.

sorry, I did not do any schematic for it. But the overall of the view, is arduino uno, connect a0-2 with three sensors, while the 5V and ground are connected to breadboard so the sensors can just connect their power supply and ground there while the analog outputs will be connected to a0-2. That is how i connect the sensors to my arduino uno :sweat_smile:

Checked out. Have fun!

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