Pico W fruit piano: MPR121, buzzer and RGB LED
MicroPython on a Pico W: each of the twelve MPR121 electrodes plays its own note on a passive buzzer while an RGB LED flashes a random color. The whole build, a Raspberry Pi Pico W and 8 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.
Advanced About 35 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $9.95. On Mokxi they're free.
What you'll learn
- Capacitive touch sensing with an MPR121
- Playing notes by changing a PWM frequency
- Mixing color with three PWM channels
- A list used as a lookup table
- Difficulty: Advanced
- Time: About 35 minutes
- Board: Raspberry Pi Pico W
- Parts: 11
How it works
An MPR121 touch controller on I2C (SDA GP6, SCL GP7) senses twelve electrodes. Its helper module sets the touch threshold to 12 and the release threshold to 6, then lets the program ask which pads are touched.
When a pad is touched, the program looks up a note for the first touched pad in NOTES, from 220 Hz (A3) to 659 Hz (E5). It sets the buzzer's PWM frequency to that note and plays it. The buzzer on GP15 is switched by a transistor. The RGB LED flashes a random color, then everything goes quiet.
Only the first pad touched plays. Each note holds for half a second.
Parts list
11 parts, plus the jumper wires. They retail for about $9.95; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Capacitive touch, MPR121
- 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 × RGB LEDIn Starter Projects Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Power, 3.3 V
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 |
|---|---|
| Capacitive touch, MPR121 pin SDA | Raspberry Pi Pico W pin GP6 |
| Capacitive touch, MPR121 pin SCL | Raspberry Pi Pico W pin GP7 |
| Resistor, 220 Ω (2) pin 2 | Raspberry Pi Pico W pin GP11 |
| Resistor, 220 Ω (1) pin 2 | Raspberry Pi Pico W pin GP12 |
| Resistor, 330 Ω pin 2 | Raspberry Pi Pico W pin GP13 |
| Resistor, 1k Ω pin 1 | Raspberry Pi Pico W pin GP15 |
| Capacitive touch, MPR121 pin VIN | 3.3 V |
| Capacitive touch, MPR121 pin GND | Ground |
| 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, 330 Ω pin 1 | RGB LED pin R |
| RGB LED pin C | Ground |
| Resistor, 220 Ω (1) pin 1 | RGB LED pin G |
| Resistor, 220 Ω (2) pin 1 | RGB LED pin B |
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.
# Fruit piano: each of the MPR121's twelve electrodes plays its own note
# on the buzzer (GP15), and the RGB LED (GP13, GP12, GP11) flashes a
# random color while it sounds.
from machine import I2C, Pin, PWM
from mpr121 import MPR121
import random
import time
NOTES = [220, 247, 262, 294, 330, 349, 392, 440, 494, 523, 587, 659] # A3 to E5
touch = MPR121(I2C(1, sda=Pin(6), scl=Pin(7), freq=400000))
buzzer = PWM(Pin(15))
buzzer.duty_u16(0)
rgb = [PWM(Pin(n)) for n in (13, 12, 11)]
for ch in rgb:
ch.freq(1000)
def light(r, g, b):
for ch, v in zip(rgb, (r, g, b)):
ch.duty_u16(v)
while True:
pads = touch.pads()
if pads:
hz = NOTES[pads[0]]
buzzer.freq(hz)
buzzer.duty_u16(30000)
light(random.randint(0, 65535), random.randint(0, 65535), random.randint(0, 65535))
print("pad", pads[0], hz, "Hz")
time.sleep_ms(500)
buzzer.duty_u16(0)
light(0, 0, 0)
time.sleep_ms(20)
# mpr121.py: the MPR121 capacitive touch controller, the few registers
# a touch keyboard needs. touched() returns a 12-bit mask: electrode 0
# is bit 0.
import time
class MPR121:
def __init__(self, i2c, addr=0x5A):
self.i2c = i2c
self.addr = addr
self._set(0x80, 0x63) # soft reset
time.sleep_ms(1)
self._set(0x5E, 0x00) # stop, so the setup can be written
for e in range(12):
self._set(0x41 + 2 * e, 12) # touch threshold
self._set(0x42 + 2 * e, 6) # release threshold
self._set(0x5D, 0x04) # sample timing
self._set(0x5E, 0x8C) # run all 12 electrodes
def _set(self, reg, value):
self.i2c.writeto_mem(self.addr, reg, bytes([value]))
def touched(self):
low, high = self.i2c.readfrom_mem(self.addr, 0x00, 2)
return (low | high << 8) & 0x0FFF
def pads(self):
mask = self.touched()
return [e for e in range(12) if mask & (1 << e)]
Code walkthrough
The key functions of the sketch, and what each one does.
light main.py
light takes three numbers from 0 to 65535, for red, green and blue. It writes each one as the duty of its PWM channel, so bigger means brighter. The main loop sends random numbers while a note plays and 0, 0, 0 to turn the LED off.
def light(r, g, b):
for ch, v in zip(rgb, (r, g, b)):
ch.duty_u16(v)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- There is no sound. This project
The buzzer sits behind the transistor, driven from GP15 through the 1k resistor. Check the base, collector and emitter. - The notes seem to be on the wrong pads. This project
Pads count from 0 on the MPR121's electrode 0. Pad 0 plays 220 Hz. - The colors look wrong. This project
The red leg goes to GP13, green to GP12 and blue to GP11. - The sensor is not found on I2C. Capacitive touch
The default address is 0x5A, and the ADDR pin can change it to 0x5B, 0x5C or 0x5D. Check SDA, SCL, 3.3 V power and a shared ground. Run an I2C scanner to confirm. - Touches register randomly or never. Capacitive touch
Keep electrode wires short and away from other wires. The chip sets its baseline at start up, so do not touch the pads while it powers up. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.