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.
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)