What it shows
The Arduino turns the distance of your hand into the frequency of a tone, so you can play music in the air. It shows how a sensor reading can be mapped to a sound.
The science
Sound is vibrations, and pitch is the frequency of vibration. The sketch measures the distance of your hand from 5 to 35 cm and maps it to a frequency from 1000 Hz to 200 Hz, so nearer is higher. The tone() function makes a square wave of that frequency on a pin. Snapping the frequency to the nearest note of a scale, such as C major, makes the instrument easy to play in tune.
Materials
Cost: under Rs 1,500. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- An Arduino Uno or compatible board
- An HC-SR04 ultrasonic sensor
- A small speaker (8 Ω) with a 100 Ω resistor in series, or a piezo buzzer
- A push button to switch the scale
- A breadboard and jumper wires
- A card tube to guide the sound
Safety
For this project: USB power only. Keep the sound level low for your ears.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Connect the sensor VCC to 5 V, GND to GND, TRIG to pin 9 and ECHO to pin 10.
Connect the speaker in series with the 100 Ω resistor between pin 8 and GND.
Write a function to read the distance in centimetres and smooth it by averaging three readings.
Map distances of 5 to 35 cm to frequencies from 1000 to 200 Hz with the map() function.
Play the tone with tone(8, frequency) and call noTone when the hand is beyond 40 cm.
Move your hand towards and away from the sensor and listen to the pitch change.
Add an array of eight note frequencies and choose the nearest to give the smooth sweep a clean scale.
Add a button that switches between the smooth sweep and the scale, and play a simple tune.
What you should see
The pitch rises smoothly as the hand nears the sensor, and in scale mode it jumps cleanly between notes.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What does pitch depend on?
The frequency of the sound vibration.
2What does tone() output?
A square wave of the chosen frequency on the pin.
3Why average distance readings?
Single readings are noisy and cause the note to jump.
4How does this differ from a real theremin?
A real theremin senses the capacitance of your hand with radio-frequency oscillators, not sound echoes.
Take it further
Add a second sensor to control the volume with another hand using a digital potentiometer or PWM.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Find out more about electronics and robotics
Free, well-known places to explore the subject further. Ask a grown-up before you sign up for anything or type your name anywhere.
