SHARE
TWEET

Arduino SR04 Distance Sensor test

w0lfiesmith Aug 15th, 2013 4,686 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <NewPing.h>
  2.  
  3. #define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  4. #define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
  5. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  6.  
  7. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  8.  
  9. void setup() {
  10.   Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  11. }
  12.  
  13. void loop() {
  14.   delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  15.   unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  16.   Serial.print("Ping: ");
  17.   Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  18.   Serial.println("cm");
  19. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top