Original guides / Guide

Reason about an ESP32 sensor pipeline

Inspect calibration, smoothing and thresholds with controlled ADC samples, without claiming a hardware test.

You will learn to

  • Convert a raw count to a voltage estimate
  • Smooth noise
  • Separate simulation from measurement

Before you start

Arithmetic and Python lists

Identify what is measured

An ADC converts an analog input to a digital count. Converting that to temperature or another physical quantity is a separate step. This lesson assumes an ideal 12-bit range from 0 to 4095 and a 3.3 V reference to explain arithmetic. Actual ESP32 behavior depends on the chip, attenuation and calibration; these assumptions are not a hardware specification.

Consult the exact board and sensor documentation before connecting anything. Do not connect a higher-voltage output because a formula happens to mention 3.3 V. This lab makes no electrical connection and reports no firsthand measurements.

Convert then calibrate

The ideal model is voltage = raw / 4095 * 3.3. Reject out-of-range readings rather than quietly producing impossible voltages. A sensor conversion may then apply an offset and slope derived from reliable calibration evidence.

Noise can arise from grounding, power supply variation, aliasing or the sensor. An average can hide an electrical problem. Keep raw samples available while deciding whether a filter is appropriate.

Smooth with a defined window

A moving average uses the most recent n readings. A larger window can steady the display but delay its response. Threshold alerts may need hysteresis: different thresholds for entering and leaving an alert prevent noisy values from chattering around a boundary.

The exercise estimates the mean voltage of a controlled sample list. It is not a hardware driver and cannot flash an ESP32. Empty input returns None because there is no observation to estimate.

Run a controlled experiment

All-zero input should return 0 V, and all-maximum input should return 3.3 V within this ideal model. Floating-point checks use a tolerance rather than exact decimal equality.

Compare [0,4095] with [2048,2048]. Their means are similar but their variation is different. A single average cannot explain every sensor issue. Label any future measurements with actual hardware and conditions rather than presenting this simulation as experimental evidence.

Check a controlled calculation

For samples [0, 4095], the mean count is 2047.5. Under the ideal assumptions above, 2047.5 / 4095 * 3.3 gives 1.65 volts. Repeated maximum values give 3.3 volts; [0, 0] gives 0.0. An empty list returns None, distinguishing no observation from a measured zero.

Implement the exercise by returning None for an empty list, then sum(samples) / len(samples) / 4095 * 3.3 otherwise. Dividing by the number of samples twice understates the result. As an extension, reject out-of-range counts and make the reference voltage a named input. Calibration and noise smoothing solve different problems: averaging cannot repair a biased reference. These outputs are calculations, not physical measurements.

Try it yourself

def average_voltage(samples):
    return None

print(average_voltage([0, 4095]))

Enable JavaScript for this interactive activity. You can read all lesson explanations above without it.

Continue exploring