Tone Pitch Follower example script (arduino.py) Plays a pitch on a piezo buzzer on D8 that follows the voltage on A0, from 120 Hz at 0 V to 1500 Hz at 3.3 V, and prints the reading and the pitch.
- Wiring
- A piezo buzzer between D8 and GND, and a potentiometer wiper (ends on GND and 3.3 V) or a light sensor divider on A0.
- Uses
- analogRead(), map(), tone()
from arduino import *
BUZZER_PIN = 8
LOW_HZ = 120
HIGH_HZ = 1500
def setup():
pass
def loop():
reading = analogRead(A0)
pitch = map(reading, 0, 1023, LOW_HZ, HIGH_HZ)
print(reading, pitch)
tone(BUZZER_PIN, pitch)
delay(10)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()