Analog Averaging example script (phyduino.py) Shows how averaging steadies a reading. For each setting, takes 100 readings of A0 and A3 and prints the lowest, the highest and the spread between them (in ADC steps), then waits 5 seconds and starts again.
analog_averaging() applies to every analogue pin: A3-A5 average in the external ADC itself, A0-A2 take the samples one after another.
- Wiring
- Connect a steady voltage, e.g. a potentiometer wiper, to A0 (0-3.3 V) and A3 (0-2.048 V), or leave them as they are.
- Output
averaging 1 A0 lowest 2040 highest 2056 spread 16
averaging 1 A3 lowest 2046 highest 2050 spread 4
...
averaging 32 A0 lowest 2047 highest 2049 spread 2
import time
import phyduino as mcu
READINGS = 100
def spread(name):
"""Takes READINGS readings of one pin; returns the lowest, the highest and the gap."""
values = [mcu.analog_read(name) for _ in range(READINGS)]
return min(values), max(values), max(values) - min(values)
while True:
for samples in (1, 4, 8, 16, 32):
mcu.analog_averaging(samples)
for name in ("A0", "A3"):
low, high, width = spread(name)
print("averaging %2d %s lowest %4d highest %4d spread %3d" % (samples, name, low, high, width))
mcu.analog_averaging(1)
print()
time.sleep(5)