Interfacing TDS sensor and dosing pump

Hello, I have some problem right here. I want to create a smart dosing system. I am using TDS sensor to check the conductivity of the solution while dosing pump used to deliver the nutrient solution into the water. In this project I want TDS sensor to measure the initial reading in the water first before the dosing pump can start the dosing process. If the TDS sensor get a reading below 1000 ppm at initial, the dosing pump will start its dosing process with 1 second delay. This process will be keep continue until the TDS sensor gets a reading of 1000 ppm. When the TDS sensor get a reading equal to 1000 ppm, the dosing pump will be stop working and print the reading in serial monitor and the dosing process is complete.

The problem is when I run the experiment, the TDS sensor reading does not change (which is it remain at 0 ppm) in the serial monitor even I have already submerge the TDS sensor into the solution causing the dosing pump keep continue ON.

Here is my coding:

#define TdsSensorPin 34
#define VREF 3.3              // analog reference voltage of the ADC
#define SCOUNT  30            // sum of sample point
#define RELAY_PIN_1 18
#define RELAY_PIN_2 19

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;     // Floats to calculate average voltage & TDS value
float tdsValue = 0;
float temperature = 25;       // 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);
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);
}

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 / 4096.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;

      if (tdsValue < 1000) {
        Serial.print("TDS Value Low:");
        Serial.print(tdsValue, 0);
        Serial.println("ppm");

        // turn on pump A & B for 1 seconds
        digitalWrite(RELAY_PIN_1, LOW);
        digitalWrite(RELAY_PIN_2, LOW);
        delay(1000);

        digitalWrite(RELAY_PIN_1, HIGH);
        digitalWrite(RELAY_PIN_2, HIGH);
        delay(1000);
      }
      else if (tdsValue = 1000) {
        Serial.print("TDS Value:");
        Serial.print(tdsValue, 0);
        Serial.println("ppm");
      }

      else {}

    }
  }
}

Here is my circuit diagram:

Here is my serial monitor during the process:
Serial monitor (error)

#esp32, #TDSsensor, #Minipump, #smart dosing system

Have you done the first step, which is to get the TDS sensor working properly without the pump?

try commenting out the

  pinMode(TdsSensorPin, INPUT);

statement
GPI34 is already an analog input pin - you are setting it as digital input pin

Use a logic analyzer to see what happens.

Insert Serial.println()´s at points of interrest.

Yes. I have already test the TDS sensor and dosing pump separately and both of it is working fine and normal. But when I try to combine the codes, the TDS sensor reading does not changing (remain at 0 ppm) even after I already submerged the TDS sensor into the solution. The TDS sensor reading should be increase actually not remain at 0 ppm.

This is a serious mistake, but probably not the main problem.

For the mistake, can you state the reason, it would be really appreciated.

"==" is used for equality comparison. "=" assigns a value.

Thank you.

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