Skip to content
International Shipping
Wish Lists Cart
0 items

#23 Air Piano Project With PicoBricks

02 Nov 2023
#23 Air Piano Project With PicoBricks

With the development of electronic technology, musical instruments that are difficult to produce, expensive and producing high-quality sound have been digitized. Pianos are one of these instruments. Each key of digital pianos produces electrical signals at a different frequency. Thus, it can play 88 different notes from its speakers. Factors such as the delay time of the keys of digital instruments, the quality of the speaker, the resolution of the sound have appeared as the factors affecting the quality. In electric guitars, vibrations in strings are digitized instead of keys. On the other hand, In wind instruments, the notes played can be converted into electrical signals and recorded thanks to the high-resolution microphones plugged into the sound output. This development in electronic technology has facilitated access to high-cost musical instruments, music education has gained a wider variety and spread to a wider audience.

In this project we will make a simple piano that can play 7 notes with PicoBricks. The speaker of this piano will be the buzzer. The ultrasonic sensor will act as the keys of the piano.

Details and Algorithm

In this project, we will make a piano application using the HC-SR04 Ultrasonic distance sensor and the buzzer module on PicoBricks. We will make the buzzer play different notes according to the values ​​coming from the distance sensor, and we will create melodies by moving our hand closer to the sensor and away from it. In addition, we will instantly print the distance and played note information on the OLED screen.


1X PicoBricks
1X HC-SR04 Ultrasonic Sensor
Jumper Cables
Easy Connection Cables

Wiring Diagram

You can code and run Picobricks’ modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.

Construction Stages of the Project

MicroBlocks Codes of the PicoBricks

You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.

Microblocks Run TabI

MicroPython Codes of the PicoBricks


from machine import Pin, PWM, I2C
from utime import sleep
import utime
from picobricks import SSD1306_I2C
import _thread

# Define the libraries
buzzer = PWM(Pin(20, Pin.OUT))
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)

# Define the input and Output pins
WIDTH = 128

# OLED screen settings
sda = machine.Pin(4)
scl = machine.Pin(5)
i2c = machine.I2C(0, sda=sda, scl=scl, freq=1000000)

# Initialize digital pin 4 and 5 as an OUTPUT for OLED communication
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
measure = 0

def getDistance():
    while echo.value() == 0:
        signaloff = utime.ticks_us()
    while echo.value() == 1:
        signalon = utime.ticks_us()
    timepassed = signalon - signaloff
    distance = (timepassed * 0.0343) / 2
    return distance

def airPiano():
    global measure
    while True:
        if 5 < measure < 11:
        elif 10 < measure < 16:
        # ... [Kodun geri kalanı bu şekilde devam ediyor]

# Play the tone determined by the value of the distance sensor
while True:
    measure = int(getDistance())
    oled.text("Distance " + str(measure) + " cm", 5, 30)

Arduino C Codes of the PicoBricks


#include <Wire.h>
#include "ACROBOTIC_SSD1306.h"
#include <NewPing.h>
#define TRIGGER_PIN 15
#define ECHO_PIN 14
#define MAX_DISTANCE 400
#define T_C 262
#define T_D 294
#define T_E 330
#define T_F 349
#define T_G 392
#define T_A 440
#define T_B 493
const int Buzzer = 20;

void setup() {
    #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)

void loop() {
    int distance = sonar.ping_cm();
    if(distance > 5 & distance < 11) {
        tone(Buzzer, T_C);
    // ... [Kodun geri kalanı bu şekilde devam ediyor]
    oled.setTextXY(2, 4);
    oled.putString("Distance: ");
    oled.setTextXY(4, 6);
    String string_distance = String(distance);
    oled.setTextXY(4, 8);

GitHub Page
Prev Post
Next Post

Thanks for subscribing!

This email has been registered!

Shop the look
Choose Options

Edit Option

Have Questions?

Back In Stock Notification


Product SKUDescription Collection Availability Product Type Other Details

Terms & Conditions

What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
this is just a warning
Shopping Cart
0 items