Advertisement
icstation

For Arduino Ultrasonic Module HC-SR04 Distance Transducer S

Nov 25th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. test code for Ultrasonic Module HC-SR04 Distance Transducer Sensor (http://www.icstation.com/product_info.php?products_id=1389)
  2. //Function: This procedure applies in the using ARDUINO MEGA 2560 drive ultrasonic module HC-SR-04.Measure distances with a serial show
  3. //Time: August 16, 2012
  4. int TrigPin = 5;
  5. int EchoPin = 6;
  6. void setup()
  7. {
  8. Serial.begin(9600);
  9. pinMode(TrigPin,OUTPUT);
  10. pinMode(EchoPin,INPUT);
  11. }
  12. void loop()
  13. {
  14. int distance,duration;
  15.  
  16. digitalWrite(TrigPin,HIGH);//TrigPin prepare high of more than 10us
  17. delayMicroseconds(11);
  18. digitalWrite(TrigPin,LOW);
  19.  
  20. duration = pulseIn(EchoPin, HIGH);//EchoPin received high start counting until the receiver to the low,return to the count valu
  21. duration = duration/29/2;//Calculating the distance cm
  22. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  23.  
  24. Serial.print(duration);//Serial display distance
  25. Serial.print("cm");
  26. Serial.println();
  27. delay(1000);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement