Distance Detection

with MaxSonar ultrasonic rangefinder

This is a writeup on the MaxSonar ultrasonic range finder. I felt there was a lack of information out there on this product so I thought this might help out people who are learning.

First of All I used the LV-MaxSonar EZ1 but I imagine the fundamentals will be the same and can be applied to all the MaxSonar products.

Please feel free to update the information here if you find different results for different products.


There are 3 ways to interface the the MaxSonar.

  1. Analog. Very simple, but not as accurate as pw in my experience.
  2. PW - Pulse Width. I found there was not much information out there but it is easy to do.
  3. Serial communication. I will not cover this but someone else is free to contribute :).


Analog

//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09

//Analog pin 1 for reading in the analog voltage from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int anPin = 1;

//variables needed to store values
long anVolt, inches, cm;
int sum=0;//Create sum variable so it can be averaged
int avgrange=60;//Quantity of values to average (sample size)

void setup() {

  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);

}

void loop() {

  pinMode(anPin, INPUT);

  //MaxSonar Analog reads are known to be very sensitive. See the Arduino forum for more information.
//A simple fix is to average out a sample of n readings to get a more consistant reading.\\ //Even with averaging I still find it to be less accurate than the pw method.\\ //This loop gets 60 reads and averages them

for(int i = 0; i < avgrange ; i++) {

    //Used to read in the analog voltage output that is being sent by the MaxSonar device.
    //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
    anVolt = analogRead(anPin);
    sum += anVolt;
    delay(10);

  }  

  inches = sum/avgrange;
  cm = inches * 2.54;
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  //reset sample total
  sum = 0;

  delay(500);

}


PW

I found this to be more accurate than the analog method and just as easy to implement.

//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09

//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;

void setup() {

  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);

}

void loop() {

  pinMode(pwPin, INPUT);

    //Used to read in the pulse that is being sent by the MaxSonar device.
//Pulse Width representation with a scale factor of 147 uS per Inch.

pulse = pulseIn(pwPin, HIGH); //147uS per inch inches = pulse/147; //change inches to centimetres cm = inches * 2.54;

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

  delay(500);

}