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.

Distinguish estimates from measurements

The exercise uses an idealized full-scale reference of 3.3 volts and a twelve-bit maximum count of 4095. Real ESP32 variants, attenuation settings and ADC calibration change the relationship. A moving average reduces some noise but cannot correct systematic bias. Check the board and sensor documentation before connecting hardware, and never infer electrical compatibility from this mathematical example.

Change the sample list from [0, 4095] to repeated maximum values and explain why the estimate changes from 1.65 to 3.3. Then test an empty list: no observation must remain distinct from a genuine zero reading. If you extend the function, reject counts outside the chosen ADC range and make the reference voltage a named calibration input. No physical sensor readings were taken for this lesson.

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