Create Sonar Rangefinder Using Arduino





Introduction: Create Sonar Rangefinder Using Arduino

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


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


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);

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

duration = pulseIn(echoPin, HIGH);
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");

Step 4: Run Your Program

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



  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Paper Contest 2018

    Paper Contest 2018

We have a be nice policy.
Please be positive and constructive.




Thanks for sharing :)