Arduino Sonar Kit

37

3

1

Posted

Introduction: Arduino Sonar Kit

This instructable will show you the steps on how to connect an HC-SR04 Ultrasonic Sensor to an Arduino UNO in order to indicate whether a door is open or closed.

Step 1: Setting Up and Getting All Material

The first step would be to get all of the materials needed and get set up. You'll need:

Arduino Uno microcontroller with USB to connect it to a computer Arduino

sonar Breadboard

Assorted wires for connections

Two different colored LEDs (Yellow and Blue as depicted in images will work)

Step 2: Connect the Wires

The next step is to get to wiring everything together. The Arduino sonar has four connection pins: GND (ground), TRIG, VCC, and ECHO. Connect GND on the sonar to GND on the Arduino. Do the same for 5V. Connect ECHO to Pin10, and connect VCC to Pin9. Now, to connect the LEDs; whichever LED you want to turn on when the door is open, connect it to Pin8. for whichever LED you want to turn on when the the door is closed, connect it to Pin7. Then check your connections with the diagram above to see if they match.

Step 3: The Code

This step is very important to having the computer do what the purpose of the set up is. The computer must be able to read the correct code or the set up will not work. Insert the following code in the program:

int off = 7;
int on = 8;

int trig = 9;

int echo = 10;

void setup() {

pinMode (7, OUTPUT);

pinMode (8, OUTPUT);

pinMode (9, OUTPUT);

pinMode (10, INPUT);

Serial.begin (9600); }

void loop() {

digitalWrite (trig, LOW);

delayMicroseconds (2);

digitalWrite (trig, HIGH);

delayMicroseconds (10);

float duration;

float distance;

duration = pulseIn (echo,HIGH);

Serial.println (duration); }

Step 4: Calibration

After step 3, the values that appear will seem not correct and that there is a problem, this is because calibration has not occurred and you must calibrate it to get the accurate values you are looking for. To so, set the sonar/Arduino contraption up on a flat surface. Then, place tape at intervals of 6 in, up to about 48 in. You can choose to continue calibrating it beyond 48 in to get more accurate data, but the is the sonar's general range. Once you're ready, use a large, flat object to hold in front of the sonar at the taped intervals, and record the approximate voltage that comes up.

Step 5: Create Curve and Equation

Taking the data you recorded from calibrating, use a program such as Excel or Google Sheets to create a scatter plot graph of the data. Then, format a trendline, and make it a linear trendline, and display the equation for it.

Step 6: What to Do With the Equation

You should now have a general equation for your calibration curve (although it's more of a line), we're going to take it and add it to our code, creating a new variable named distance. (NOTE *** The numbers displayed in the code below are from my data and calibrations. Your data may be similar, but it's best to use the data that you recorded):

distance = (0.006 * duration) + 3.083;
Serial.println (distance);

if (distance <= 6) {

digitalWrite (on, HIGH);

digitalWrite (off, LOW);

}

else {

digitalWrite (on, LOW);

digitalWrite (off, HIGH);

}

}

Your code should now look like this:

int off = 7;

int on = 8;

int trig = 9;

int echo = 10;

void setup() {

pinMode (7, OUTPUT);

pinMode (8, OUTPUT);

pinMode (9, OUTPUT);

pinMode (10, INPUT);

Serial.begin (9600);

}

void loop() {

digitalWrite (trig, LOW);

delayMicroseconds (2);

digitalWrite (trig, HIGH);

delayMicroseconds (10);

float duration;

float distance; duration = pulseIn (echo,HIGH);

//Serial.println (duration);

distance = (0.006 * duration) + 3.083;

Serial.println (distance);

if (distance <= 6) {

digitalWrite (on, HIGH);

digitalWrite (off, LOW);

}

else {

digitalWrite (on, LOW);

digitalWrite (off, HIGH);

}

}

Step 7: Test the Device and Set Up

Now that you have completed the necessary steps, it's now time to test your device! Set it in front of a door -- or use any other similarly flat surface -- any place it in front of the sonar. If you put it too close (according to the boundaries you've previously set), one LED should turn on. When you move it away, outside of that boundary, the first LED should turn off and the second should turn on. If it all works, you are all done!

Share

    Recommendations

    • Science of Cooking

      Science of Cooking
    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Spotless Contest

      Spotless Contest
    user

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

    Tips

    Questions

    Comments

    Thanks for sharing :)