HC-SR04 Ultrasonic Measuring Module (including Libraries and a test sketch for arduino 1.0 / 1.0.1)
Working Voltage : 5V(DC)
Static Current: Less than 2mA. Output Signal: Electric frequency signal, high level 5V, low level 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 Mode of connection:
Module Working Principle:
Cost of HC-SR04: £1.30
Bought from: notebookpcparts168 (ebay) Delivery time: 13 days Library for HC-SR04 (Arduino IDE 1.0 / 1.0.1 compatible) Click the file below and wait for download. Once downloaded, extract the file to C:\Documents and Settings\yourname\My Documents\Arduino\libraries (if "libraries" file does not exist you will need to create it in the Arduino directory). Ultrasonic library was written by Stigern, the original file can be found here: http://stigern.net/blog/?tag=hc-sr04#codesyntax_1 ![]()
Restart the IDE and check for Ultrasonic in sketch/import library. It should appear at the bottom of the list.
To test the HC-SR04, connect vcc to 5v on arduino, GND to Ground, Trig to pin 13 and Echo to pin 12. Then, in the IDE, select Ultrasonic from the import library (as above), this should produce one line at the top of the sketch (#include <Ultrasonic.h>). Then, copy and paste the code to the right under the first line. The code was written by Terry King and further information can be found at his site: http://arduino-info.wikispaces.com/UltraSonicDistance. It will print to serial the distances in inches. |
![]() HC-SR04 Ultrasonic Module
/*-----( Import needed libraries )-----*/
#include "Ultrasonic.h" /*-----( Declare Constants and Pin Numbers )-----*/ #define TRIG_PIN 12 #define ECHO_PIN 13 /*-----( Declare objects )-----*/ Ultrasonic OurModule(TRIG_PIN, ECHO_PIN); /*-----( Declare Variables )-----*/ void setup() /****** SETUP: RUNS ONCE ******/ { Serial.begin(9600); Serial.println("UltraSonic Distance Measurement"); Serial.println("YourDuino.com [email protected]"); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { Serial.print(OurModule.Ranging(CM)); Serial.print("cm "); delay(100); //Let echos from room dissipate Serial.print(OurModule.Ranging(INC)); Serial.println("inches"); delay(500); }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ |