HC-SR04 Ultrasonic Sensor Distance Measuring Module: Difference between revisions

From air
Jump to navigation Jump to search
Line 33: Line 33:
** [http://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example Example] with multiple ultrasonic sensors.
** [http://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example Example] with multiple ultrasonic sensors.
===Simple Example===
===Simple Example===

[http://code.google.com/p/arduino-new-ping/wiki/Simple_NewPing_Example Simple Example] with '''schematic'''.

<pre>
<pre>
// load the library for HC-SR04 http://iteadstudio.com/application-note/arduino-library-for-ultrasonic-ranging-module-hc-sr04/
// load the library for HC-SR04 http://iteadstudio.com/application-note/arduino-library-for-ultrasonic-ranging-module-hc-sr04/

Revision as of 12:23, 20 June 2013

HC-SR04 array with NewPing library
HC-SR04 Ultrasonic Sensor on Arduino Nano breadboard

HC-SR04 Ultrasonic Sensor Distance Measuring Module


  • Model: HC-SR04
  • 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

Projets


Arduino

Simple Example

Simple Example with schematic.

// 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


#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

Three HC-SR04 Ultrasonic Sensors
Three HC-SR04 Ultrasonic Sensors
Three HC-SR04 Ultrasonic Sensors on a tilt bracket
/*
 * 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)
 */
 
 
#include "Ultrasonic.h"
#include <Servo.h> 
 

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(servoPos<MINSERVOPOS || servoPos>MAXSERVOPOS) 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;
}

New Ping

From http://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example

... TODO ...