PING sensor giving incorrect values on Pin A0!

I have connected PING sensor to arduino mega 2560 adk
I have connected the SIG pin to analog pin A0
I am getting readings.
but the readings are in a specific range 300-500..
when i try bringing objects in front of the sensor,the values do not change much.
The values are not exact.
This is my code:

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
//convert to cm
long dist=sensorValue*(0.03434);
Serial.print(dist);

// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
}

The ultrasonic waves of the sensor do not detect objects in front of it or maybe the waves get past through the objects,,Plz help.. Thanks in advance :slight_smile:
How can i get exact readings in cms../?

The ouput in the serial monitor is like this

15sensor = 510 output = 127
17sensor = 464 output = 115
15sensor = 501 output = 124
17sensor = 477 output = 118
16sensor = 486 output = 121
16sensor = 493 output = 122
16sensor = 470 output = 117
16sensor = 506 output = 126
17sensor = 460 output = 114
15sensor = 513 output = 127
17sensor = 459 output = 114
15sensor = 506 output = 126
17sensor = 469 output = 116
16sensor = 493 output = 122
16sensor = 485 output = 120
16sensor = 477 output = 118
16sensor = 500 output = 124
17sensor = 464 output = 115
15sensor = 509 output = 126
17sensor = 458 output = 114
15sensor = 510 output = 127
17sensor = 464 output = 115
15sensor = 500 output = 124
17sensor = 477 output = 118
16sensor = 485 output = 120
16sensor = 492 output = 122
16sensor = 469 output = 116
16sensor = 506 output = 126
17sensor = 460 output = 114
15sensor = 512 output = 127
17sensor = 460 output = 114
15sensor = 505 output = 125
17sensor = 469 output = 116
16sensor = 493 output = 122
16sensor = 485 output = 120
16sensor = 477 output = 118
16sensor = 501 output = 124
17sensor = 464 output = 115
15sensor = 510 output = 127
17sensor = 458 output = 114
15sensor = 510 output = 127
17sensor = 464 output = 115
15sensor = 500 output = 124
17sensor = 477 output = 118
16sensor = 485 output = 120
16sensor = 493 output = 122
16sensor = 469 output = 116
16sensor = 506 output = 126
17sensor = 460 output = 114

Mostly the values are same evn if i move objects in frnt of the sensor

please use the # button to apply proper code tags - you can even modify your existing post.

Read this tutorial? - http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor - it provides all the answers (for this q.)

In this tutorial ,the sensor is connected to digital pin 7 and mine is connected to analog pin A0.
n we cant convert the ADC values to cms in the same manner as the tutorial does.
i dont understand why isnt there a major difference in the values when i move objects in front of the sensor.
Is it a delay problem? How much delay one should keep.?

Do you have a link to the datasheet you used? please post the link.

  • does the sensor give an analog voltage or a digital pulse (like in the tutorial).

The pulses may look to the analogIn as a medium voltage. That could explain the behavior...

Heya..thanks for replying. here is the link to the datasheet:
http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

The reason why m i using the PING sensor with Analog pin A0,is that the wire is getting loose on the digital pins. PING sensor is mostly used with the digital pins.
the holes of the digital pins on the arduino mega are slightly bigger than the analog pins.
The wire do not fit in the digital pin.Wht should i do? plz help..
=( But i have seen sketches which use PING sensor with analog pin A0. i just want the values in cms n accurate

sensorValue = analogRead(analogInPin);

The device has three pins, none of which is an analogue output.
Just use A0 as the Ping pin for the pinMode, digitalWrite etc in the example code.

Please go back to your original post, click on "modify", highlight the code, then click on the # icon on the editor's toolbar, then click on "save". Thank you.

(Not compiled, not tested)

const int pingPin = A0;

void setup() 
{
  Serial.begin(9600);
}

void loop()
{
  long rawRange = pingRange (pingPin);
  long cm = microsecondsToCentimeters(rawRange);
 
  Serial.print(cm);
  Serial.println("cm");
 
  delay(100);
}

long pingRange (int pingPin)
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Hi AWOL, Thank you so much for the code
The output is still 0 cm..
I think my sensor is faulty
sometimes when i hold the wire tight,it gives out values like 245 cm like tht
but these values r due to electric noise..
How to chekc whether my PING Sensor is faulty or not? The ACT LED on the sensor does not blink.
It hasnt blinked evn once frm the day of purchase.
But i have been gettin values if i hold the wire a little tight when it gets loose.
Plz help me out..Is the sensor faulty?

Sorry, I can't really help - I've never even seem a Ping.

But i have been gettin values if i hold the wire a little tight when it gets loose.

Trying to do electronics like thesis stupid. You must use the proper connectors and solder wires. Otherwise you can never know if you have a proper connection.

I have tested the code posted by AWOL with a Ping Sensor on A0 on a MEGA 2560 and the setup works perfectly.

There is no difference in the header pins for the Mega 2560, so there is no reason to use Analog pins vs Digital for this, but it will work on A0.

Using standard jumper cables you should get quite a snug fit.

Run a wire from the pin marked GND into Arduino pin marked GND
Run a wire from the pin marked 5V into Arduiono pin marked 5V
Run a wire from the pin marked SIG into Arduino pin marked A0

Run AWOL's code or the standard ping lib code modified to use A0.

You should see a bright green LED on the Ping blink like mad and the serial monitor will show correct distance.