ESP32 analogRead bleibt bei 4095 hängen, sobald sich WiFi verbindet
Von English mit KI übersetzt ·
Langjähriger stiller Leser, erster Beitrag. Ich baue Bodenfeuchtesensoren für meine Gemüsebeete: ESP32 DevKit v1, ein kapazitiver Bodensensor v1.2 an GPIO 4, der alle 5 minutes den Rohwert und einen Prozentwert an Hermetry sendet. Auf dem Tisch, mit dem seriellen USB-Monitor, sieht alles richtig aus: 1500 trocken, 3300 in einem Glas Wasser.Sobald ich WiFi und den MQTT-Client aktiviere, springt der Messwert auf 4095 und bleibt dort. Wenn ich WiFi.begin() auskommentiere, ist er wieder normal. Ich habe analogSetPinAttenuation(4, ADC_11db) probiert, einen 100-nF-Kondensator am Signal, einen anderen Sensor, dasselbe Ergebnis. Das Dashboard zeigt nur eine flache 100-%-Linie, die zwar hübsch, aber nutzlos ist.
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
Von English mit KI übersetzt ·
Wird der Sensor mit 3.3 V oder 5 V versorgt? Einige dieser Boards geben bei 5 V Versorgung mehr als 3.3 V aus.
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.
Von English mit KI übersetzt ·
3,3 V vom DevKit-Pin.
3.3 V from the DevKit pin.
Von English mit KI übersetzt ·
Vielleicht zieht das WLAN zu viel Strom und die 3.3 V brechen ein? Bei mir gab es Brownouts
maybe the WiFi pulls too much current and the 3.3 V sags? I had brownouts on mine
Von English mit KI übersetzt ·
Welcher Pin noch mal? GPIO 4?
Which pin again? GPIO 4?
Von English mit KI übersetzt ·
Im Anschluss macht mein Feuchtigkeitssensor beim Senden ebenfalls etwas Seltsames
following, my moisture sensor does something weird too when it sends
Von English mit KI übersetzt ·
Könnte es ein Bibliothekskonflikt mit der MQTT-Bibliothek sein?
Could it be a library conflict with the MQTT lib?
Von English mit KI übersetzt ·
Es liegt am Pin. GPIO 4 liegt an ADC2, und beim ESP32 nutzt der WLAN-Treiber ADC2, daher schlägt analogRead() an jedem ADC2-Pin fehl, solange WLAN aktiv ist. Je nach Core-Version erhältst du 0, 4095 oder einen Fehler im Log. Derselbe Sensor an GPIO 34 funktioniert einwandfrei. ADC1 liegt auf GPIO 32-39, und 34-39 sind reine Eingänge, was für einen Sensor genau das Richtige ist.Also: Kabel an GPIO 34 (oder 32/33/35/36/39) umstecken, Pinnummer ändern, fertig. Keine Codeänderungen für WLAN nötig.Zwei kleinere Dinge, solange du dabei bist. Nimm 16 Messwerte und bilde den Mittelwert, der ADC des ESP32 ist rauschig und an den Enden etwas nichtlinear. Und sende sowohl soil_raw als auch soil_pct, damit du später aus der Rohdatenhistorie neu kalibrieren kannst, ohne neu zu flashen.
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);
Von English mit KI übersetzt ·
Das Kabel auf 34 umgesteckt, jetzt liest er bei eingeschaltetem WLAN 1480. Darauf wäre ich nie gekommen, ich hatte zwei Abende mit Kondensatoren und Attenuation verbracht. Die Kurve sieht jetzt aus wie eine Bodenfeuchte-Kurve.
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.
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

