AI-Duino: Artificial Intelligence Robot with Arduino
Lets Be Friends
  • AI-Duino Overview
  • Aiduino Forum
  • AI-Duino Research Lab
    • HC-SR-4 Ultrasonic Sensor Module
    • Related Projects
  • AI-Duino Blog
  • AI-Duino Aims
  • AI-Duino Media
  • About AI-Duino
    • Contact Us
    • Donate

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:
  1. VCC
  2. trig(T)
  3. echo(R)
  4. GND

Module Working Principle:
  1. Adopt IO trigger through supplying at least 10us sequence of high level signal.
  2. The module automatically send eight 40khz square wave and automatically detect whether receive the returning pulse signal.
  3. If there is signals returning, through outputting high level and the time of high level continuing is the time of that from the ultrasonic transmitting to receiving. Test distance =(high level time * sound velocity (340M/S) / 2.
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
ultrasonic.rar
File Size: 1 kb
File Type: rar
Download File

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.

Picture
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  terry@yourduino.com");

}//--(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 )-----*/

Powered by Create your own unique website with customizable templates.