ESP32 analogRead stuck at 4095 as soon as WiFi connects
Long-time reader, first post. Building soil moisture probes for my vegetable beds: ESP32 DevKit v1, a capacitive soil sensor v1.2 on GPIO 4, sending the raw value and a percentage to Hermetry every 5 minutes. On the bench with the USB serial monitor everything looks right: 1500 dry, 3300 in a glass of water.
Then I enable WiFi and the MQTT client, and the reading goes to 4095 and stays there. Comment out WiFi.begin() and it’s back to normal. I tried analogSetPinAttenuation(4, ADC_11db), a 100 nF cap on the signal, a different sensor, same thing. Dashboard just shows a flat 100 % line which is pretty but useless.
raw=1512 pct=99 (wifi off)
raw=4095 pct=0 (wifi on) <- every reading after connect
Is the sensor powered from 3.3 V or 5 V? Some of those boards output more than 3.3 V on 5 V supply.
3.3 V from the DevKit pin.
maybe the WiFi pulls too much current and the 3.3 V sags? I had brownouts on mine
Which pin again? GPIO 4?
following, my moisture sensor does something weird too when it sends
Could it be a library conflict with the MQTT lib?
It’s the pin. GPIO 4 is on ADC2, and on the ESP32 the WiFi driver uses ADC2, so analogRead() on any ADC2 pin fails while WiFi is on. Depending on the core version you get 0, 4095 or an error in the log. Same sensor on GPIO 34 works fine. ADC1 is GPIO 32-39, and 34-39 are input-only, which is all you need for a sensor.
So: move the wire to GPIO 34 (or 32/33/35/36/39), change the pin number, done. No code changes for WiFi.
Two smaller things while you’re in there. Take 16 readings and average them, the ESP32 ADC is noisy and a bit non-linear at the ends. And send both soil_raw and soil_pct, so you can recalibrate later from the raw history without reflashing.
const int SOIL = 34; // ADC1_CH6
uint32_t sum = 0;
for (int i = 0; i < 16; i++) { sum += analogRead(SOIL); delay(2); }
int raw = sum / 16;
int pct = constrain(map(raw, 3300, 1500, 0, 100), 0, 100);
Moved the wire to 34 and it reads 1480 with WiFi on. Never would have guessed that, I’d spent two evenings on caps and attenuation. Chart looks like a soil chart now.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

