Advertisement
microrobotics

Maxbotix EZ0 Sonar Range Sensor 7.6M

Mar 30th, 2023
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Maxbotix EZ0 Sonar Range Sensor is a digital ultrasonic sensor that measures distance up to 7.6 meters. Here's an example code in Arduino that reads the distance from the sensor:
  3.  
  4. Note: This code assumes that the Maxbotix EZ0 Sonar Range Sensor is connected to pin 2 for the trigger and pin 3 for the echo of the Arduino board. If you're using different pins, you'll need to modify the TRIGGER_PIN and ECHO_PIN definitions accordingly. Also, the MAX_DISTANCE constant is set to 760 cm, which is the maximum distance the sensor can measure. Finally, note that the accuracy and reliability of the sensor may be affected by factors such as temperature and humidity, so be sure to test the sensor under the conditions in which it will be used.
  5. */
  6.  
  7. #include <NewPing.h>
  8.  
  9. #define TRIGGER_PIN  2  // Arduino pin connected to sensor's trigger pin
  10. #define ECHO_PIN     3  // Arduino pin connected to sensor's echo pin
  11. #define MAX_DISTANCE 760  // maximum distance the sensor can measure, in centimeters
  12.  
  13. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);  // create a NewPing object for the sensor
  14.  
  15. void setup() {
  16.   Serial.begin(9600);
  17. }
  18.  
  19. void loop() {
  20.   delay(50);  // wait 50ms between readings
  21.  
  22.   // read the distance from the sensor in centimeters
  23.   int distance = sonar.ping_cm();
  24.  
  25.   // print the distance
  26.   Serial.print("Distance: ");
  27.   Serial.print(distance);
  28.   Serial.println(" cm");
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement