Skip to content

Fruit Piano

Level Beginner
Board V2 only
Parts micro:bit V2 · 2 pieces of fruit (bananas, apples...) · 3 crocodile clip leads

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
Fruit 1 (key C)clipFruit 2 (key E)clipYou (other hand)holdmicro:bitP1P2GND

The touch logo on the front needs no wiring.

Code

15 fruit-piano.py
'''
Example: Fruit piano with touch pins and the touch logo in micro:bit V2
By: Meqdad Darwish
'''
# Note: clip pin1 and pin2 to two pieces of fruit, and hold the GND pad
# (or a clip from GND) with your other hand while you touch them.

from microbit import *
import music

KEYS = [
    (pin1, "C4:2"),
    (pin2, "E4:2"),
    (pin_logo, "G4:2"),
]

while True:
    for key, note in KEYS:
        if key.is_touched():
            display.show(note[0])  # the note's letter
            music.play(note)
            display.clear()
    sleep(10)

How it works

  • pin1.is_touched() works by noticing a tiny current flowing from the pin, through you, to GND. 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 KEYS list 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.