HELP! Turbidity Sensor Not working with ESP 32 Project

Hello,
I am working on a project that involves 4 sensors: vibration, TDS, water, and turbidity. I went ahead and coded each sensor individually to see if they all work and have added them to a dashboard that I am making. The vibration, TDS, and water sensors are working great with the dashboard, however, I am not getting a read from my turbidity sensor even though I have tested it individually. In the individual test, I am getting read from the serial console, but with the combined code, IT IS NOT WORKING AT ALL! In the code below, you can see that it is already declared as a float value since that is what I want the gauge to display. I'm super sad that it is not reading even though it works by itself. Can anyone help me troubleshoot?
Here is my code from the full program:

/*
  The following variables are automatically generated and updated when changes are made to the Thing

  float tdsValue;
  float turbidityValue;
  int vibrationSensor;
  bool waterLevel;
*/

#include "thingProperties.h"
#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,temperature = 25;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  Serial.begin(115200);
  //water sensor
  pinMode(34,INPUT);
  
  //TDS sensor
  pinMode(33, INPUT);
  
  //vibration Sensor
  pinMode(32, INPUT);
  
  //turbidity Sensor
  pinMode(25, INPUT);
  
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Water level code 
  waterLevel=digitalRead(34);
  
  //vibration sensor code
  vibrationSensor = TP_init();
  
  /*if(vibrationSensor>15000)
  {
    spill= true;
  }
  else
  {
    spill = false;
  }
  */
  
  //Turbidity Sensor Code
  turbidityValue = analogRead(25)*(5.0/4095.0);

  //TDS code
   static unsigned long analogSampleTimepoint = millis();
  if (millis() - analogSampleTimepoint > 40U)  //every 40 milliseconds,read the analog value from the ADC
  {
    analogSampleTimepoint = millis();
    analogBuffer[analogBufferIndex] = analogRead(33);    //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

  }

}

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

int TP_init(){
  int measurement=pulseIn(32, HIGH);  //wait for the pin to get HIGH and returns measurement
  return measurement;
}

Again, I'm not sure why this is happening SOMEONE PLEASE HELP :'((

Move these outside the "comment" block...

/*
    The following variables are automatically generated and updated when changes are made to the Thing
*/

float tdsValue;
float turbidityValue;
int vibrationSensor;
bool waterLevel;

Thanks for using code tags on your first post!

Please explain. What did you expect to happen, and actually happens?

Hopefully, you are not connecting a 5V sensor output to an 3.3V ESP32 analog input.

Is "25" an analog input? If so, what does this do?

  //turbidity Sensor
  pinMode(25, INPUT);

In any case, the scaling here is not correct.

  //Turbidity Sensor Code
  turbidityValue = analogRead(25)*(5.0/4095.0);

Tutorial: ESP32 Analog Input with Arduino IDE | Random Nerd Tutorials

So the reason why the variables are in the comments is that I am using the Arduino IOT cloud dashboard and the variables are already declared and associated with a widget. If I declared them again, I get an error.

Thank you for taking the time to read my discussion post!
So, I went ahead and programmed the turbidity sensor individually and tested it with my esp32, and the serial monitor was getting a value of around 3400 when outside of water and 4095 when it was inside of the water. The scaling that I used is analogInput* 5/4095 (to return the voltage value) since the sensor that I bought says that it can be connected to 5v and the analog input was going up to 4095. Once I did this, I was getting a correct reading when I added dirt into my water to make it cloudy (the lower the voltage, the dirtier the water is)

When I tried integrating this into my Arduino IOT cloud code, I am not getting a reading at all :frowning: Im not too sure why it works individually, but not with the rest of my code.

Any input that you can give would be amazing :')

The ESP32 analog input cannot be connected to any voltage higher than 3.3V, or it will be destroyed.

For the ESP32, the voltage value from the analog input is given approximately by

float V = analogInput* 3.3/4096;

Thank you so much! This cleared up a lot for me :smiley:

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