Trouble With Analog Reading From Ultrasonic Sensor

I am working on a project where I want to use an ultrasonic sensor to read water level in a holding pond. That reading will be transmitted by Xbees to an Arduino which will control a linear actuator to open and close a water valve as needed.

I am trying to take it one step at a time and right now I am just connecting the sensor directly to the Arduino. So far I am having issues getting good readings from the ultrasonic sensor (Maxbotix MB7560).

The code recommended by Maxbotix for this sensor is shown below.

When I run the code I noticed that the readings were very inaccurate. While trying to figure out the problem I noticed that even when the sensor is disconnected from the Arduino there are still results being shown.

I thought this might indicate a problem with the Arduino so I tried two more Arduinos with the same results.

Any ideas?

Thank you.

/*
This code reads the Analog Voltage output from the
5 Meter HR-MaxSonar sensors
If you wish for code with averaging, please see
playground.arduino.cc/Main/MaxSonar
Please note that we do not recommend using averaging with our sensors.
Mode and Median filters are recommended.
*/

const int anPin1 = 0;
long distance1;

void setup() {
  Serial.begin(9600);  // sets the serial port to 9600
}

void read_sensors(){
  /*
  Scale factor is (Vcc/1024) per 5mm. A 5V supply yields ~4.9mV/5mm
  Arduino analog pin goes from 0 to 1024, so the value has to be multiplied by 5 to get range in mm
  */
  distance1 = analogRead(anPin1)*5;
}

void print_all(){
  Serial.print("S1");
  Serial.print(" ");
  Serial.print(distance1);
  Serial.print("mm");
  Serial.println();
}

void loop() {
  read_sensors();
  print_all();
  delay(1696); 
}

If there is no connection to the analogue input, the input "floats" so you get stray pickup. Tie the input to 5v or Gnd while the sensor is not connected.

Weedpharma

What do you mean by "inaccurate"?

Do they vary alot from each other?

How is it wired up?

Check here for connection diagrams, code and more: Using a MaxSonar With an Arduino

weedpharma:
If there is no connection to the analogue input, the input "floats" so you get stray pickup. Tie the input to 5v or Gnd while the sensor is not connected.

Weedpharma

That makes sense. Thanks.

dlloyd:
How is it wired up?

Check here for connection diagrams, code and more: Using a MaxSonar With an Arduino

Thank you for the link. I have referred to it previously.

I have it wired:

Arduino A0 to Sensor Pin 3 (Analog Voltage Output)

Arduino 5v to Sensor Pin 6 (V+)

Arduino Ground to Sensor Pin 7 (GND)

ieee488:
What do you mean by "inaccurate"?

Do they vary alot from each other?

The measurements don't seem to have any connection to reality. Sometimes if you move an item (such as a book) closer to the sensor the measurements won't change. Sometimes it might show that the book is further away. It seems to be fairly random.

Try a 100uF capacitor from V+ Pin 6 to GND Pin 7 (as per datasheet if power supply is noisy).

Thanks. I had tried using the capacitor when I was experimenting with a different sketch and it didn't seem to help so I didn't think to try it this time.

This time it stabilized the reading a great deal.

FYI, the 1mm accuracy is only obtainable via serial data, not analog input. Analog or PWM is usually 5mm accuracy. Are you using more than one sensor together? They interfere with each other. My experience is they are fairly stable and accurate. I've only used serial output.

I am only using 1 sensor. For my application 5 mm. accuracy is more than enough.

Are you pointing the bare sensor against water surface or do you have it inside a plastic tube or other enclosures? They will reflect sound wave and create interference as well. I just can't imagine maxbotix sensors being that non-responsive as you described. Any set up photo?