Arduino

What is the HC-SR04 Ultrasonic Sensor and How to Interface with Arduino Uno R3

What is the HC-SR04 Ultrasonic Sensor?

  • The HC-SR04 Ultrasonic Sensor is a device that helps to measure the distance.
  • It does this by sending out a sound wave, which is too high-pitched for our ears to hear.
  • When this sound wave hits something and bounces back, the sensor listens carefully and figures out how long it took for the sound to return.
  • This sensor provides 2cm to 400cm (~13 feet) of non-contact measuring range with an accuracy of up to 3mm.
  • This ultrasonic Sensor operates on 40KHz ultrasonic sound pulses which humans cannot hear.

HC-SR04 Ultrasonic Sensor Pinout

The HC-SR04 Ultrasonic Sensor has four pins:

  1. VCC
  2. Trigger
  3. Echo
  4. Ground
Ultrasonic Sensor Pin Diagram

Specifications

Operating Voltage

DC 5V

Operating Current

15 mA

Operating Frequency

40kHz

Max Range

4m

Min Range

2 cm

Ranging Accuracy

3 mm

Measuring Angle

15 degree

Trigger Input Signal

10µS TTL pulse

Dimension

45 x 20 x 15mm

HCSR-04 Ultrasonic Sensor Interfacing with Arduino Uno R3

HCSR-04 Ultrasonic Sensor Interfacing with Arduino Uno R3

Install Required Libraries

To install the library, navigate to Sketch > Include Libraries > Manage Libraries and now wait for the Library Manager window (You can use the shortcut key Ctrl+Shift+I) to install the library.

Now type the NewPing on the search bar and install the updated version.

HCSR-04 Ultrasonic Code for Arduino:

#include "NewPing.h" #define TRIGGER_PIN 9 #define ECHO_PIN 10 #define MAX_DISTANCE 400 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); void setup() { Serial.begin(9600); } void loop() { Serial.print("Distance = "); Serial.print(sonar.ping_cm()); Serial.println(" cm"); delay(500); }
#include "NewPing.h" #define TRIGGER_PIN 9 #define ECHO_PIN 10 #define MAX_DISTANCE 400 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); void setup() { Serial.begin(9600); } void loop() { Serial.print("Distance = "); Serial.print(sonar.ping_cm()); Serial.println(" cm"); delay(500); }

Leave a Reply