Simple Distance Sensor
Fun projects from beginner to advanced

 
Simple Distance Sensor

Simple Distance Sensor

Ever wonder how the car detects objects while reversing? Here's a simple example of how it works.

This device takes a reading from the ultrasonic sensor. The Arduino then processes that information and outputs the preset distances onto the LED's and also outputs an audible sound.
This project needs:
* Arduino Uno x 1
* Breadboard x 1
* Piezo buzzer x 1
* HC-SR04 Ultrasonic sensor x 1
* LED's x 3(1 red,1 yellow,1 green)
* Resistor 150 ohm x 3
* Resistor 100 ohm x 1
* Assorted jumper cables

Connecting it up

Simple Distance Sensor

Electrical Diagram

Simple Distance Sensor Electrical Diagram


Programming
You can copy and paste this code into the Arduino IDE or write it yourself.

/*
* Ultrasonic Distance Sensor 
* :-this program lights up the different 
* colour LED's and beeps faster, when the 
* object gets closer to the sensor.
* Code by intRobotics
* references: https://www.arduino.cc/en/tutorial/ping
*/

// setup LED pin numbers
int led_red = 6;
int led_yellow = 10;
int led_green = 9;

// setup speaker pin number
int speaker = 7;

// speaker tone interval
unsigned long tone_time = 0;

// setup ultrasonic sensor pin numbers
int trig = 4;
int echo = 2;

//=========================================================
//NUMBERS TO CHANGE
// speaker tone.  Change the number to change the tone
int pitch = 400;

int beep_time = 15; // multiplyer for beep interval.  change this number to change beep interval

//create distances for program (distances in cm)
int distance_min= 2; //minimum range
int distance_mid = 20; 
int distance_max = 40; 
int distance_over = 60; // maximum range

//=========================================================

void setup() {
  Serial.begin(9600);
  //initialize led pins as outputs
  pinMode(led_red, OUTPUT);
  pinMode(led_yellow, OUTPUT);
  pinMode(led_green, OUTPUT);
  
  //initialise speaker pin as an output
  pinMode(speaker, OUTPUT);
  
  //initialize ultrasonic pins as an output and input
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}


//=========================================================

void loop() {
  long duration, distance; //setup 2 variables for the ultrasonic sensor 'ping'

  digitalWrite(trig, LOW);  //pulse the trigger pin
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH); //get time between pulse and echo
  
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  distance = duration / 29 / 2;
  
  Serial.print(distance);

  if(distance >= distance_max && distance < distance_over){
    digitalWrite(led_red,LOW);
    digitalWrite(led_yellow,LOW);
    digitalWrite(led_green,HIGH);
    speaker_beep(distance);
  }
  
  else if(distance >= distance_mid && distance < distance_max){
    digitalWrite(led_red,LOW);
    digitalWrite(led_yellow,HIGH);
    digitalWrite(led_green,LOW);
    speaker_beep(distance);
  }
  
  else if(distance >= distance_min && distance < distance_mid ){
    digitalWrite(led_red,HIGH);
    digitalWrite(led_yellow,LOW);
    digitalWrite(led_green,LOW);
    speaker_beep(distance);
  }
  
  else if(distance > distance_over){
    digitalWrite(led_red,LOW);
    digitalWrite(led_yellow,LOW);
    digitalWrite(led_green,LOW);
    
    noTone(speaker); //turn off speaker
  }
}

void speaker_beep(int DISTANCE){
  if((millis()-tone_time) >= (DISTANCE*beep_time)){
    tone(speaker, pitch);
    tone_time = millis();
  }
  else if((millis()-tone_time) >= 50){
    noTone(speaker); //turn off speaker
  }
}


Technical stuff
The ultrasonic sensor works by sending an ultrasonic 'ping' out of the device. A timer (stopwatch) is started when this ping is sent. The 'ping' hits an object and is bounced back, this is called an 'echo'. When the 'echo' reaches the sensor again the timer stops.
So what we really have is a sensor that can tell distance in the form of time.
The 'time' value isn't really any good to us for this project so we need to convert that time into a distance value, and this is where some simple math comes in.

distance (d) = speed (s) x time (t)
Where:
Distance = is the value we want to find (in centimeters, cm)
Speed = the speed of sound (ultrasonic means it is a sound at a high frequency) because this is happening so fast we need to work in mircoseconds (1.0x10^-5 seconds)
Time = the time it took for the sound to reach the object

Lets look at the speed of sound:
This is approximately 340m/s at sea level, but we want to know how fast sound travels over 1cm. We know that there is 100cm in a meter. Therefore:

t = d/s
t (seconds) = cm/(cm/s)
t = 1/(340 x 100)
t = 1/34000
t = ~2.9x10^-5 seconds/cm

Now it is much easier for the program to calculate in whole numbers so we can convert this time to microseconds.

t = seconds x 1000000 microseconds
t = (~2.9x10^-5) x 1000000
time(speed of sound) = 29 microseconds/cm

Lets now look at the time of the ultrasonic sensor:
We program the arduino to log this time in microseconds so this will fit into our equation nicely. However, the sound went out to the object and BACK AGAIN. To measure the distance we only need the time in one direction. Since the sound travels the same speed in both directions all we need to do is divide our logged time by 2. Therfore:
time(halfdistance) = time(ultrasonic)/2

Lets now look at the final equation:
We now have the speed of sound and the ultrasonic time calculated, now it is as simple as putting the equations together.
Therefore:

distance(object) = time(microseconds)/2/29(microseconds/cm)

distance(object) = time(halfdistance)/29
or
distance = time(ultrasonic)/29/2

Math Complete!
Now you can use the distance measurement value (in centimeters) for any project you want!

PCB - Arduino UNO

References

* Arduino-Ping Tutorial: https://www.arduino.cc/en/Tutorial/Ping

 
     

© Copyright intRobotics. All Rights Reserved.