Build Fun Piano with Ultrasonic using Maker Nano RP2040

This tutorial shows how to make a piano using an ultrasonic sensor and Maker Nano RP2040. The project is super fun and very simple to make! 🎹🎵

Video Tutorial

How Does it Work?

how does it work

Components Required

Connection

ultrasonic piano circuit diagram

Code

#define echoPin 12 
#define trigPin 13
#define led1 14 
#define led2 15
#define buzzer 22 // buzzer pin in Maker Nano RP2040 is 22

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement


void setup() {
  Serial.begin(9600); 
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
}

void loop() {
// Ultrasonic Code 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");


  if (distance < 5){
  tone(buzzer, 523); // Play tone C 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else if (distance < 10){
  tone(buzzer, 587); // Play tone D 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else if (distance < 15){
  tone(buzzer, 659); // Play tone E 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else if (distance < 20){
  tone(buzzer, 698); // Play tone F 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else if (distance < 25){
  tone(buzzer, 783); // Play tone G 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else if (distance < 30){
  tone(buzzer, 880); // Play tone A 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else if (distance < 35){
  tone(buzzer, 987); // Play tone B 
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH); 
  delay(500); 
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  delay(500);   
  }

  else {
  noTone(buzzer);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  }
}

Project Complete

screenshot
Upon completing the project successfully, the buzzer will beep in a specific tone based on the distance between the ultrasonic and the object

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

5 Best Plug-ins for Cura
Common Linux Terminal Commands
WIFI Robot Maneuver With MDDS10
I2C Communication between micro:bit and Arduino
Quadruped Robot with SC08A Servo Controller
Tutorials of Cytron Technologies Scroll to Top