Skip to main content
Ultrasound Music Machine with Arduino Nano 33 BLE
Blog Post

Ultrasound Music Machine with Arduino Nano 33 BLE

I make a musical instrument with a speaker & ultrasound sensor

JE
jeremybobbin
@jeremybobbin·Apr 20, 2026· 1 min read·audio
2 views
Ultrasound Music Machine with Arduino Nano 33 BLE

This is a very simple project. I wanted to be able to move my hand to change the pitch. Here is the project in its physical form:

complete

This is how we setup & calculate the distance from the ultrasound sensor:

c
void setup() {
	pinMode(D2, OUTPUT); // trigger
	pinMode(D3, INPUT);  // echo
	Serial.begin(9600);
}

void loop() {
	digitalWrite(D2, LOW);
	delayMicroseconds(2);
	digitalWrite(D2, HIGH);
	delayMicroseconds(10);
	digitalWrite(D2, LOW);

	duration = pulseIn(D3, HIGH);
	distance = (duration*.0343)/2;
}

And here are the values for the C-major scale which we can pass to tone:

c
int notes[] = {262, 294, 330, 349, 392, 440, 494, 523};

Here is the code for the whole project:

c
#define LENGTH(x)  ((int)(sizeof (x) / sizeof *(x)))

int notes[] = {262, 294, 330, 349, 392, 440, 494, 523};
float duration, distance;
int n, m;


void setup() {
	pinMode(D2, OUTPUT); // trigger
	pinMode(D3, INPUT);  // echo
	Serial.begin(9600);
}

void loop() {
	digitalWrite(D2, LOW);
	delayMicroseconds(2);
	digitalWrite(D2, HIGH);
	delayMicroseconds(10);
	digitalWrite(D2, LOW);

	duration = pulseIn(D3, HIGH);
	distance = (duration*.0343)/2;

	tone(D4, notes[((int)(distance/4))%LENGTH(notes)]);
	char buf[128];
	sprintf(buf, "%0.02f %d\n", distance, ((int)(distance/4))%LENGTH(notes));
	Serial.print(buf);
	delay(100);
}

And here are the results!

YouTube
JE
Written by
jeremybobbin
@jeremybobbin
Related Posts

Comments

No comments yet. Be the first!