HC-SRO4 controlled sound installation

I am attempting to make a sound installation using HC-SRO4 sensors to map the noise too, I was originally planning to adapt the AUDUINO synthesiser and have built it using potentiometers and wanted switch them out for the sensors, due to my basic knowledge of Arduino it was too difficult for me, but if anyone could help me work it out it would be greatly appreciated!!!

I am using an Arduino UNO R3

But because of this I decided to try my own, I have been able to map the distances provided by the sensors to some scales, but I don't know how to get the Arduino to make a noise aha whether it be a tone or a play function im unsure how to make it work, ill leave the code here too.

// Include NewPing Library
#include "NewPing.h"
#include <avr/io.h>
#include <avr/interrupt.h>
// Define Constants

#define TRIGGER_PIN_1  10
#define ECHO_PIN_1     10
#define TRIGGER_PIN_2  5
#define ECHO_PIN_2     5
#define MAX_DISTANCE 4000
#define PWM_PIN 3


// Define Variables

float distance1; // Stores calculated distance in cm for First Sensor
float distance2; // Stores calculated distance in cm for Second Sensor
float tone1;
float tone2;

int output;

uint16_t minorpentatonicTable[55] = {

uint16_t mapminorPentatonic(uint16_t input) {
  uint8_t value = (1023-input) / (1024/53);
  return (minorpentatonicTable[value]);

uint16_t minordiatonicTable[76] = {

uint16_t mapminorDiatonic(uint16_t input) {
  uint8_t value = (1023-input) / (1024/53);
  return (minordiatonicTable[value]);

void setup() {
  Serial.begin (9600);
 pinMode(PWM_PIN, OUTPUT);

 void play ( int output ) {               
    digitalWrite(PWM_PIN, HIGH);            
    digitalWrite(PWM_PIN, LOW);              

void loop()
 distance1 = sonar1.ping_cm();
  // Add a delay between sensor readings
  // Measure duration for first sensor
  distance2 = sonar2.ping_cm();

  // Send results to Serial Monitor
    Serial.print("Distance 1: ");

    if (distance1 >= 4000 || distance1 <= 2) {
    Serial.print("Out of range");
    else {
    Serial.print(" cm ");
    Serial.print("Distance 2: ");

    if (distance2 >= 4000 || distance2 <= 2) {
    Serial.print("Out of range");
    else {
    Serial.print(" cm");
  Serial.println(" ");

 tone1 = mapminorDiatonic( distance1 );

  tone2 = mapminorPentatonic( distance2);


Thanks Guys, Much appreciated!!!

Hi there! What sort of sounder(s) do you have? With a simple piezo buzzer, you can use the tone() function.

You may already know this, but a way to define notes from a chromatic scale is to take a starting tone, e.g. 440 Hz (which is a concert pitch A), and multiply it by 2^(1/12) to get one semitone up. I can expand on this if it would help :smile:


Hiya thanks for the response!

im using an 2.5mm AUX output



Anyone able to help?

4000 cm is 40 meters. You'll be lucky to pick up anything much further than 4 meters. I would set MAX_DISTANCE to 500 and then use MAX_DISTANCE in your 'if' statements rather than 4000.

The 10 millisecond delay may be too short. Start at 30 and reduce that until the second sensor starts reading echoes from the first sensor's ping (low readings).

The lack of a delay between the second sensor and the repeat of loop() may cause similar late echo problems. Put a similar delay after the second sensor.

Awesome I’ll have a scan through and switch it up!

Thank you

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.