ESP32, Grove TDS sensor and AnalogRead(): pls confirm I solved my issue

Hi guys,

I need just a confirmation that what I am doing is right.

I have connected the Grove TDS Sensor (Grove - TDS Sensor | Seeed Studio Wiki) to the ESP32 NodeMCU CP2102, and at the beginning I was simply testing it with the code available in the link and that you can find there:

#define SERIAL Serial
#define sensorPin A0
 
int sensorValue = 0;
float tdsValue = 0;
float Voltage = 0;
 
void setup() {
    SERIAL.begin(9600);
}
void loop() {
    sensorValue = analogRead(sensorPin);
    Voltage = sensorValue*5/1024.0; //Convert analog reading to Voltage
    tdsValue=(133.42*Voltage*Voltage*Voltage - 255.86*Voltage*Voltage + 857.39*Voltage)*0.5; //Convert voltage value to TDS value
    SERIAL.print("TDS Value = "); 
    SERIAL.print(tdsValue);
    SERIAL.println(" ppm");
    delay(1000);
}

After starting to lose my patience, I just tried to understand what was going on (value out of range), and, sadly, I just figure out that Analog Pin on ESP32 have a resolution of 12 bits and not 10 like on arduino.

So I needed to change the Voltage calculation in the code above.

This is what I have done, and results seem to be right now...

    Voltage = sensorValue*3.3/4095; //Convert analog reading to Voltage

Could you confirm that this is right?

I am pretty sure about the analog maximum of 4095, but what about the 5? I thought it was related to input voltage on the sensor (for this I have tried to use 3.3 because I was considering to use the 3.3V as power input) but I see that if I switch input voltage from 5 to 3.3 results don't change

So I have to keep it at 5? And in this case could you pls explain why?

Best,
Fabio

Hi, there

I am working on TDS measurement switching from Arduino UNO to ESP32, which having the same concern: the resolution ratio and the Vref.

After reading the sensor board schematics, I noticed the output analog voltage from sensor board won't change regardless of 3.3V or 5V voltage, which means if your different Vref voltage on micro-controller boards (3.3V or 5V), the reading will be different too...

So to reply back to your question:

  1. Arduino has 10bits ADC resolution (1024), ESP has 12bits ADC resolution(4096), which has to adjust accordingly.

  2. I believe the sensor board analog voltage output is based on 5V as reference, so if you use ESP32 with 3.3V, then obviously the reading with be *5/3.3. So, you need to keep 5V as Vref in ESP32 program no change...

Hope it helps.