Maxbotix sonar sensor, weird readings

Hello,

I have been fighting with Maxbotix EZ1 sensor for a while. It seems I'm not getting sensible values from the ultrasonic sensor.

In case I connect sensor's "PW pin" to arduino's digital pin and divide the reading value by 147 (147uS per inch) and multiply by 2.54 (1 inch = 2.54 cm) I get values between 150cm-50cm. The first strange thing is that I get lower reading when I move my hand out of the sensor. The second strange thing is that I actually get reading from 0-5 cm away from the sensor. It doesn't detect anything away 5 cm. According to the specs (maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf) "Objects from 0-
inches to 6-inches range as 6-inches" so it shouldn't even be detecting as close range values as I can get. Eventhough I can get very precise readings from 0-5 cm away from the sensor, that's not really what I'm after. The output values are also very inaccurate (150cm-50cm).

It is also possible to connect sensor's "AN pin" to arduino's analog pin. This seems to be a way more popular method, but I get constantly floating values (values up to ~600). Voltage seems to be changing every moment even if I keep my hand at fixed height above the sensor.

I think it would be a good idea to also post the code I'm using with my arduino board.

//This code is for the digital pin mode

int pingPin = 7;
int ledPin = 13;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop()
{
long duration, cm;
long inches;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
//inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

//Serial.print(inches);
//Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(10);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 147;
}

long microsecondsToCentimeters(long microseconds)
{
long inches = microseconds / 147;
return inches * 2.54 ;
}

//end of digital pin mode

And code for analog pin mode:

//Analog mode

//Output
int statusLed = 13;

//intput
int analogSignal = 0;

void setup() {
pinMode(statusLed,OUTPUT);
pinMode(analogSignal,INPUT);

Serial.begin(9600);
}

void loop() {
int val = analogRead(analogSignal);
if (val > 0) {
// The Maxbotix reports 512 steps of information on AN
// but we read that as 1024. Each step is 1", so we need
// to divide by 2 to get the correct rough range in inches.
//
// this value should also be calibrated for your particular
// sensor and enclosure
val = val / 2;
val = val * 2.54;
Serial.println(val); // cm
}
blinkLed(statusLed,100);
}

void blinkLed(int pin, int ms) {
digitalWrite(pin,LOW); // turn it off it was on
digitalWrite(pin,HIGH);
delay(ms);
digitalWrite(pin,LOW);
delay(ms);
}
//end of analog mode

If someone has arduino and maxbotix sensor, could you please provide some help and probably the code that works for you. I would also like to know if it's better to use digital or analog mode. At the moment I'm declining to a digital mode.

Hi,

I've done this tuto: http://www.sherkhan.net/blogs/frikadas/?p=210
And it's worked well.

You could try this fix too:
http://www.maxbotix.com/MaxSonar-EZ1__FAQ.html#New_Flash_UAV_and_Mobile_Robotic_Users

And here goes another link to do more testing:
http://www.arduino.cc/playground/Main/MaxSonar

I guess the best method depends on what are you trying to achieve, but i think PW is more precisely.

Btw, I'm looking for examples about the use of RX pin to trigger the sensor, and a schematic about resyncying it with an Arduino signal.

Hope that helps!

Thanks for the links.
I got it working some time ago maybe the connection wasn't the best possible on a breadboard so I soldered it. Although the distance value I'm getting isn't right (about +-10cm, +-4inch) but at least it works. Well it's easy to compensate it with little programming, but let's see what I'm going to do when I have time again to play.