Raspberry Pi Pico
Waveshare Piano For micro:bit
This Waveshare micro:bit accessory has 13 touch pads, some Neopixels (not used here) and a buzzer.
You need to connect the 3V and GND pins to power pins on the Pico. You also need to connect the I2C pins (19 SCL, 20 SDA) from the micro:bit to two of the many I2C pins of the Pico. I connected 19 to GP17 and 20 to GP16 for my test circuit. You also need to connect pin0 from the micro:bit to a GPIO on the Pico. I connected it to GP15.This is a short library for reading the touch pads of the piano - save as piano.py.
class Piano(): def __init__(self, i2c): self.last = 0 self.i2c = i2c def read(self): arr = self.i2c.readfrom(0x57,2) return arr[0] + arr[1]*256 def read_left(self): r = self.read() d = list(reversed([r >> i & 1 for i in range(12,-1,-1)])) if r>0: return d[0:13].index(1) else: return -1
Here is the test code for playing the piano. It requires the music library from the Passive Buzzer page on this site.
from machine import Pin, I2C, PWM from piano import Piano from time import sleep from music import note_on, note_off i2c = I2C(0,sda=Pin(16), scl=Pin(17)) p = Piano(i2c) buzz = PWM(Pin(15)) notes = [523,554,587,622,659,698,740,784, 831,880,932,988,1047] last = -1 while True: reading = p.read_left() if reading == -1: note_off(buzz) elif reading != last: note_on(buzz, notes[reading]) last = reading sleep(0.01)