Wednesday, February 06, 2013

How to Build an Arduino Audible Distance Sensor

In this post I will summarize how I built an audible distance sensor, which can be used as a vehicle reverse sensor, using an Arduino Uno R3, HC-SR04 ultrasonic sensor, and a some simple electronic components.

This sensor outputs a beep-beep sound which increases in frequency when an obstacle gets closer to the sensor. Shorter the distance between the obstacle & sensor, higher the frequency.

Part List


  1. Arduino or Arduino clone such as TechDuino
  2. HC-SR04 ultrasonic sensor
  3. 8 Ohm speaker
  4. BD140 transistor
  5. 100 Ohm resistor
  6. 220 Ohm resistor
  7. 1N4004 diode
  8. Red LED

Step 1: 

Setup the HC-SR04 sensor as shown in the two diagrams below
HC-SR04 ultrasonic sensor

Pin positions for HC-SR04 sensor

Step 2:

Setup the speaker with a series current limiting resistor as shown below
Pin positions for speaker connection





Step 3: 

Compile the following code in the Arduino IDE & upload it to your Arduino or Arduino clone.


// Arduino Audible Distance Sensor (Reverse Sensor)

#define NOTE_C4  262

void setup(){
  pinMode (2,OUTPUT);//attach pin 2 to vcc
  pinMode (5,OUTPUT);//attach pin 5 to GND
  // initialize serial communication:
  Serial.begin(9600);
}

void loop(){
  digitalWrite(2, HIGH);
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(3, OUTPUT);// attach pin 3 to Trig
  digitalWrite(3, LOW);
  delayMicroseconds(2);
  digitalWrite(3, HIGH);
  delayMicroseconds(5);
  digitalWrite(3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode (4, INPUT);//attach pin 4 to Echo
  duration = pulseIn(4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  
  if (inches < 2){
      playTone(NOTE_C4, 10);
      Serial.print("< 2");
   } else  if (inches < 4){
      playTone(NOTE_C4, 8);
      Serial.print("< 4");
   } else if (inches < 5){
      playTone(NOTE_C4, 7);
      Serial.print("< 5");
   } else if (inches < 6){
      playTone(NOTE_C4, 5);
      Serial.print("< 6");
   } else if(inches < 10){
      playTone(NOTE_C4, 4);
      Serial.print("< 10");
   } else if(inches < 20){
      playTone(NOTE_C4, 3);
      Serial.print("< 20");
   } else if(inches < 25){
      playTone(NOTE_C4, 2);
      Serial.print("<25");
   } else if (inches < 30){
      playTone(NOTE_C4, 1);
   }
}


void playTone(int note, int noteDuration){
    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    
    int duration = 1000/noteDuration;
    tone(8, note,duration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = duration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8); 
}

long microsecondsToInches(long microseconds){
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

Step 4:

Improve the sound by connecting a 9V external power source as shown in the diagram below.

Sound circuit schematic

The complete setup

References

  1. Arduino Basics: Making Sound http://www.instructables.com/id/Arduino-Basics-Making-Sound/
  2. Play melody using tone function http://arduino.cc/en/Tutorial/tone
  3. Easy ultrasonic monitoring http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/

Recommended vendor in Sri Lanka

TechKatha

Recommended vendor - International

Post a Comment