Tone Pitch Follower example script (phyduino.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
- pwm(), analog_read()
import time
import phyduino as mcu
LOW_HZ = 120
HIGH_HZ = 1500
buzzer = mcu.pwm("D8", duty_u16=32768)
while True:
reading = mcu.analog_read("A0")
pitch = LOW_HZ + reading * (HIGH_HZ - LOW_HZ) // 4095
print(reading, pitch)
buzzer.freq(pitch)
time.sleep_ms(10)