Jul 26, 2009

Ultrasonic Range Finder with Arduino

I recently got a Maxbotix LV-EZ4 Ultrasonic Range Finder to try understand how to use it with my little Arduino. Having a ultrasonic range finder can be very useful in many different projects, as it can detect objects, I can use it to make alarm systems (cars usually use this kind of sensor), Obstacle-Avoiding Robot and many other things that you may have in mind. It works better outdoors then the IR sensor, but the only thing I could notice is that it doen’t detects very well some thick carpets. Besides that, it’s a great sensor. From the datasheet we can read that it sonar range information from 6-inches out to 254-inches (15.24 cm – 6.452m).

Maxbotix Features:

  • 42kHz Ultrasonic sensor
  • Operates from 2.5-5.5V
  • Low 2mA supply current
  • 20Hz reading rate
  • RS232 Serial Output – 9600bps
  • Analog Output – 10mV/inch
  • PWM Output – 147uS/inch
  • Small, light weight module

Docs:

How the Sonar works:
It basically sends a high frequency wave in one direction, the wave hits a object that reflects the wave. When the wave gets back to the sensor, it calculates the distance depending on the time it takes to go and back. More information can be checked at Wikipedia.

Working with Arduino:

I think the easiest way to work with the sensor is using the Analog Output Mode, where you just need to give the sensor some juice connecting with the 5V and Ground, and the Analog Output on any Analog-Digital Converter pin on the Arduino. Where it gives (Vcc/512) / inch where I am using 5V, so it’s 10mV/inch. But the AD from the Arduino have 1024 steps from 0-5V. So to have the information from the sensor in inches we need to make ADvalue/2 and if you want to have the information in cm, just multiply the previous value for 2.4 (1 inch = 2.4 cm).

Pics:

Video:

So here is the code:
You can download the file HERE.

/*

  @ Code for interfacing arduino with a Maxbotix LV-EZ4 Ultrasonic Range Finder
  @ Code by Daniel Spillere Andrade
  @ www.danielandrade.net    -=-   [email protected]

*/



int blinkLed=13;         // Where the led will blink
int sensorPin=0;         // Analog Pin In
int sum=0;     // Variable to calculate SUM
int avgrange=50;         // Quantity of values to average
int sensorValue;         // Value for te average
int i,media,d;           // Variables
float cm,inch;           // Converted to cm


void setup()
{
  Serial.begin(9600);   // To check what is being read on the Serial Port
}

void loop() {

    d=analogRead(sensorPin);            // Read the analog value
    digitalWrite(blinkLed,HIGH);        // Turn on LED
    delay(d);                           // Delay changes with the analogread
    digitalWrite(13,LOW);               // Turn off LED
    delay(d);                         // Another delay
   
    cm = (d / 2) * 2.4;                 // Convert the value to centimeters
    inch = d/2;                         // Value in inches
   
    Serial.println(cm);                 //Print average of all measured values
   
    // This is the code if you want to make an average of the read values
   
     /*
     
       for(i = 0; i < avgrange ; i++) {
     sum+=analogRead(sensorPin);
     delay(10);
  }

        media = sum/avgrange;
  Serial.println(media);  //Print average of all measured values
   
      sum=0;
      media=0;
   
    */

   
}

Leave a comment