Aug 29, 2011

Maxbotix Ultrasonic Rangefinder

I said in yesterday’s post that I haven’t had much time to work on projects. With hurricane Irene baring down on the East Coast at the time I am writing this entry, I don’t have anywhere else to go and little else to distract me while I wait out the storm. To pass the time, I decided to start experimenting with a Maxbotix LV EZ4 ultrasonic range finder I purchased from SparkFun a few weeks back. For those who don’t know, Maxbotix makes affordable sonar sensors for distance measurements and whatever else you can come up with. I have seen several small robot builds that use ultrasonic sensors to detect objects in the robot’s path and adjust accordingly. The sensors fire a 42kHz sound wave (beyond the range of human hearing by about 2x) and then record the reflections using the echoes to determine the distance of the object reflecting the wave.

 Figure 1. My Maxbotix LV EZ4 ultrasonic range finder
So why would I want one of these sensors and what am I planning on doing with it? Well, some time back we overloaded our garage with two refrigerators in one parking bay. In order to be able to open both doors and park a car in the bay the car has to be in just the right spot. Since we live in a world governed by electronics I figured hanging a tennis ball from the roof would be too primitive a solution to this pseudo-problem. Instead, I thought it would be cool to mount a sensor on the wall facing the car and have it track the movement of the car as it pulls into the bay. The idea was to rig the sensor to a microcontroller and have it change an RGB LED from green to yellow to red as the car moved closer to the perfect parking spot. At first I was really excited about this project because I thought it had a few unique elements that set it aside from other similar projects. Unfortunately, I have since come to find out that not only is this not a new project, but there are already a few commercial products that do this exact thing (including my traffic light plan…)

After some encouragement, I realized this could still be a learning experience and a fun project even if I do end up spending more time and money than it would take to buy something off the shelf. This entry is really just to show off some of the cool things this sensor can do and how to interpret its outputs. In addition, I will look at a few different configuration options you have when working with these sensors.

These Maxbotix sensors give you three options to read their outputs: analog, serial, and pulse width. The analog is probably the most popular and the simplest to understand. It is also the output I have chosen to start with in my design. The analog output works by increasing the output voltage of the sensor the farther away an object is from its position. Maxbotix specs this relationship at 9.8mV/inch with a minimum distance of 6 inches and a maximum distance of 254 inches (yes we are working in SI units here). That means that the sensor’s output voltage will increase by 9.8mV every time the object you are tracking moves an inch farther away.

The serial output uses the basic transmit (TX) and receive (RX) communication standard and the appropriate COM port on your computer. I am less familiar with this operation, but all you really need to know that it is possible to communicate with the sensor using a serial port hence you are transmitting a digital value, which is typically less noisy and more accurate.  

Lastly, the pulse width output uses low frequency pulses to monitor the object’s distance. The farther away the object is from sensor, the wider the pulse width and vice versa. Figure 2 below is published on MaxBotix’s website and it shows the difference between the analog pin output and the pulse width output.

Figure 2. Maxbotix LV EZ line pulse width versus analog output relationship, Courtesy of Maxbotix

My plan right now is to use a microcontroller to digitally sample the analog output and use the samples to track how close the car is to the ideal spot – meaning I will need to calibrate the sensor to the perfect spot. As the car gets closer I will write a function that controls the weights of red, green, and blue in an RGB piranha superflux LED. Right now I am still debating on whether to fade the LED between the traffic light colors or use discrete color states like a real traffic light. I will probably experiment with both and figure out which I like best, but reader input is always welcome.

Before I get ahead of myself, first thing is first: I need to make sure the sensor works. To make things simple, I built up a quick “acquisition” system using the ultrasonic sensor, my oscilloscope, and a little excel magic (and by magic I mean making a graph). Figure 3 shows the rig I built for testing. You can find my connection diagram at the end of this entry.

 Figure 3. Testing rig for rangefinder

By setting my oscilloscope’s time base to 2.5 seconds/division I automatically put it into roll mode, which means that it slowly tracked the sensors response in nearly real time since it is a digital store oscilloscope (DSO). As you can see in Figure 4, the only problem is that the oscilloscope tracks the output voltage and not the distance.

Figure 4. Oscilloscope readout of rangefinder's analog output voltage

Luckily, my scope can output the samples it uses to construct the waveform in a .CSV file, making it easy to process in excel. Using the 9.8mV/inch spec I was able to convert the recorded measurements into distance. Lastly, I time shifted the scope’s output for causality’s sake. You can see the result in Figure 5 below, though it is mostly a sample of me waving my hands in the front of the sensor and moving them back and forth...hence the sine waves. You may also notice that there are high frequency spikes in the middle of otherwise naturally following waveforms. These are an area of concern for me and right now I am thinking I may need to design either a filter in hardware or using some sort of averaging of the samples in the software to correct the issue.

Figure 5. Oscilloscope output translated into distance data via Microsoft Excel

I did a few more tests using walls as my detection sources and I have to say I am really impressed with the accuracy of the sensor. I tested the measurements I was getting from the sensor against some hand measurements made with a tape measure and they were virtually spot on.

Figure 6. Oscilloscope acquisition connection diagram
I am looking forward to playing around with this thing a bit more in the future, but for now I am pretty happy with the results of this initial test run. If you want one of these sensors to try out for your very own, Sparkfun offers them for about $31 shipped. I encourage you to visit Maxbotix’s website for their sensor selection guide, which is basically a flowchart that will lead you to the best sensor for your application. A good general purpose one is the EZ0 model of the product line I purchased. As promised, the connection diagram I used for my oscilloscope can be seen on the right.


Post a Comment