Get wrong ADC value from ESP32 DevKit

Hi I was testing 2 soil humidity sensors with ESP32 DevKit and Arduino UNO. The sensor supplier told me that with 5V the value should be around 310 ~ 565, and Arduino UNO looked as expected but ESP32 DevKit got wrong. Here is the code:

#include <Arduino.h>

#ifdef ESP32
static int SENSOR_A = 12;
static int SENSOR_B = 13;
#elif ARDUINO_AVR_UNO
static int SENSOR_A = A0;
static int SENSOR_B = A1;
#endif

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

void loop()
{
  Serial.print("A: ");
  Serial.println(analogRead(SENSOR_A));
  Serial.print("B: ");
  Serial.println(analogRead(SENSOR_B));
  delay(1000);
}

// Arduino
A: 572
B: 543

// ESP32 with SENSOR_A = 12 and SENSOR_B = 13
A: 510
B: 137

// ESP32 with SENSOR_A = 32 and SENSOR_B = 33
A: 3200
B: 3400

I'm not sure if I should set SENSOR_A/B as 12/13 or 32/33, as I took the random nerd tutorials as the pinout reference:

But neither printed an expected value. Any ideas?

Thank you!

The ESP32 is 3.3V only and cannot measure 5V. You will burn it out if you try.

It also has a higher resolution ADC (12 bits instead of 10), so expect 4x larger numbers for the same input voltage.

However, the ESP32 ADC is quite nonlinear, and not particularly useful for quantitative measurements.

Thanks for your quick reply!
I'm a software engineer who is quite familiar with code but I have almost zero concept of hardware/electricity. Is there anyway I can change the code and pinout of ESP32 to make it work like Arduino UNO?

You can specify the resolution of the ESP32 ADC to be 10 bits. But it does not work as well as the Arduino 10 bit ADC, by a long shot. For example, you cannot measure voltages less than about 0.2V with it. The red curve below

You can't change the ESP32 pinout arbitrarily, although with advanced programming techniques (accessing the HAL), some flexibility is built in.

The supplier just told me that if it's 3.3V, the value should be around 460 ~ 840. Inspired by you, I modified my code and pinout like this:

#include <Arduino.h>

#ifdef ESP32
static int SENSOR_A = 32; // D32
static int SENSOR_B = 33; // D33
#elif ARDUINO_AVR_UNO
static int SENSOR_A = A0;
static int SENSOR_B = A1;
#endif

void setup()
{
  Serial.begin(115200);
  pinMode(SENSOR_A, INPUT);
  pinMode(SENSOR_B, INPUT);
}

void loop()
{
  analogReadResolution(10); // Lower resolution ADC
  Serial.print("A: ");
  Serial.println(analogRead(SENSOR_A));
  Serial.print("B: ");
  Serial.println(analogRead(SENSOR_B));
  delay(1000);
}

Seems the result is correct now:

A: 843
B: 851

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