Proximity Sensor unstable analogRead

Hi.Need help with sensors.

I testing Infrared Proximity Sensor (2-15cm GP2Y0A51SK0F Infrared Proximity Distance Sensor)

with simple code and analogRead is unstable. Serial Monitor printing for example:

270
271
270
270
310
265

I tried with 2 arduinos (Mega and Uno),A1,A2,A3,A4,A5 (Uno),A6,A7(Mega) and 6 sensors
but every time is unstable with dc 5,5v battery and ac adapters.I wired trimpot potentiometer to adjust voltage but no luck.

I really appreciate your help.

with simple code

...which, along with your schematic, you didn't post.

Did you try the code from: http://www.instructables.com/id/How-to-Use-the-Sharp-IR-Sensor-GP2Y0A41SK0F-Arduin/?

Paul

Thanks Paul.Perfect :slight_smile:
My code was:

int infra = A2;

void setup() {
 
  pinMode (infra, INPUT);  
  Serial.begin(9600);

}

void loop() {
 
Serial.println (analogRead(infra));
delay(500);
 

}

Ahmo:
Thanks Paul.Perfect :slight_smile:
My code was:

int infra = A2;

void setup() {

pinMode (infra, INPUT); 
  Serial.begin(9600);

}

void loop() {

Serial.println (analogRead(infra));
delay(500);

}

Such helpless people. That was the second or third entry that Google returned to me.

Did you connect the grounds?