Median of 32 reads: taming noisy analog probes
Learn how the firmware uses median filtering on 32 samples to reject noise spikes from pumps and lamps, ensuring stable TDS and turbidity readings.

Most new homelab builders assume that a single reading from an analog probe is accurate enough for their monitoring dashboard. That is the trap. Analog sensors connected to water or air often suffer from electrical noise caused by nearby equipment like filter pumps and lamp timers. A single spike in voltage can skew your data, making it look like a sudden change in water quality when it is just interference.
The book opens its firmware chapter with exactly this problem under the heading reading the probes. It explains that taking one reading is risky, so the firmware takes 32 samples and keeps the median. This post covers why the median works better than an average, how to configure the PlatformIO project for the XIAO board, and the exact code structure used to clean the data before sending it to your server.
One caveat, straight from the book: this is general guidance for the specific hardware outlined in Chapter 3 (the Seeed XIAO ESP32 C3). Your board may have different pin mappings or ADC characteristics, so check every number against your own hardware.
The trap: letting spikes skew your average
The usual way people find out their analog readings are noisy is that the graph shows sudden, impossible jumps. If you calculate a simple average of 32 random samples, one massive spike can pull the entire result up or down significantly. The book’s answer is to use the median instead of the mean for analog pins.
The firmware reads one sensor at a time, takes 32 samples, and keeps the middle value. This approach ensures that a single spike in the data stream cannot skew the final reading. The filter pump and the lamp timer are where these spikes usually come from. By sorting the 32 values and picking the center, you throw out the outliers that an average would otherwise include.
To implement this, you need to set up your PlatformIO environment correctly. The project configuration in platformio.ini must match the XIAO board exactly:
[env:xiao_c3]
platform = espressif32
board = seeed_xiao_esp32c3
framework = arduino
monitor_speed = 115200
build_flags =
-DARDUINO_USB_MODE=1
-DARDUINO_USB_CDC_ON_BOOT=1
lib_deps =
paulstoffregen/OneWire
milesburton/DallasTemperature
bblanchon/ArduinoJson@^7
The board = seeed_xiao_esp32c3 line picks the pin names like D1, D2, and D4. If you use a different board name, those letters map to different physical pins. The monitor_speed = 115200 sets the serial speed; if your monitor shows garbage characters, this number disagrees with Serial.begin() in the code. The two build_flags enable USB CDC so you can watch readings via the USB-C port without extra drivers.
The fix: median filtering in code
The core of the noise reduction lies in a small function that handles the analog pins. It takes 32 readings, sorts them, and returns the middle value. Here is the exact implementation from the book:
static float medianMv(int pin) { // 32 reads, keep the median
uint16_t v[32];
for (int i = 0; i < 32; i++) { v[i] = analogReadMilliVolts(pin); delay(10); }
std::sort(v, v + 32);
return (v[15] + v[16]) / 2.0f;
}
This function uses analogReadMilliVolts(), which applies the chip’s factory calibration and returns millivolts instead of raw counts. This is accurate up to about 2.5 V at the default attenuation. The delay(10) spaces the reads 10 milliseconds apart, totaling about a third of a second for the full set. The sort is what makes it a median; without it, you would just be averaging noisy data.
You must also define the pins correctly in your code to ensure you are reading from the right GPIOs:
#define TEMP_PIN D4 // GPIO6, DS18B20 DAT (adapter has the pull-up)
#define TDS_PIN D1 // GPIO3, ADC1
#define TURB_PIN D2 // GPIO4, ADC1, through the /5 divider module
#define TURB_RATIO 5.0f // 2.0f if you used the two-resistor divider
Both analog pins are on ADC1, which matters on an ESP32 because ADC2 stops working while Wi-Fi is active. ADC1 does not have this limitation. If you built a different voltage divider for the turbidity sensor, adjust TURB_RATIO accordingly (e.g., 2.0f) to undo the signal shrinking before it reaches the pin.
The trap: reading probes in parallel or during network calls
Another common mistake is trying to read multiple probes at the same time or reading them while sending data over HTTPS. The book warns that reading TDS and turbidity probes together can make the TDS number jumpy because they both put a small voltage into the same water. Sequential reads fix most of this issue.
Furthermore, you should never read probes inside the send function to "get fresh numbers." A failed TLS handshake can take 15 seconds, and if your watchdog is set to 90 seconds, the reading cadence becomes dependent on network stability. Instead, keep sampling and sending on separate timers.
The main loop in the firmware handles this separation cleanly:
void loop() {
esp_task_wdt_reset();
uint32_t now = millis();
if (now - lastSample >= SAMPLE_MS) { lastSample = now; sampleOnce(); }
if (now - lastSend >= sendEveryMs) {
lastSend = now;
pushReading();
if (WiFi.status() != WL_CONNECTED) connectWifi();
if (WiFi.status() == WL_CONNECTED && sendBuffered()) failures = 0;
else if (++failures >= 8) ESP.restart();
}
delay(200);
}
The first if runs sampleOnce() every SAMPLE_MS (30 seconds), using millis() arithmetic that survives the 49-day rollover. The second if handles the sending interval. This structure ensures that network issues do not delay your sensor readings.
Checklist
- Verify
board = seeed_xiao_esp32c3inplatformio.inimatches your hardware. - Set
monitor_speed = 115200to avoid garbage serial output. - Use
analogReadMilliVolts()for ADC1 pins to get calibrated millivolt values. - Implement the
medianMvfunction to sort 32 samples and return the middle value. - Define
TURB_RATIOcorrectly based on your voltage divider (5.0f or 2.0f). - Keep sampling and sending on separate timers in the main loop.
- Reject DS18B20 values of -127°C and 85.0°C as invalid readings.
- Reset the watchdog with
esp_task_wdt_reset()to prevent wedged reboots.
