HC-SR04 Ultrasonic Sensor Distance Measuring Module



HC-SR04 Ultrasonic Sensor Distance Measuring Module

- Model: HC-SR04 - Color: Blue + Silver - 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 3mm - Mode of connection: VCC / trig(T) / echo(R) / GND - Module Working Principle: - Adopt IO trigger through supplying at least 10us sequence of high level signal - The module automatically send eight 40khz square wave and automatically detect whether receive the returning pulse signal - 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

A lire
 * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1272177425
 * http://iteadstudio.com/application-note/arduino-library-for-ultrasonic-ranging-module-hc-sr04/

Projets

 * Fusion multi-capteurs pour table tactile 2013
 * RobAIR 2013 Configuration 2

Arduino

 * Ultrasonic
 * Library New Ping

Simple
// load the library for HC-SR04 http://iteadstudio.com/application-note/arduino-library-for-ultrasonic-ranging-module-hc-sr04/ // Remark: change the “WProgram.h” reference to “Arduino.h” in files "Ultrasonic.h" and "Ultrasonic.cpp" and everything will be fine


 * 1) include "Ultrasonic.h"

// Sensor VCC pin connected to Arduino pin 5V // Sensor GND pin connected to Arduino pin GND // Sensor TRIG pin connected to Arduino pin 12 // sensor ECHO pin connected to Arduino pin 13

Ultrasonic ultrasonic(12,13);

void setup { Serial.begin(9600); }

void loop { Serial.print(ultrasonic.Ranging(CM)); Serial.println(" cm");

delay(100);

Serial.print(ultrasonic.Timing); // Distance = ((Duration of high level)*(Sonic :340m/s))/2 Serial.println(" ms");

delay(100); }

With 3 Ultrasonic + Servo


/* * UltraSonic Scanner * * output values from a ultrasonic sensors array in a NMEA 0183 sentence (ie formatted message) * * $USDIS,LEFT_DISTANCE_IN_CM,CENTER_DISTANCE_IN_CM,RIGHT_DISTANCE_IN_CM,SERVOPOS*CHECKSUM[CR][LF] * * TODO Calcutate checksum (always 00) */
 * 1) include "Ultrasonic.h"
 * 2) include 

Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created Ultrasonic ultrasonicCenter(12,13); Ultrasonic ultrasonicRight(10,11); Ultrasonic ultrasonicLeft(6,7);

int servoPos = 90;   // variable to store the servo position int servoIncr = 10;   // variable to store the servo increment const int MINSERVOPOS=55; const int MAXSERVOPOS=125;

void setup { Serial.begin(4800); myservo.attach(3); // attaches the servo on pin 9 to the servo object }

void loop { Serial.print("$USDIS,"); Serial.print(ultrasonicLeft.Ranging(CM)); Serial.print(","); Serial.print(ultrasonicCenter.Ranging(CM)); Serial.print(","); Serial.print(ultrasonicRight.Ranging(CM)); Serial.print(","); Serial.print(servoPos); Serial.print("*"); Serial.print("00"); Serial.print("\r\n"); myservo.write(servoPos);             // tell servo to go to position in variable 'pos' servoPos=servoPos+servoIncr; if(servoPosMAXSERVOPOS) servoIncr=-servoIncr;

delay(100); }

byte calcChecksumBuf(byte checksum, byte buf[], int i, int len){ for(i<len; i++){ checksum=calcChecksum(checksum,buf[i]); } return checksum; }

byte calcChecksum(byte checksum, byte c){ if ( c == '$' ) return 0; if ( c != '\r' &&  c != '\n'  &&  c != '*' ) { checksum ^= c;       } return checksum; }