Skip to content
/ rangiano Public

Play melody by changing distance. Arduino project


Notifications You must be signed in to change notification settings


Folders and files

Last commit message
Last commit date

Latest commit



15 Commits

Repository files navigation


Arduino based instrument for playing melodies by changing distance

Demo video:
thumbnail link to YouTube

Used components

  1. Arduino UNO Rev3
  2. Seeedstudio Grove ultrasonic ranger v2.0
  3. Seeedstudio Grove speaker v1.1

rangiano wiring

Wiring scheme with links to components and assembled project at DESCRiBE


Works out of the box

  • Clone project: git clone or copy code to your sketch

  • Connect sensor and speaker to pins 12 and 3 accordingly (or set yours in rangiano.ino)

To customize

In melody.h:

  • Choose or write melody following this pattern:
[ note + number of octave + _N ] ==> C4_N == piano middle C

In rangiano.ino:

  • Set working range in centimetres:
#define FAR 40
#define MINDIST 5  
  • Set direction of playing (default: played by approaching sensor):
// #define GOAWAY true   // to play by moving away from sensor
  • Set method of playing (default: using tone() method):
// #define GENERATE true  // to play by digitalWrite() and delay(): with pause, but more realiable

How it works

Our goal is to play melody note by note:

  • Melody is set as an array of notes in melody.h Notes are defined by respective frequencies in notes_frequencies.h:
const float notes[] = { C4_N, D4_N, E4_N, F4_N, G4_N, A4_N, B4_N, C5_N };
  • Note in melody is accessed by its index in array (from 0 to N),
  • We divide working range into steps — subranges by number of notes in melody,
  • Then play note corresponding to the index of step of current location


Ranger measures distance from 3 to 350 cm ==> cm
Then we check if it is in the working range.

               |<-------- working range -------->|
               |                                 |
  |           NEAR          |                   FAR          |
  |                         |                                |
  3 cm                      cm                              350 cm

If object is in range, we find index of current step (depending on direction of playing).
By default we play by approaching sensor. So divide distance from FAR to cm by noteStep, e.g.:

cm =        124
FAR =       170
noteStep =  25

int index = (170 - 124)/25 ==> 1.84 ==> 1  // not rounding

        NEAR       |    FAR
         |         |     |
steps:   | 3 | 2 | 1 | 0 |
notes:   | C | D | E | F |

Then pass index to player method, either:

  • one using builtin tone() method, or
  • one generating sound.

Known issues

  • tone() somehow disturbs sensor at about 150 cm
  • not yet sure if it's possible to use generateSound() method without pause for measure


Will be great if you add features or fix something (e.g. this readme) by making a pull request or raising an issue.


This project is made for educational purposes.
Be careful working with electricity.
Code provided as is and no warranty applied — so be careful and check everything before usage — I can not be responsible for any damage caused by your usage of this code.


Some code or concepts used from these Arduino tutorial:

Rangiano is released under the MIT License.


Play melody by changing distance. Arduino project







No releases published