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);
}