Sonar Range Finder

Introduction: Sonar Range Finder

Goal: We want to be able to determine whether a computer screen is open or closed.

We will accomplish this by creating a circuit around an ultrasonic range finder that returns values measuring the amount of voltage across certain points in the circuit which we will translate into distance values (i.e. define a range of current values that indicate the door is open and a range of current values that indicate the door is closed).


1. Gather the following materials:

  • Arduino
  • Ultrasonic range finder
  • Four male to female Arduino jumper wires
  • USB 2.0 Cable Type A/B

Step 1: Build Your Circuit

2. Construct the following setup:

a. Connect the 5v column in the arduino to the VCC pin of the ultrasonic range finder using a male to female jumper wire.

b. Connect Digital pin 9 in the arduino to the TRIG pin of the ultrasonic range finder using a male to female jumper wire.

c. Connect Digital pin 10 in the arduino to the ECHO pin of the ultrasonic range finder using a male to female jumper wire.

d. Connect the GND column in the arduino to the GND pin of the ultrasonic range finder using a male to female jumper wire.

Step 2: Create the Program

int trig=9;
int echo=10;
void setup() {
void loop() { 
float duration, distance;

3. Save the file as Sonar_Range_Finder_Your names

4. To check that the Arduino can compile your code, "Verify" it. You can do this by either pressing Command+R or by clicking on the Verify button (it looks like a checkmark). Troubleshoot any errors that arise

Step 3: Run the Program

5. Using the USB cable, connect your Arduino to your computer. From the "Tools" menu in the Arduino IDE, make sure the appropriate Serial Port is selected.

6. Upload the "Sonar_Range" code. You should see some lights on the Arduino board flash. The Arduino will start to execute the last program that was uploaded to it.

7. Wait a few seconds. Then open the Serial Monitor by pressing Ctrl+Shift+M. (Alternately, you can click the magnifying glass icon at the top right corner of the Arduino IDE window to open the Serial Monitor).

8. You should see a list of numbers start to appear. Move your hand over the sonar (back and forth) slowly and observe how the numbers change.

Step 4: Recording Your Calibration Data

Right now, your Arduino is not producing distance values. We need to calibrate it, which means taking a series of voltage measurements with the Arduino at various distances, while simultaneously recording the distances at each voltage measurement. This way, we can create a chart that has voltage values on the left and distances on the right. From this chart, we will be able to come up with an equation that will allow us to automatically convert between volts and centimeters.

First, place the sensor 3 centimeters away from a flat surface. Write down the value that your Arduino returns on the serial monitor. Repeat this process for distances increasing in increments of 3 centimeters until you reach 21 centimeters.

You should now have a series of voltage values, each with their own corresponding distance. Enter these into a spreadsheet.

Step 5: Creating Your Calibration Curve

Plot your data on a graph. Your voltage values are your 'X' values and your distance values are your 'Y' values. Although the distance was the independent variable in our little calibration experiment in the previous steps, we want this graph to show the conversion between volts and centimeters that our range finder uses. Therefore, voltage goes on the 'X-Axis' and distance goes on the 'Y-Axis'.

Get a linear line of best fit from the plotted points and write down that equation. This is what you are going to program into your Arduino to make it convert voltage values to distance values automatically before it prints them.

Step 6: Calibrating Your System

Now that you have created a calibration curve and derived the equation that converts voltage values to distance values, you must update your code so that your Arduino prints distance values to the serial monitor.

You need to add a new line after "duration=pulseIn(echo, HIGH);", write "distance = m*duration + b", where "m" and "b" are numbers that you get from your calibration equation. Next, change delete "Serial.println(duration)". We are not going to look at the distance itself but instead will use an if statement to decide if we are within a certain distance or not. We are going to add a piece of code that will use the distance information to make a decision about whether or not the laptop is open. On the next line, write the following: if (distance>14) { Serial.println("The laptop is open") } Save your code and upload it to the Arduino.

Step 7: Testing Your Device

You have successfully constructed a digital ruler that can measure distance using an ultrasonic range finder and an Arduino!

Now to test it:

Set up your ruler directly next to the laptop so that it measures the vertical distance of the top part of the screen away from the bottom part of the keyboard (it should be perpendicular to the keyboard and pointing at the sky). Place the sensor next to the ruler and on the keyboard of the computer. Open and close the screen while watching the serial monitor. If your serial monitor says "The laptop is open" when the screen is away from the keyboard, you are done!

Be the First to Share


    • Furniture Contest

      Furniture Contest
    • Make it Real Student Design Challenge #3

      Make it Real Student Design Challenge #3
    • Arduino Contest

      Arduino Contest



    1 year ago

    Thanks for the deep dive on this component!