Smoothing example script (arduino.py) Prints a running average of the last 10 readings of A0, which steadies a noisy signal. Each new reading replaces the oldest one. For averaging done by the board itself, see Analog Averaging.
- Wiring
- A potentiometer wiper or sensor on A0 (0-3.3 V).
from arduino import *
NUM_READINGS = 10
readings = [0] * NUM_READINGS
index = 0
total = 0
def setup():
pass
def loop():
global index, total
total -= readings[index]
readings[index] = analogRead(A0)
total += readings[index]
index = (index + 1) % NUM_READINGS
print(total // NUM_READINGS)
delay(1)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()