Problems obtaining accurate distance with MaxSonar-EZ

I have two MaxSonar connected to two analog input. The problem is that I can’t obtain any accurate distance. What could be the problem? Here’s the code.



  • EctoSensor.h
  • Created on: Feb 8, 2017
  •  Author: Adrian 47




#include “Ultrasonic.h”
#include “AHRS.h”
#include “WPILib.h”

class EctoSensor {

double distance1;
double distance2;


AnalogInput *range1;
AnalogInput *range2;

void SetDistance1();
void SetDistance2();

double GetDistance1();
double GetDistance2();

virtual ~EctoSensor();



Source .cpp


  • EctoSensor.cpp
  • Created on: Feb 8, 2017
  •  Author: Adrian 47


#include “HEADERS/EctoSensor.h”

EctoSensor::EctoSensor() {

// distance1 = range1->GetVoltage() * ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_VOLT;
// distance2 = range2->GetVoltage() * ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_VOLT;

range1  = new frc::AnalogInput(0);
range2 =  new frc::AnalogInput(1);


void EctoSensor::SetDistance1(){
// TODO: add distance

// distance1 = range1->GetVoltage()/0.009766;
distance1 = range1->GetVoltage() * ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_VOLT;

void EctoSensor::SetDistance2(){
// TODO: add distance
distance2 = range2->GetVoltage() * ANALOG_RANGE_FINDER_DEFAULT_INCHES_PER_VOLT;

double EctoSensor::GetDistance1(){
return distance1;

double EctoSensor::GetDistance2(){
return distance2;

EctoSensor::~EctoSensor() {


Where on your robot are these sensors and what’s their intended purpose?

their are in the front of the robot, it’s intended purpose is to help aligning in autonomous, but I can’t take any distance, I know my code isn’t working, but why do you know why? Or a better solution.

What I’m basically trying to do is obtain the voltage of the analog input, and multiply it by a factor and obtain distance.

Two ultrasonic sensors on front of your robot in free run mode is probably going to cause problems, since they’ll be sending out identical pings at various times. Inevitably, the left sensor is going to pick up the right sensor’s ping at some point, and give you bad data.

First test is to disable one of the sensors and see if you’re getting reliable data from just one. Check the analog output of the sensor with a multimeter to make sure the sensor is giving you good data. Then test your code and see if the code is working.

I’m a little confused by your SetDistance/GetDistance functions, but it should work if you always call SetDistance before GetDistance.

Assuming you get everything working with one sensor, the next trick is getting it working with two. Look at the MaxSonar datasheet an scroll to "Using Multiple sensors in a single system. You probably want one the first or second option. Either one is going to require you rigging a digital output to one of the sensors, to start a measurement cycle. For the first option, you’ll have to start the cycle every time you want a measurement. For the second option, you only start the cycle once and then it loops continuously until power is turned off. The first option is a lot friendlier to other robots using ultrasonics.

Hello, I’ve allready test both sensors, with the multi meter and it still give me error, I’m messing about 60 to 70 mV, and it’s also jumping a lot the signals.