is this a noise problem?? ( How can i solve it?? )

Hi guys, I’m a Software developer who has started playing with Arduinos. I’m currently involved in a project where I’m using the Sharp IR (20-150cm) distance sensors, they are pretty straightforward to use, 3 pins ( 5v, GND, Analog Output ). As my strength is C# i decided to just output the analogRead every 2 milliseconds and do all the post processing in the computer application.

My Basic Setup:
Arduino Uno with serial cable connected to a PC
Sharp GP2Y0A02YK0F Analog Distance Sensor 20-150cm
3-Pin Female JST PH-Style Cable (30 cm) with Male Pins for 0.1" Housings

My problem:

I have the sensor sitting at around 150 cm from a reflective object, every time someone gets at 125 cm or closer i raise an event inside my app, for this I always keep the last 3 analogReads and get the median value as the actual value and display it (raise the event if obtained median < 125 cm), also from all medians i process i check for the minimum value to check for deviation. I let the program run for a minute or so.

My Case Tests:

#1 ( Works almost as expected )
1 Sensor with the JST cable straight to arduino.
with the sensor at around 150 cm it was reading an avg of 146 cm and had a max deviation of 5 cm and very tight between reads, i will say, like expected.

#2 ( This is what i need but had awful results )
This case i had 2 IR sensors. I connected 5V & GND from arduino to a proto, from the proto to both 5V & GND, the inputs to the analog ports on the arduino, i kept the code for 1 sensor for now as it was the second time with this issue and i was trying to find what was the cause and figured it was not gonna make a difference codewise. The result was the Average moving between 140 - 135 but with a deviation of 30 cm getting reads of 110 more than 1 time ( this will make my code fire, a false positive )

3#
Just changed the configuration of the jumper wires on the proto to keep everything as far away as possible from other connection, this helped a bit from the previous case but not much to make a big difference.

#4
Last case I tried I was back to 1 sensor, but this time instead of connecting the cables straight to the arduino, i added jumper wires in the middle to make the cables longer and then to the arduino, and yes, this made the avg drop a bit, and have a deviation better than #2 & #3 but still unacceptable.

Arduino Code:

#define SENSOR A0

void setup() {
Serial.begin(19200);
pinMode(SENSOR,INPUT);
analogRead(SENSOR); //discard the first read
delay(500); //wait for a bit before start running the loop.
Serial.println("Ready");
}

void loop() {
  float val = analogRead(SENSOR);
  Serial.println(val);
  delay(2);
}

krume300:
Hi guys, I'm a Software developer who has started playing with Arduinos. I'm currently involved in a project where I'm using the Sharp IR (20-150cm) distance sensors, they are pretty straightforward to use, 3 pins ( 5v, GND, Analog Output ). As my strength is C# i decided to just output the analogRead every 2 milliseconds and do all the post processing in the computer application.

What is the drive power on the sensor, you might reduce the jitter with some capacitance on the sense line. Is the cable shielded, or is your sense line acting as an antenna? do you have capacitors at the sense head? how fast (ns) is your signal fluctuating? Can you add a low pas filter network between the sensor head and the ADC?

The real World is not as cut and dried as the Virtual software world. Get ready to get dirty. :slight_smile:

Chuck.

Hi Chuck. thanks for your answer, and yes I already seen that the real world on this stuff aint easy and I'm ready to get my hands dirty.

I'm trying to use two Sharp IR GP2Y0A02YK ( Between 20 and 150 cm ) sensors they require between 4.5 and 5.5v. I got the Arduino connected trough the USB to talk to the computer + an external supply of 13v to keep it properly powered.
I'm powering the breadboard trough the 5v pin and gnd pin from the arduino and both from the bread to the sensors. With the sensor outputs to the A0 and A5 analog. No capacitors, nothing extra on the boards as I'm kinda clueless what should i add even after almost a day of research. The wires aren't shielded so they might pick up noise as i even seen making the wire longer adds noise, but having both sensors at the same time just doesn't work and readings are way to unstable. As i said I'm kinda clueless to add the low pass filter, what should i do? should I add a capacitor between VCC and ground for each sensor or for all together??

Thanks.
Alex.

Your code shows that you are only using A0. Where did A5 come in ?
You should add capacitors. On the power rails, and on the analog inputs (.1 or .01uf).

If you are reading two analog pins now, please show us your code for that.

It may be good to slow down the process, maybe change delay(2); to delay(9);

This will get you started.
Leo…

byte reads = 20; // number of readings for averaging (max 64)
unsigned int total; // total values
float distance; // distance in cm

void setup() {
  Serial.begin(19200);
  Serial.println("Ready");
}

void loop() {
  analogRead(A0); // one unused reading to clear old sh#t
  for (int x = 0; x < reads; x++) { // multiple analogue readings
    total = total + analogRead(A0); // add each value
  }
  Serial.print("Raw A/D value ");
  Serial.print(total / reads); // display average value

  // convert to cm
  // distance = ---your code---;

  Serial.print("   Distance is ");
  Serial.print(distance);
  Serial.println(" cm");
  total = 0; // reset total
  delay(500);
}

Edit:
There are several other posts about using this sensor.

Jack: A5 is where the second sensor is connected, on the analog inputs, currently i'm not doing any reading out of it, as it already alters everything by just being connected. my code doesn't change much

#define SENSOR_1 A0
#define SENSOR_2 A5

void setup() {
Serial.begin(19200);
pinMode(SENSOR_1,INPUT);
pinMode(SENSOR_2,INPUT);
analogRead(SENSOR_1); //discard the first read
analogRead(SENSOR_2); //discard the first read
delay(500); //wait for a bit before start running the loop.
Serial.println("Ready");
}

void loop() {
  float val = analogRead(SENSOR_1);
  delay(2);
  float val2 = analogRead(SENSOR_2);
  Serial.println(String(val) + ":" + String(val2));
  delay(2);
}

Wawa: Thanks for your code but the reads, the distance and other numbers i calculate on the computer end and works fine. the problem comes when i have more than 1 sensor attached to the board,is then when analogRead returns unstable results. with just 1 sensor straight to the board works perfectly.

Did you look at my code.
Discard the first read only works right before the actual read.
Multiple readings averages noise.
Why use float for analogue reads. They are whole numbers/values.
Try this.
Leo…

byte reads = 20; // number of readings for averaging (max 64)
unsigned int total1; // total1 values
unsigned int total2; // total2 values
float distance1; // distance1 in cm
float distance2; // distance2 in cm

void setup() {
  Serial.begin(19200);
  Serial.println("Ready");
}

void loop() {
  analogRead(A0); // one unused reading to clear old sh#t
  for (int x = 0; x < reads; x++) { // multiple analogue readings
    total1 = total1 + analogRead(A0); // add each value
  }
  analogRead(A5);
  for (int x = 0; x < reads; x++) {
    total2 = total2 + analogRead(A5);
  }
  Serial.print("Raw A/D values ");
  Serial.print(total1 / reads); // display average value
  Serial.print(" ");
  Serial.print(total2 / reads);

  // convert to cm
  // distance1 = ---your code---;
  // distance2 = ---your code---;

  Serial.print("   Distance is ");
  Serial.print(distance1);
  Serial.print(" ");
  Serial.print(distance2);
  Serial.println(" cm");
  total1 = 0; // reset total
  total2 = 0;
  delay(500);
}

Leo, i looked at your code. Why float for analog reads is my mistake as i was doing calculations before and left it like float instead of int ( you are right they are always wholes between 0 and 1023 ). On the other hand the post processing as I said I'm doing it on the computer side C# and I'm collecting the last reads ( 5 reads ) and not getting the avg but getting the median as getting an avg can involve a bad read throwing the avg result even more off. As far as i read most of the problems seem to come from the noise side and not code as when only running 1 sensor connected to the board it is pretty accurate and the deviation is not greater than -/+ 5 cm against the bouncing readings and a deviation of 30+ cm when just connecting the wires of a second sensor ( not even with code for doing the reading of the sensor ).

AFAIK, reading the sensors as you do in post#5 is not right.
The first reading of the second sensor could still hold some of the charge of the first sensor. and vise versa.
Maybe try averaging in hardware, and median in software.
Leo..

krume300:
Jack: A5 is where the second sensor is connected, on the analog inputs, currently i’m not doing any reading out of it, as it already alters everything by just being connected. my code doesn’t change much

Can the sensors See each other? They are just optical Sonar. they emit a pulsed IR signal and look for a timed reflection. If you have multiple sensors that can see each others pulses, they will get confused.

Also, the DATA sheet recommend at least a 10uF cap at the sensor.

Chuck.