Arduino Uno Sonar Kit -- 7 Easy Steps

384

7

1

Posted in TechnologyArduino

Introduction: Arduino Uno Sonar Kit -- 7 Easy Steps

This instructable will show you 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: Gather All Materials

The first thing you're going to want to do is gather all of your materials. You'll need:

Arduino Uno microcontroller with USB to connect it to a computer

Arduino sonar

Breadboard

Assorted wires

Two different colored LEDs

Step 2: Wiring

The next step is to wire everything. It's pretty simple to follow the diagram, but I'll explain the basic connections. 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.

Step 3: Add the Code

Next, open up the Arduino program on your computer and type in the following code:

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: Calibrate Data

As of now, the sonar is seems to be producing weird values. This is just the amount of time in microseconds for the sonar to emit a sound and receive it back, translated into voltage. In order for the sonar to work effectively and display the values in actual measurments, you have to calibrate the sonar and collect data. 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 (we used a portable whiteboard to do this, but any similar object will do fine.)

Step 5: Create a Calibration Curve

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: Plug in the Equation

Now that you 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);

}

}

*****BE SURE TO SAVE AND COMPILE YOUR CODE TO AVOID LOSING YOUR HARD WORK AND TO MAKE SURE YOU DON'T HAVE ANY ERRORS!!!!!

Step 7: Test the Device

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.

PAT YOURSELF ON THE BACK BECAUSE YOU'RE NOW 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 :)