HC-SR04 Ultrasonic distance sensor

£ 2.60

FREE DELIVERY

Here we have a precision ultrasonic range finder that accurately measures distance.

This particular module is very popular with hobbyists and robotic enthusiasts and there are many online tutorials and projects that attest to this.

  • Voltage: DC5V
  • Static current: less than 2mA
  • level output: high-5V
  • level output: the end of 0V
  • Sensor angle: not more than 15 degrees
  • Detection distance: 2cm-450cm
  • High precision: up to 0.3cm
  • Input trigger signal: 10us TTL impulse
  • Echo signal : output TTL PWL signal

HC-SR04 Ultrasonic distance sensor

Getting started using your sensor simply requires direct connection to your Arduino or you could follow the hook-up diagram further down the page to build a slightly more complex circuit.

Simple Distance Measurement

Connect directly to your microcontroller and start reading distance right away

  • Ultrasonic range finder
  • Arduino
  • Jumper Wire
image

This code returns the distance in Inches. Remove the *.39 to return cm instead of inches. You could make float ultrasoundValue = 0; but then you can't print it unless you transfer it into another type, but it could be used for further calculations.


unsigned long echo = 0;
 int ultraSoundSignal = 9; // Ultrasound signal pin
 unsigned long ultrasoundValue = 0;
 void setup()
 {
 Serial.begin(9600);
 pinMode(ultraSoundSignal,OUTPUT);
 }
 unsigned long ping(){
 pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
 digitalWrite(ultraSoundSignal, LOW); // Send low pulse
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
 delayMicroseconds(5); // Wait for 5 microseconds
 digitalWrite(ultraSoundSignal, LOW); // Holdoff
 pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
 digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
  // please note that pulseIn has a 1sec timeout, which may
  // not be desirable. Depending on your sensor specs, you
  // can likely bound the time like this -- marcmerlin
  // echo = pulseIn(ultraSoundSignal, HIGH, 38000)
  echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
  ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
 return ultrasoundValue;
 }
 void loop()
 {
 int x = 0;
 x = ping();
  Serial.println(x);
  delay(250); //delay 1/4 seconds.
 }

Distance based piezo tone

The following circuit sounds a piezo buzzer when an object is detected. The tone will get progressively louder as the object draws closer to the sensor.

  • Ultrasonic range finder
  • Piezo speaker
  • 10 kohm resistor
  • Arduino
  • Jumper Wire
image

const int TRIG = 8;
const int ECHO = 7;
const int BUZZ = 5;
 
void setup(){
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
}
 
void loop(){
  long duration, inches, cm;
   
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2); 
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
   
  duration = pulseIn(ECHO, HIGH);
  inches = duration / 74 / 2 ;
  cm = duration / 29 / 2 ;
   
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  if(cm < 20){
    analogWrite(BUZZ, (20 - cm) * 10);
  }else{
    digitalWrite(BUZZ, LOW);
  }
   
  delay(100);
}