Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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:
- 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.
- */
- #include <NewPing.h>
- #define TRIGGER_PIN 2 // Arduino pin connected to sensor's trigger pin
- #define ECHO_PIN 3 // Arduino pin connected to sensor's echo pin
- #define MAX_DISTANCE 760 // maximum distance the sensor can measure, in centimeters
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // create a NewPing object for the sensor
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- delay(50); // wait 50ms between readings
- // read the distance from the sensor in centimeters
- int distance = sonar.ping_cm();
- // print the distance
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.println(" cm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement