Fruit Piano
What you'll build¶
A three-key piano. Touch the fruit on pin 1 for C, the fruit on pin 2 for E, and the gold touch logo for G. The note's letter appears on the display while it plays.
Wiring¶
| Part | Part pin | micro:bit pin |
|---|---|---|
| Fruit 1 (key C) | clip | P1 |
| Fruit 2 (key E) | clip | P2 |
| You (other hand) | hold | GND |
The touch logo on the front needs no wiring.
Code¶
How it works¶
pin1.is_touched()works by noticing a tiny current flowing from the pin, through you, toGND. That's why you hold the GND clip, and why anything that conducts, like fruit, plants or water, can be a key.pin_logo.is_touched()senses your finger capacitively, so it needs no GND connection.- The
KEYSlist pairs each touch input with a note, so adding a key is one extra line.note[0]is the note's first letter, which is shown while it plays. music.play("C4:2")plays a short note (250 ms), so holding a key repeats it.
Why not pin 0?
The music module also sends sound out of pin 0, which would interfere with using it as a key.
Try this next¶
- Add more keys with an edge connector breakout: other pins can be touch keys too.
- Play a chord when two fruits are touched together.
- Record what you play into a list and replay it with button A.