Maxbotix XL-EZ2: Ultrasonic Range Finder

The Ultrasonic Range Finder is a sensor device that measures distance between the sensor and another object. It sends out an ultrasonic sound beam and measures the time taken for it to bounce back off of the closest object in front of it. Then depending on the length of the time interval, it determines the range (distance) to the nearest object in inches.

This kind of a sensor can be used in robotics to help navigate unmanned vehicles or in robotics to help them avoid running into walls or other objects. The rangefinder would detect when there is something in front of the vehicle and the vehicle or robot can be programmed to either stop or to try going around the obstacle. The ultrasonic rangefinders can also be used in stationary installations, like machines, appliances or artwork to activate certain switches based on the distance of a person from the sensor.

About theMaxbotix XL-EZ2 Ultrasonic Rangefinder:

The MaxSonar XL-EZ2 Ultrasonic Rangefinder by MaxBotix used here gives the user an option of reading in the range either as serial, analog or PWM inputs. The sample codes mentioned here use the analog input method (pin3 on the rangefinder) to read in the ranges. The ranges being read appear to be precise from around 20cm to up to even around five meters. However the closer range values tended to fall into a dead zone on distances less than ~20cm when reading through the rangefinder's analog interface for the code below.

It is also possible to read the data in PWM or serial format on this module. Separate pins are used for these (pin2 for PWM, pin5 for Serial data). The user can also set the Serial output pin's mode to read either asynchronous Serial data, or simply just a pulse instead. The beam size of the rangefinder also varies depending upon the input voltage. It can be connected to 3.3V to up to 5V to increase total area that the ultrasonic beam covers. The code below can be used with either voltage.

Circuit:

Schematic:

Sample Arduino Program:

This code simply takes the input from the rangefinder's analog pin and checks it against a threshold to light up an LED if an object is too close.

/*

This code takes in the input from the rangefinder's analog pin.

The pin returns distance (in inches) from the sensor to the

closest object in front of it. If an object is closer than the

threshold distance, the LED lights up.

Setup:

Connect the analog pin (3/AX) of the rangefinder to the Arduino

Board's Analog input pin0. Connect the ground and v++ on the

rangefinder to a GND and 5V on the Arduino respectively. Connect

the LED to pin13 and GND.

By Naureen Mahmood

*/

#define THRESHOLD 30 // threshold distance in inches

#define LED_PIN 13 // LED output pin

#define RF_PIN 0 // Range Finder input pin

void setup()

{

Serial.begin(9600);

pinMode(RF_PIN, INPUT);

pinMode(LED_PIN, OUTPUT);

}

void loop()

{

// Read distance on Rangefinder's analog pin

int distance = analogRead(RF_PIN);

Serial.println(distance); // Print measured distance

// if an object is closer than threshold distance, turn on LED

if (distance < THRESHOLD)

digitalWrite(LED_PIN, HIGH);

else

digitalWrite(LED_PIN, LOW);

}

Sample Arduino Program (with averaged values to avoid jitters):

In this code, to avoid jitters, we take an average of 50 input samples before printing out the measured distance. If the measured distance is less than 30 inches, the LED on pin 13 is turned on.

/*

This code takes in the input from the rangefinder's analog pin. The pin

returns distance (in inches) from the sensor to the closest object in

front of it. To avoid jitters, we take an average of 50 input samples

before printing out the measured distance. If the measured distance is

less than 30 inches, the LED on pin 13 is turned on.

To avoid jitters, we take an average of 50 input samples

before printing out the measured distance. If the measured distance is

3 comments:

Hi, I want to tell you that you have done a very nice work!! I just want to ask you if you can explain the code a little bit , that I need to know what do i have to change if am not using LED and i want to write the distance result in serial monitor ? and I am using LV-1 i don't have ax only TX and RX?? can you help me please?

Hi Gigi. Thanks, I'm glad you found this post helpful. And apologies for the delay! Basically, it's just the following two lines you need in your main loop, but it may differ across different Arduino boards. So take this as a sample:

Also, bear in mind whether you want to use the analog or serial output from the Maxbotix sensor - because they're all on separate pins (e.g. for the one explained here, it's pin3 for analog, pin5 for serial etc.). Hope this helps!

In case it still doesn't help, I also found this link for you to read RX channels with the Arduino: http://dduino.blogspot.de/2012/06/read-rx-channels-through-arduino.html