Sunday, July 13, 2014

Arduino Ultrasonic Proximity Sensor


This is an Arduino-based Proximity Sensor circuit wired using the popular HC-SR04 Ultrasonic ranging module,used here for non-contact object detection purpose. The HC-SR04 module includes ultrasonic transmitters, receiver and control circuit. You only need to supply a short pulse to the trigger input to start the ranging, and then the module will send out burst of ultrasound at 40 kHz and raise its echo.

The set-up process is very simple, and best understood by referring the wiring diagram. If everything goes right,you should see the burning eye of the red LED when there is no object in the safe-zone (within 5 cm from the HC-SR04 module), and an ear-splitting beep from the piezo-buzzer otherwise.

Arduino Ultrasonic Sensor Wiring Diagram

arduino proximity sensor circuit
Arduino Sketch
  1. /*
  2. Project: Ultrasonic Proximity Sensor
  3. Sensor: HC-SR04
  4. Courtesy Note: Inspired by the Arduino Ping Sketch
  5. Tested At: TechNode Protolabz / June 2014
  6. */
  7. //Pins for HC-SR04
  8. const int trigPin = 13;
  9. //Pin which delivers time to receive echo using pulseIn()
  10. int echoPin = 12;
  11. int safeZone = 5;
  12. // Pins for Indicators
  13. int statusLed = 11;
  14. int pzBzr = 10;
  15. void setup() {
  16. }
  17. void loop()
  18. {
  19. //raw duration in milliseconds, cm is the
  20. //converted amount into a distance
  21. long duration, cm;
  22. //initializing the pin states
  23. pinMode(trigPin, OUTPUT);
  24. pinMode(statusLed, OUTPUT);
  25. pinMode(pzBzr, OUTPUT);
  26. //sending the signal, starting with LOW for a clean signal
  27. digitalWrite(trigPin, LOW);
  28. delayMicroseconds(2);
  29. digitalWrite(trigPin, HIGH);
  30. delayMicroseconds(10);
  31. digitalWrite(trigPin, LOW);
  32. //setting up the input pin, and receiving the duration in uS
  33. pinMode(echoPin, INPUT);
  34. duration = pulseIn(echoPin, HIGH);
  35. // convert the time into a distance
  36. cm = microsecondsToCentimeters(duration);
  37. //Checking if anything is within the safezone
  38. // If not, keep status LED on
  39. // Incase of a safezone violation, activate the piezo-buzzer
  40. if (cm > safeZone)
  41. {
  42. digitalWrite(statusLed, HIGH);
  43. digitalWrite(pzBzr, LOW);
  44. }
  45. else
  46. {
  47. digitalWrite(pzBzr, HIGH);
  48. digitalWrite(statusLed, LOW);
  49. }
  50. delay(100);
  51. }
  52. long microsecondsToCentimeters(long microseconds)
  53. {
  54. // The speed of sound is 340 m/s or 29 microseconds per centimeter
  55. // The ping travels forth and back
  56. // So to calculate the distance of the object we take half of the travel
  57. return microseconds / 29 / 2;
  58. }

No comments:

Post a Comment