ESPHome not reading analog on ESP32 ADC

I’m trying to use a ESP32 (DEVKIT V1) to read my Capacitive Soil Moisture Sensor which has a pin that provides voltage to indicate soil moisture.

However, using ESP32 I only ever get 4095 (on an ADC pin). Using ESP8266 (D1 mini) it works, but I was hoping I could use the ESP32s I’ve purchased for this project.

This is my yaml:

sensor:
  - platform: adc
    raw: true
    pin: GPIO34
    name: "Soil"
    update_interval: 10s

And this is the result, no matter where the probe is, it always reads 4095 from the pin:

[11:10:17][D][sensor:094]: 'Soil': Sending state 4095.00000 V with 2 decimals of accuracy
[11:10:27][D][sensor:094]: 'Soil': Sending state 4095.00000 V with 2 decimals of accuracy

When I use a multimeter I get readings from between 1.8v and 2.8v depending on moisture levels. I’ve used two different capacitive sensors, same readings.

Writing code in Arduino on the ESP I do read values correctly.

const int SOIL_PIN = 34;

int v_in = 0;

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

void loop() {
  delay (1000);
  v_in = analogRead(SOIL_PIN);
  Serial.print("reading ");
  Serial.println(v_in);
}

returns:

21:28:29.733 -> reading 3662
21:28:30.730 -> reading 3664
21:28:31.720 -> reading 3685
21:28:32.708 -> reading 3670

It seems to be a problem specifically on ESPHome for ESP32 analog readings.

Any help will be great!

Maybe it’s the auto attenuation not working as well as you would hope. Try some manual attenuation values:

1 Like

Thank you very much, this was exactly my case. Originally the code was developed on ESP8266 and migrated to ESP32 later on. My mistake I didn’t bother reading the documentation again :slight_smile: Cheers !