top of page

Interfacing the HC-SR04 Ultrasonic Sensor to the Arduino

In this tutorial, I'm going to show you how to Interface the Hc-Sr04 ultrasonic sensor to the Arduino uno.


ITEM REQUIRED

Jumper Wires

Ultrasonic Sensor HC-SR0

1 x Arduino board (I am using An Arduino Uno).

1 x USB cable

1 x Computer with Arduino software installed

1 x Breadboard


Circuit Theory:

An ultrasonic transducer is a device that converts energy into ultrasound, or sound waves above the normal range of a human hearing. Piezoelectric crystals have the property of changing size when a voltage is applied, applying an alternating current (AC) across them causes them to oscillate at very high frequencies, thus producing very high frequency sound waves.

The Hc SR0 is a an ultrasonic sensor build for the Arduino it provides 2cm-400cm non-contact measurement function the ranging accuracy is about 3mm. The modules includes ultrasonic transmitters, receiver and control circuit

.

The HC-SR0 gives a high level signal from the IO trigger for at least 10us. The Module automatically sends eight 40 kHz and detects whether there is a pulse signal back. IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning. Test distance = (high level time × velocity of sound (340M/S) / 2,


There are Four Pins on the HC-SR04. They are :


  • Vcc (5V supply)

  • Gnd (Ground)

  • Trig (Trigger)

  • Echo (Receive)

The operating voltage is around 5V dc,Current is 15mA and measure angle 15°


 

Step 1

Connect the circuit as per Circuit diagram


Circuit Diagram for HC-SR04 to Arduino
Circuit Diagraam-1

Arduino Interfaced to HC-SR04
Circuit Digram Pictorial Representation

Step 2:

Open a new window in the arduino terminal and type the code.


/*

Pulsein

Description. Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

*/


// defining the pins

const int trigPin = 9;

const int echoPin = 10;

// defining 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 in cm

distance= duration*0.034/2;

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

}

Connect the Arduino to your pc and upload the code to the Arduino board. Open the serial monitor to see the distance in cm. Place your hand in front of the sensor to see the change in values in the serial monitor.



Serial Monitor Displaying distance
Serial Monitor Output


The Hc-SR0 can be used in remote control cars to detect object distance, burglar alarms, sonars etc.



Stayed tuned for more fun tutorials


Buy your Arduino from the below link to DIY:

Buy Hc-SR04 From Below link:

94 views

Comments


bottom of page