Arduino: Reading the Maxbotix Ultrasonic Rangefinder
This was so easy it wasn’t even funny. I think I spent more time discovering that I was trying to read the wrong pin than I did actually getting this to work. The main reason I’m posting it is so that people considering this rangefinder can see just how easy it is to use compared to some of the other rangefinders out there.
This reads the voltage from the AN (analog) pin on the Maxbotix LV-EZ1 Ultrasonic Range Finder (I got mine at SparkFun). Connect power and ground on the Maxbotix to a reference ground and power, then connect the AN pin to the analog input of your choice.
Here’s a simple program to read the sensor and report the range in inches on serial:
//-*-C-*-
// read values from a LV-MaxSonar-EZ1 sensor
// this is for the Arduino MINI -- change the pin values to suit your board.
// jet@flatline.net
// 1 Dec 2007
//Output
int statusLed = 13;
//intput
int ez1Analog = 0;
void setup() {
pinMode(statusLed,OUTPUT);
pinMode(ez1Analog,INPUT);
beginSerial(9600);
}
void loop() {
int val = analogRead(ez1Analog);
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;
Serial.println(val); // inches
}
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);
}
[tags]arduino,maxbotix[/tags]