Arduino Sonar

1,984

20

2

Published

Introduction: Arduino Sonar

The goal of this test plan is to use a Sonar to detect when the door is opened.

Step 1: Gather Your Materials

First, you will have to gather all the materials necessary. They are as followed:

Arduino Uno microcontroller

USB cable (to connect the Arduino to the computer)

Laptop Computer

Breadboard

Wires

LED

SONAR

Step 2: Connecting Your Circuit

The next step is to begin plugging in the circuit that will allow you to detect when the door opens with the SONAR.

Follow the following instructions in order to correctly program your code:

1. Have all your materials out

2. Connect the SONAR to one of the far sides of the breadboard. The four pins on the SONAR should be in the middle of that column in which you connected it to

3. Connect each four wires to the breadboard, ones for each section of the SONAR (Vcc, Trig, Echo, Gnd). The wires should connected be right next to where you connected the to SONAR. The wires should be different colors and each section of the SONAR (Vcc, Trig, Echo, Gnd) should have its own respective wire.

4. Plug the Vcc wire into the 5V on the Arduino microcontroller

5. Plug the Trig wire into pin 9

6. Plug the Echo wire into pin 10

7. Plug the Gnd wire into the Gnd pin

Step 3: Programming Your Arduino

Type the following code into you computer:

int echo= 10;

int trig= 9;

void setup() {

pinMode (trig, OUTPUT);

pinMode (echo, INPUT);

Serial.begin (9600); {

void loop () {

digitalWrite (trig, LOW);

delayMicroseconds (2);

digitalWrite (trig, HIGH);

delayMicroseconds (10);

float duration= pulseIn (echo, HIGH);

Serial.println (distance);

delay (1000);

}

When your done typing the code, compile it to make sure that your didn't make any errors when typing the code. Then, upload the program to your code and run it on the serial monitor.

Step 4: Recording Your Calibration Data

Right now, your Arduino is not distance producing values. We need to calibrate it, which means we have to take a series of voltage measurements using the Arduino and the measurements have to be at various distances. At the same time, we will have to be recording the distances at the individual voltage measurements.

In order to take the calibration data, you place tape on the ground at different distances with 6 in intervals. The distance you need to use is up to you but, I recommend going up to at least 48 inches to get accurate data. You will need an object (we used a whiteboard) and place the ultrasonic sensor (SONAR) at the different distances. Then, you record the values that appear.

Step 5: Creating Your Calibration Curve

Put your recorded data into Excel. Now that all of your data is in Excel, we will use it to create a calibration curve. We will also develop an equation that will allow us too

In Excel highlight your data (voltage values should be on the left while the distances you used should be on the right. Then, select "insert" on the tool bar, go to the charts section, and click "Scatter or Bubble Chart". The graph should pop up (a series of dot should appear) and then you right-click on one of the data points and select "Format Trendline". Then "Trendline option" should appear and select "Linear" and the at the bottom select the box that say "Display Equation on chart". Your graph should have a straight line and an equation should appear next to the graph.

Step 6: Calibrating Your System

Now that you have successfully created a calibration curve and an equation, you have to update you code so that it can correctly print distance values.

Take out the comment under "float duration" and instead place under it:

float distance;
distance= (0.0061*duration) + 2.5923;

(These are the numbers from our data and yours will most likely be different; just replace the numbers from our equation with the numbers from your equation).

Now save your code, compile it to avoid any errors, and upload it to your Arduino.

Step 7: Testing Your Device

Congratulations! You have constructed a distance sensor that can measure distance using a SONAR and arduino micro-controller. Now, you have to test it to make sure it is accurate. In order to do this, you have to modify your micro controller so that it includes an LED that is connected to pin 8.

Then add this code into the " void setup() { " section:

pinMode (8, OUTPUT);

Add this code into the " void loop() { " section:

if (distance>10) {

digitalWrite (10, HIGH);

}

else {

digitalWrite (10, LOW);

}

Finally, set up the SONAR so that it is ten inches away from the door (the door should be closed). Then, open the door. If the LED lights up you have successfully calibrated your distance sensor! Good work!

Share

    Recommendations

    • Water Contest

      Water Contest
    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest

    2 Discussions

    Looks simple enough, but since I have no idea of how to "enter or complied my 'code'", it'll have to wait. E.g., I have never done that before. I have the hardware, but have never really made anything with it.

    Welcome to Instructables! Thanks for sharing :)