HC-SR04 Ultrasonic Sensor
Ultrasonic distance sensor for robotic projects
Made In The UK
INTERNATIONAL SHIPPING from £3.40
Royal Mail Signed For 1st Class (UK only) £2.50
AIRMAIL without Tracking (Whole World) £3.60 (£3.40 for EU)
ROYAL MAIL with Tracking (Whole World) £9.50
DHL Express £29-£44 depends on location
Welcome Sign in
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent range accuracy and stable readings in an easy-to-use package. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). Similar in performance to the SRF005 but with the low-price of a Sharp infrared sensor.
Using the HC-SR04 with an Arduino
There is an Arduino library for the HC-SR04 that offers two ways to use the sensor. To install, download the “Ultrasonic Library” from this page, unzip the release package into your “arduino-0022/libraries/” folder. Open the Arduino IDE and include the library by Sketch-Import library-Ultrasonic . There is also an example sketch in File-Examples-Ultrasonic-UltrasonicDemo.
You can directly connect it to your arduino board and measure the distance without wiring with this sketch.
#include <Ultrasonic.h>
int trigpin = 10;//appoint trigger pin
int echopin = 11;//appoint echo pin
Ultrasonic ultrasonic(trigpin,echopin);
void setup()
{
Serial.begin(9600);//set Serial Baud rate
Serial.println("Ultrasonic sensor starting!!!!!");
pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(9, HIGH);
digitalWrite(12, LOW);
}
void loop()
{
float cmdistance,indistance;
long microsec = ultrasonic.timing();
Serial.print("microsec: ");
Serial.print(microsec);
cmdistance = ultrasonic.CalcDistance(microsec,Ultrasonic::CM);//this result unit is centimeter
indistance = ultrasonic.CalcDistance(microsec,Ultrasonic::IN);//this result unit is inches
Serial.print(" cmdistance: ");
Serial.print(cmdistance);
Serial.print(" indistance: ");
Serial.println(indistance);
delay(1000);
}