Maker.io main logo

How to Use a Sonar Sensor on an Arduino to Measure Distance

2017-11-22 | By All About Circuits

License: See Original Project 3D Printing Arduino

Courtesy of All About Circuits

Sonar Sensors

The primary use for sonar is to be able to "see" and navigate underwater, using the propagation of sound to detect objects. Sound waves travel farther through water than air, making sonar the preferred of sensors over other options such as radar. Although sonar is preferred for underwater sensing, it can still be used in air. Note, however, that there is a small chance of interference, which might show up when we measure distance.

There are two types of sonar: active sonar and passive sonar. Active sonar contains an emitter and a detector. Depending on the amount of time that the signal takes to return to the sonar, it can detect the distance of an object and its orientation. It can also detect the strength of a signal to determine the amount of time it took to be picked up by the receiver. Passive sonars pick up signals from vessels and marine life, such as whales and submarines. They don't have emitters; they simply receive sound waves coming toward them.

There are two types of sonar: active and passive

Bill of Materials

You will use an Arduino UNO as your microprocessor to read the distances detected by sonar. This project uses the Maxbotix Ultrasonic Range Finder, but any models close to this one with an output as a pulse width or analog cou be used in this project. Your three three loose wires need to be soldered onto the Ultrasonic Range finder with your soldering iron. When everything is soldered in place, upload the code below to the Arduino via the IDE. The Arduino will also be connected with a USB Type B.

Getting Started

With the Arduino and code interpreting the output of the sonar in volts, you don’t want any false connections or shorts between the circuit. Make sure when you solder the pins, no solder residue is left to cause a short.

You can see the three pins you will solder on the sonar sensor in the image below.

the three pins you will solder on the sonar sensor

Solder the first wire to the ground (V in of +5 volts) and the second from the bottom (the pulse width output). After soldering, clean everything with a Q-Tip and alcohol to remove any remaining residue.

After this step, use your multimeter to check the resistance between the three pins. There should be OL or infinite resistance between the GND and the +5 V. Checking with a multimeter for an open or continuity, it shouldn’t come up. However, if any continuity shows up between these three pins, you need to de-solder the wires, clean up the solder residue, and reattach. Once you have checked and tested the wires with no shorts registering, you can connect to the Arduino.

How to connect the sensor to the Arduino

How to connect the sensor to the Arduino 

You can either connect the sensor and the Arduino with a breadboard as a medium or connect directly from the Arduino to the sensor. The Arduino grounds the sensor and delivers power from the Arduino +5V output. The sensor's pulse width output is connected to any input on the Arduino that can accept a pulse width. In this example, we are using digital pin 3.

Copy Code
#include "Maxbotix.h"

Maxbotix rangeSensorPW(3, Maxbotix::PW, Maxbotix::LV); // #3 Defines which Digital Input in being Read
//Maxbotix:PW defines that the Arduino is reading PW signals

void setup()
{
Serial.begin(9600);
}

void loop()
{
unsigned long start;

Serial.println("Reading 1st Sensor"); //Serial Monitor will print this line

start = millis(); // Number of Milli seconds until the Sonar Receives the signal it sent out
Serial.print("PW 1: ");
Serial.print(rangeSensorPW.getRange()*.393701); // Multiply by this to convert Cm to Inches
Serial.print(" inches - ");
Serial.print(millis() - start);
Serial.println("ms");

Serial.println();
delay(1500); // Wait for 1.5 Seconds

Arduino Sonar Code 

Once you connect the Arduino like the diagram above and the code is uploaded, the serial monitor can be opened, displaying the distances in inches with a refresh every 1.5 seconds. Depending on where your sonar sensor is pointing when you run the serial monitor, it will give you a certain number of inches. When you put your hand (or another large object) in the area the sonar is pointed, it will read the object and display its distance. The range is 20 feet for the sonar in this project.

The image below shows you how the serial monitor and code will look once they are running.

To download the code, click here.

image shows you how serial monitor and code will look

 

 

 

 

 

 

制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
制造商零件编号 172
ULTRASONIC SENSOR MLV-MAXSONAR
Adafruit Industries LLC
制造商零件编号 153
JUMPER WIRE M TO M VARIOUS
Adafruit Industries LLC
制造商零件编号 P2C
SOLDERING IRON CORDLESS 25-75W
Apex Tool Group
制造商零件编号 389261
60/40 370 3% .015DIA 27AWG
Harimatec Inc.
制造商零件编号 3021001-03
CBL USB2.0 A PLUG TO B PLUG 3'
Qualtek
制造商零件编号 815
HAND HELD PARTS TESTER
B&K Precision
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum