Pico W light theremin: light sets the pitch
MicroPython on a Pico W: a photoresistor on GP28 learns its range for five seconds, then the light level sets a passive buzzer’s pitch on GP15. The whole build, a Raspberry Pi Pico W and 7 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.85. On Mokxi they're free.
What you'll learn
- Calibrating a sensor to its own range
- Reading an analog light sensor with an ADC
- Mapping a reading to a sound frequency
- Switching a speaker with a transistor
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Raspberry Pi Pico W
- Parts: 10
How it works
A light sensor and a 10k resistor form a divider read on GP28. At start, the LED on GP16 lights for five seconds. Wave your hand over the sensor in that time so the program learns the darkest and brightest readings it sees.
After five seconds the LED goes out. If the readings barely changed, the program falls back to the full range. It prints the learned range.
Then the light level sets the pitch of the speaker on GP15, from 50 Hz up to 6000 Hz. A transistor with a 1k resistor on its base switches the speaker. The pitch prints every 100 ms.
Parts list
10 parts, plus the jumper wires. They retail for about $6.85; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Light sensor
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 2 × Power, 3.3 V
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 1k Ω pin 1 | Raspberry Pi Pico W pin GP15 |
| Resistor, 10k Ω pin 2 | Raspberry Pi Pico W pin GP28 |
| Light sensor pin 1 | Raspberry Pi Pico W pin GP28 |
| Resistor, 220 Ω pin 1 | Raspberry Pi Pico W pin GP16 |
| Piezo speaker pin 1 | 3.3 V |
| Transistor (NPN or PNP) pin C | Piezo speaker pin 2 |
| Resistor, 1k Ω pin 2 | Transistor (NPN or PNP) pin B |
| Transistor (NPN or PNP) pin E | Ground |
| Resistor, 10k Ω pin 1 | 3.3 V |
| Light sensor pin 2 | Ground |
| LED, red pin A | Resistor, 220 Ω pin 2 |
| LED, red pin C | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Light theremin: for five seconds, with the LED on GP16 lit, wave your
# hand over the photoresistor (divider on GP28) so it learns the darkest
# and brightest light. Then the light sets the buzzer's pitch on GP15.
from machine import ADC, Pin, PWM
import time
light = ADC(28)
led = Pin(16, Pin.OUT)
buzzer = PWM(Pin(15))
buzzer.duty_u16(0)
led.value(1)
low, high = 65535, 0
began = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), began) < 5000:
v = light.read_u16()
low, high = min(low, v), max(high, v)
time.sleep_ms(20)
led.value(0)
if high - low < 1000: # nothing moved: use the whole range
low, high = 0, 65535
print("calibrated", low, high)
while True:
v = min(max(light.read_u16(), low), high)
pitch = 50 + (v - low) * (6000 - 50) // (high - low)
buzzer.freq(pitch)
buzzer.duty_u16(30000)
print("pitch", pitch)
time.sleep_ms(100)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The pitch hardly changes. This project
Move your hand over the sensor during the five-second start, from very dark to bright, so the range is wide. - There is no sound. This project
Check the transistor: collector to the speaker, emitter to ground, base to GP15 through the 1k resistor. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds. - The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong. - The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.