Introduction: Massive Shakib-Sonar Instructable With Patrick, Haroon, and Chloe

In this arduino instructable you will learn how to use an ultrasonic sensor to measure distance.

Supplies

Bread Board

Ultrasonic Sonar

Jumper Wires

Arduino Motherboard

Step 1: Assemble

Connect VCC to 5V

Connect GND to GND

Connect Trig to pin 9 (digital output)

Connect ECHO to pin 10 (digital input)

Step 2: Write Code

const int trigPin = 9; const int echoPin = 10; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); }

Step 3: Record and Calibrate

HOW IT WORKS

Ultrasonic range finders measure distance by emitting a pulse of ultrasonic sound that travels through the air until it contact a given surface. When it hits the surface, it's reflected off the object and travels back to the ultrasonic device. Then, the device measures how long it takes for the sound pulse to travel from the sensor and back. From then, it sends a signal to the Arduino with info about how long it took for the sonic pulse to travel.

CALIBRATION

The speed of sound is 340 m/s, or 29.412 microseconds per centimeter. To measure the distance sound has traveled, you can use distance = time (speed of sound) / 2 (travels back and forth so you divide by 2). The sound travels away from the sensor, then bounces back after contact on a surface and returns back. To read the distance as centimeters, use the formula centimeters = ((microseconds / 2 )/29).

For example, if it takes 1000 microseconds after sending out the ultrasonic for it to come back, then the distance from the ultrasonic sensor can be found using ((1000/2) / 29) cm.