ESP32 analogRead se queda en 4095 en cuanto se conecta el WiFi
Traducido del English por IA ·
Lector desde hace tiempo, primera publicación. Estoy construyendo sondas de humedad del suelo para mis bancales de hortalizas: ESP32 DevKit v1, un sensor capacitivo de suelo v1.2 en GPIO 4, que envía el valor en bruto y un porcentaje a Hermetry cada 5 minutos. En la mesa, con el monitor serie USB, todo parece correcto: 1500 en seco, 3300 en un vaso de agua.Luego activo el WiFi y el cliente MQTT, y la lectura sube a 4095 y se queda ahí. Si comento WiFi.begin(), vuelve a la normalidad. Probé analogSetPinAttenuation(4, ADC_11db), un condensador de 100 nF en la señal, otro sensor: lo mismo. El panel solo muestra una línea plana al 100 %, muy bonita pero inútil.
raw=1512 pct=99 (wifi off)
raw=4095 pct=0 (wifi on) <- every reading after connect
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
Traducido del English por IA ·
¿El sensor está alimentado a 3.3 V o a 5 V? Algunas de estas placas dan más de 3.3 V con una alimentación de 5 V.
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.
Traducido del English por IA ·
3,3 V desde el pin del DevKit.
3.3 V from the DevKit pin.
Traducido del English por IA ·
quizás el WiFi consume demasiada corriente y el 3.3 V cae? Yo tuve brownouts en el mío
maybe the WiFi pulls too much current and the 3.3 V sags? I had brownouts on mine
Traducido del English por IA ·
¿Cuál pin era? ¿GPIO 4?
Which pin again? GPIO 4?
Traducido del English por IA ·
Siguiendo con el tema, mi sensor de humedad también hace algo raro cuando envía
following, my moisture sensor does something weird too when it sends
Traducido del English por IA ·
¿Podría ser un conflicto de librerías con la librería MQTT?
Could it be a library conflict with the MQTT lib?
Traducido del English por IA ·
Es el pin. El GPIO 4 está en el ADC2, y en el ESP32 el driver de WiFi usa el ADC2, así que analogRead() en cualquier pin del ADC2 falla mientras el WiFi está activo. Según la versión del core, obtendrás 0, 4095 o un error en el log. El mismo sensor en el GPIO 34 funciona bien. El ADC1 va del GPIO 32 al 39, y los 34-39 son solo de entrada, que es todo lo que necesitas para un sensor.Así que: mueve el cable al GPIO 34 (o 32/33/35/36/39), cambia el número de pin y listo. No hacen falta cambios de código para el WiFi.Dos cosas menores mientras lo revisas. Toma 16 lecturas y promédialas, ya que el ADC del ESP32 es ruidoso y algo no lineal en los extremos. Y envía tanto soil_raw como soil_pct, para poder recalibrar después desde el historial de datos brutos sin reflashear.
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);
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);
Traducido del English por IA ·
Moví el cable al 34 y ahora lee 1480 con el WiFi encendido. Nunca lo habría adivinado, me había pasado dos tardes con los condensadores y la atenuación. La gráfica ahora parece la de humedad del suelo.
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.

