Tone Generator with a Potentiometer
Welcome to the Tone Generator Lab
In this lab, you will turn a knob to change the pitch of a tone. Twist the potentiometer and hear the sound change in real time!
Setup
Wire up your components like this:
- Connect the middle pin (wiper) of the potentiometer to the ADC0 pin — that is GPIO pin 26 on the Pico.
- Connect one outer pin of the potentiometer to GND.
- Connect the other outer pin to 3.3VREF — use the isolated GND pin next to it.
- Connect the speaker between GND and GPIO pin 15 (lower-left corner of the Pico).
Then load and run the code below.
Tone Generator Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Code Explanation
pot = machine.ADC(26)— sets up pin 26 as an Analog-to-Digital Converter (ADC) input to read the potentiometer voltage.speaker_pwm = machine.PWM(speaker_pin)— creates a Pulse-Width Modulation (PWM) signal on pin 15 for the speaker.speaker_pwm.duty_u16(32768)— turns the sound on at 50% duty cycle. This stays on the whole time; only the frequency changes.pot_value = pot.read_u16()— reads the potentiometer. The value goes from 0 (fully turned one way) to 65535 (fully turned the other way).frequency = MIN_FREQ + (pot_value / 65535) * (MAX_FREQ - MIN_FREQ)— converts the potentiometer reading into a frequency between 100 Hz and 2,000 Hz.speaker_pwm.freq(int(frequency))— sends the new frequency to the PWM output. Theint()call rounds it to a whole number.time.sleep(0.01)— waits 10 milliseconds. Without this delay, the program would update the frequency thousands of times per second, which is not useful.speaker_pwm.deinit()— turns the PWM hardware off when you stop the program. Without this, the tone would keep playing!
Key Idea
The try/except KeyboardInterrupt block is important! When you press Ctrl-C to stop the program, the except block runs deinit() to stop the PWM. Without it, the tone would keep playing after your program stops.
Monty's Tip
When you turn the potentiometer, the ADC reads the changing voltage on pin 26 and converts it to a number. Twisting the knob changes the number, which changes the frequency, which changes the pitch you hear.
Challenges
- Change
MAX_FREQto4000. Can you hear the full range? - What is the highest frequency you can still hear? (Hint: most people cannot hear above 20,000 Hz, but many already start losing high frequencies much earlier.)
- Connect an OLED display and draw a picture of a sine wave that changes shape as the frequency changes.
Great Work!
You just built a real electronic theremin-style instrument! You control the pitch with your fingers on a knob.