Supplier photo of an assembled infinity mirror light cube illuminated on a dark background.
Supplier photo
Temu affiliate offer · Project idea

Build an infinity light cube

Listing specifies 5-volt direct current. Soldering required; battery and glue not included.

View kit on Temu

Tricky Circuit may earn a commission through these links. Product ideas have not been bench-tested by Tricky Circuit and are not verified lesson parts lists.

A Noisy Knob: Compare a Moving Average with a Median

A knob can make a number shake even when your hand is still. Software filtering can steady the display, but it also changes how quickly the system follows a real movement. This experiment shows raw data, a five-sample moving average and a five-sample median side by side so you can choose based on the type of error you actually have.

The small analog circuit

A potentiometer has a movable contact called a wiper. Its two outer terminals connect across 3.3 V and ground; the wiper selects a voltage between them. The ADC, or analog-to-digital converter, measures that voltage on GP26. GPIO means general-purpose input/output; GP26 is the board’s signal name, while its physical header position is 31.

With a 10 kΩ total track, the current between supply rails is approximately 3.3/10,000 = 0.33 mA. The midpoint’s ideal source resistance is at most 2.5 kΩ, because both halves of the track contribute in parallel. Start with short wires and a firm ground connection before trying to solve every instability in software.

Parts to gather

  • Original Raspberry Pi Pico with MicroPython 1.26
  • One 10 kΩ linear potentiometer, breadboard and wires
  • USB cable and serial console or serial plotting tool
A Noisy Knob: Compare a Moving Average with a Median. Potentiometer outer terminal → Pico 3V3, physical pin 36 Wiper, normally centre terminal → GP26, physical pin 31 All analog wiring remains between 0 and 3.3 V
Original Tricky Circuit connection diagram. Nominal values are calculated; this build has not been physically tested.

Complete connections

ConnectionPurpose
Potentiometer outer terminal → Pico 3V3, physical pin 36Other outer terminal → GND, pin 38.
Wiper, normally centre terminal → GP26, physical pin 31Confirm terminal behavior with resistance measurements before powering.
All analog wiring remains between 0 and 3.3 VDo not introduce external spikes electrically; demo spikes are software values.

Two different questions

The mean asks for the arithmetic average of recent samples. Every value contributes, including one very large outlier. The median sorts a group and selects its middle value. With a five-sample window, one or two isolated outliers can leave the median close to the remaining ordinary readings.

Take 30,000, 30,000, 65,535, 30,000 and 30,000. Their mean is 37,107, while the median is 30,000. That does not make median universally better. If an important real event is a short narrow pulse, the median may erase exactly what you wanted to detect.

Complete MicroPython program

"""Pico: 10k potentiometer between 3V3 and GND, wiper to GP26."""
class Filters:
    def __init__(self):
        self.window = []
    def update(self, raw):
        self.window.append(raw)
        if len(self.window) > 5:
            self.window.pop(0)
        n = len(self.window)
        return sum(self.window) / n, sorted(self.window)[n // 2]

def main():
    from machine import ADC, Pin
    from time import sleep_ms
    adc = ADC(Pin(26)); filters = Filters(); index = 0
    INJECT_DEMO_SPIKES = False
    while True:
        measured = adc.read_u16()
        sample = 65535 if INJECT_DEMO_SPIKES and index % 100 == 99 else measured
        mean, median = filters.update(sample)
        print("%d,%d,%.1f,%d" % (measured, sample, mean, median))
        index += 1
        sleep_ms(20)

if __name__ == "__main__":
    main()

Run the comparison

  1. Wire with power off, reconnect and save the program as main.py. Leave INJECT_DEMO_SPIKES set to False.
  2. The console emits four columns: measured, sample, mean and median. Initially measured and sample are identical.
  3. Hold the knob still at several positions. Compare spread rather than expecting an exactly constant least significant digit.
  4. Turn the knob abruptly and watch how the two filtered values catch up.
  5. For a controlled demonstration, set INJECT_DEMO_SPIKES to True and restart. Every hundredth sample is replaced with 65,535 in software. The original measured column remains unchanged so the artificial event cannot be mistaken for captured electrical noise.

Quantify the delay

The intended interval is approximately 20 ms plus execution and console overhead. Five samples span roughly 80 ms from first to last; they arrive across five sampling moments, not five full gaps. Following a sustained step, a full-window median switches once the new level forms a majority, which takes three new samples. The mean moves in fractions of one fifth and reaches the new level after five new samples.

At startup the window is not yet full. The code averages only the available samples and chooses the upper middle for an even-sized partial window. After five readings it uses the intended odd-sized window. This prevents invented zero samples from dragging the initial display down.

Do not hide a wiring problem

A disconnected wiper can produce a floating input and plausible-looking filtered numbers. Neither filter proves a sensor is present. A poor connector that occasionally jumps to a rail deserves a hardware investigation even if the median makes its display attractive. Keep raw data available during commissioning.

The program’s counter controls only the labeled software demo; it is not evidence of measured interference. Host tests use a constant input, a single injected spike and a sustained step to check the actual Filters class. Physical noise, timing jitter and converter accuracy still need a bench measurement.

Challenge: what does a five-sample median do to two isolated high spikes in one window?

If the other three values remain near the baseline, the middle sorted value is still a baseline reading. With three high samples, the majority changes and so does the median. This explains both spike rejection and delayed step response.

Sources and related learning

Explore the Tricky Circuit interactive lessons. This article presents a design and software checks; no physical build results are claimed.

📡Broadcast the signal — amplify the connection.

Leave a Reply

Temu affiliate offers

Find your next electronics project

Explore these do-it-yourself project kits. Check the selected option, included parts and instructions on each offer.

Tricky Circuit may earn a commission if you buy through these Temu affiliate links. These are product ideas; they have not been bench-tested by Tricky Circuit and are not verified lesson parts lists.

Supplier photo of a breadboard, jumper wires and assorted electronic components in a plastic storage case.

Supplier product photo

Try breadboard experiments

Breadboard, wires and assorted parts. Arduino controller board not included.

View kit on Temu

Supplier photo of an assembled blue surface-mount soldering practice board with a circular ring of small lights.

Supplier product photo

Solder a chasing-light board

Surface-mount soldering practice. Battery and battery holder not included.

View kit on Temu

Supplier photo of an assembled two-wheeled line-following car with a blue circuit board, two motors and an empty battery holder.

Supplier product photo

Build a line-following car

Soldering and assembly required. Check battery and track requirements before buying.

View kit on Temu