obligatory obscure reference


self-deprecating yet still self-promotional witty comment

2007/12/02

Arduino: Reading the Maxbotix Ultrasonic Rangefinder

Filed under: Arduino,Hacking — jet @ 18:32

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]

4 Comments »

  1. hi,

    If i want to build a circuit refer to EZ1 circuit diagram, can you teach me about the c code to program PIC16F676? Is the TX supposed to be an output in this program?

    Tq

    Comment by zero — 2011/03/18 @ 10:01

  2. Any possibility you can post code for doing the same thing using the serial com pin? Would be great help. I feel that would have more accuracy. Thanks. :D

    Comment by Amogh Gudi — 2011/03/27 @ 11:46

  3. 1 – Sorry, I don’t know much about using the PIC. Basically all we’re doing is reading a value from the analog pin that is reported as an integer between 0 and 512.

    2 — Using the Serial class wouldn’t be any more accurate and it would also be slower. The sensor reports a byte between 0 and 512, it’s faster to read that directly off of the analog input than it is to read it as a serial character then convert it to an int.

    Comment by jet — 2011/03/27 @ 12:02

  4. Hi,

    I’ve build the program above but there are some errors:

    Undeclared identifier [pinMode] & [OUTPUT] in expression;

    I try to declared the identifier but failed. Can you help me?

    Tq

    Comment by zero — 2011/04/05 @ 04:12

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress