Create Sonar Rangefinder Using Arduino

689

13

2

Hello! the goal of this instructable is to be able to detect the distance of an object using a sonar rangefinder. This will include how to set up the sonar to the Arduino, an example code that includes an explanation of the calibration, and how to use this device to calculate distance. Note that some of the images used will use the circuit board for attaching the sonar to the Arduino, and some do not, however they can be used interchangeably as they are the same.

Step 1: Gather Your Materials

You will need:

1. Arduino board

2. Sonar Rangefinder

3. jumper wires, or

3.5. circuit wires(optional, for breadboard use)

4. breadboard(optional)

Step 2: Connect Sonar Rangefinder to Aruino

WITHOUT BREADBOARD

1. Connect Trig to Pin 11 (or the pin of your choice)

2. Connect Echo to Pin 10 (or the pin of your choice)

3. Connect GND to the corresponding GND on the Arduino

4. Connect Vcc to the 5v position on the power section of the Arduino

WITH BREADBOARD

1. Connect Vcc to the positive terminal of the breadboard, and GND to the negative terminal

2. Connect the negative terminal to the GND position of the Arduino, and the positive terminal to the 5v position of the Arduino

3. Connect Trig to the pin of your choice (on the diagram, pin 8)

4. Connect Echo to the pin of your choice (on the diagram, pin 9)

Step 3: Create Your Code

The following code has already been calibrated, as it was obtained from an outside source that included the calibrations.

#define trigPin 9 //tells Arduino that the trig pin is pin 9
#define echoPin 10 //tells Arduino that the echo pin is pin 10

void setup() {
Serial.begin (9600); //sets the data rate of transmission to 9600
pinMode(trigPin, OUTPUT); //sets the trigPin as the output
pinMode(echoPin, INPUT); //sets the echoPin as the input
}

void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
//digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
Serial.println(duration);
distance = (duration / 2) * 0.0344; //calculates the duration into centimeters

if (distance <= 2){
Serial.print("Distance = ");
Serial.println("Out of range"); //doesn't print distance if less that a certain interval
}
else {
Serial.print("Distance = ");
Serial.print(distance); //prints distance within the interval
Serial.println(" cm");
delay(500);
}
delay(500);
}

Step 4: Run Your Program

This is pretty self explanatory. After you run the program, record the data.

Share

    Recommendations

    • Toys Contest

      Toys Contest
    • Big and Small Contest

      Big and Small Contest
    • First Time Author

      First Time Author

    2 Discussions

    0
    None
    Techno_Bot01

    Question 7 months ago

    Can it record it in inches?

    0
    None
    Swansong

    11 months ago

    Thanks for sharing :)