Introduction: How to Use Ultrasonc Sensor Using Arduino

About: someone who likes electronics

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.

Step 1: How It Work

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

Step 2: Components Required

Step 3: Connect the Ultrasonic Sensor to Arduino

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

Step 4: Upload Sketch

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

Step 5: Result

You can put objects in front of the sensor

You can see the results on the serial monitor.