Programming MaxSonar EZ1 Ultrasonic Rangefinder in Simple Robot Template

Our team is using the simple robot example as a template for our code for this year. We were wondering what the easiest way to wire and program the MaxSonar EZ1 rangefinder would be.

We looked at some other posts and it seems that we can wire it as either a digital or an analog input. Is there a “better” way to wire it, or are both equally good?

Finally, once we wire it, how would we declare it in our code, and how would we pull in a distance from the sensor?

Team 1997

If I remember correctly, the analog configuration is the default, and doesn’t offer as many options but is very easy to setup.

In 2012 when I last used this sensor, there was no WPILib class that supported it, but if they’ve added one, you’ll want to use it; otherwise you are welcome to use my 2012 code here:

You may have to configure the sensitivity ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_VOLT in the .h

We used analog coming out of the 9201 module in the rio wire red to +5 black to fend and the signal to an on the sensor

I have some new coders so I am not bothering with subroutines so we just put the commands in the main program. So this is inelegant code but it works.

float volt,inches;
AnalogChannnel *range ;

Range =new AnalogChannel(1,1);

The command to call and convert to inches is
volt = range ->GetVoltage();
inches = volt/0.009766;

That is the conversion factor and for us seems quite accurate within 3 feet.
Just use inches wherever you need the range data

We can’t seem to get readings above that so if anyone has a suggestion for boosting the power of his device to the supposed voltage of 5v I would appreciate an answer to that.

We have been having a similar issue with the Sonar sensor. Using this sensor is not complicated and appears that you have it right.

We had this working a couple years ago, but never really had a need for it until this year.

We have ours wired with red (+5v) from analog port to +5v on the sensor, ground from the analog port to ground on the sensor, and signal from analog port to “An” pin on the sensor.

We confirmed with a multimeter that the sensor was getting +5v, so it was getting the appropriate power.

We then checked the signal voltage directly on the sensor, and it never read about .066v (66 mv) and matched what the software was reading from the analog port, and this was with the sensor pointed at the tile floor at about 3’ above the floor, which should have resulted 0.3v, and pointing at it a whiteboard about 10-15’ away still resulted in 0.066v…

We have concluded that the issue is with the sensor itself, and are going to be replacing the sensor.

Wade Hasbrouck
Team 1258
Seattle Lutheran HS
Programming Mentor