Category:PCB Arduino

Group Administrators: 1 | Group Member: 81 | Group Threads: 86

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects. About Arduino and Arduino Projects sharing are appreciated.

Thread Reply
How To Measure Distance with an Arduino-based Sonar Sensor Reply 2017-02-12 23:06:20

Tom Wong-Cornall

Leave A Message


Sonar Sensors
Sonar’s most popular and primary use is to be able to "see" underwater. Sonar uses the propagation of sound to detect objects. Since sound waves travel farther in water than they do in air, sonar is preferred over other types of sensors like radar for this reason. Even though it's preferred for underwater sensing, sonar can still be used in air; however, there exists the small chance of interference, which we might see when measuring distance.
There are two types of sonar: passive sonar and active sonar. Active sonar has an emitter and a detector: depending on the time that the signal takes to come back to the sonar, it can detect the range or distance of an object and its orientation. It also detects the strength of a signal to determine how much time it took to be picked up by the receiver. Passive sonars are used to pick up signals from vessels and other marine life like whales and submarines. Passive sonars don't have emitters; they just receive sound waves coming towards them.
Bill of Materials
  1. Arduino Uno
  2. MaxBotix Ultrasonic Range Finder
  3. 3 Loose Wires
  4. Soldering Iron
  5. Solder
  6. Computer with Arduino IDE (Integrated Development Environment)
  7. USB Type B to connect the Arduino
  8. Multimeter
We will be using an Arduino Uno as our microprocessor to be able to read distance detected by the sonar. The sonar that we are using is the Maxbotix Ultrasonic Range Finder, but any models that are close to this one with an output as a pulse width or analog might be able to be used in this project. The three loose wires will be soldered to the Ultrasonic Range finder. We need the solder and soldering iron to solder wires to the sensor. Once everything is soldered and in place, the code below will have to be uploaded to the Arduino via the IDE, and it will also be connected with a USB Type B.
Getting Started
Since the Arduino and the code will interpret the output of the sonar in volts, we do not want there to be any false connections or shorts between the circuit, so we have to make sure that when the pins are soldered there is no solder residue that can cause a short.
The 3 pins that will be soldered on the sonar sensor are shown below.
Solder a wire to the ground, V in of +5 Volts, and the second from the bottom, which is the pulse width output. After soldering these three pins, clean with a cotton swab and some alcohol around the holes to get rid of any residue that may remain from the solder. To check for any shorts then use the multimeter and check the resistance between these three pins. Between the GND and the +5 V there should be OL or infinite resistance. If you check with the multimeter for an open or if you check continuity, then it should not come up. If there is some continuity between any of these three pins, then you need to de-solder the wires and clean up any solder residue. Once the wires are soldered on the Sonar Sensor and you have checked for no shorts then you can connect to the Arduino.
How to connect the sensor to the Arduino
You can connect the sensor and the Arduino above with a breadboard as a medium or you can connect directly from the sensor to the Arduino. The sensor is being grounded on the Arduino and is receiving power from the Arduino’s +5V output. The sensor's pulse width output is being connected to any input on the Arduino that can accept a pulse width. In this case, I'm using digital pin 3.
                    #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()

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);  
  delay(1500);   // Wait for 1.5 Seconds
When the Arduino is connected as shown in the diagram above and the code in uploaded, you can open the serial monitor and the distances will be displayed in inches with a refresh every 1.5 seconds. When you run the serial monitor, depending on where your sonar sensor is pointing, it will give you a certain number of inches. If you put your hand or another large object where the sonar is pointed, it will also read that and display its distance. For this specific sonar, the range is 20 feet.
Below is an image of how the serial monitor and code should look like once they're running. Happy building!
Statement: This post is only the personal view of the author and does not represent the opinions of


Leave A Message


Thanks for the article. Very valuable.