How to Use Ultrasonc Sensor Using Arduino

by MrSottong in Circuits > Arduino

233 Views, 0 Favorites, 0 Comments

How to Use Ultrasonc Sensor Using Arduino

IMG_7121.JPG
IMG_7117.JPG

HC-SR04 is an ultrasonic sensor that can be used to measure the distance of an object. This sensor can be used to measure objects from 2cm - 400cm.

This sensor has 2 parts. The first part is the signal transmitter. The second part is the signal receiver.

HC-SR04 sensor has 4 pins. Vcc, Trig, Echo and Gnd. Vcc and Gnd are pins for supply. Trig pin to give the Trigger to the Signal Transmitter to emit a signal. Echo pin to measure how long the signal sent by the signal transmitter until received by the signal receiver.

How It Work

004-6-karakteristik-pewaktu-sensor-ultrasonik-hc-sr04.jpg
IMG_7119.JPG

When the Trig Pin gets a positive voltage for 10uS. Senosor will emit 8 Step ultrasonic signal with a frequency of 40Khz. After the emitted signal hits an object, the signal will return and be received by the signal receiver. The time difference between the sending and receiving signals will be used to interpret the distance between the object and the sensor.

See the picture for a visual representation.

Formula for measuring distances :

S = 340*t/2

Components Required

IMG_7118.JPG
IMG_6882.JPG
IMG_6885.JPG

Connect the Ultrasonic Sensor to Arduino

bitmap.png
IMG_7120.JPG

Connect the ultrasonic sensor to Arduino.

See the picture or follow the instructions below:

Sensor ultrasonic ke arduino

Vcc ==> +5V

Trig ==> D12

Echo ==> D11

Gnd ==> GND

Upload Sketch

image2134.png

I have provided a sketch for this ultrasonic sensor.

// defines pins numbers
const int trigPin = 12; const int echoPin = 11;

// 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); }

Result

IMG_7123.JPG
a.png
IMG_7124.JPG
b.png

You can put objects in front of the sensor

You can see the results on the serial monitor.