Connecting IR sensor to arduino board

Hello,
I am new to arduino, so if i posted something non-technical or asked stupid questions, i apologize first.
I have been reading arduino forums to understand more about arduino but it seems that it doesn't help me much coz there are loads of information so i don't know where i should start to read

Here are a few questions i would like to ask:

  1. Currently, i have an IR sensor ( SHARP 2Y0A02 ) to connect to ardupilot mega board and program it to detect the distance, but i have no idea how to program it. Can i use ping from the library?
    I did some research about how an IR sensor works, but i can't relate it to the programming part. So i m wondering if anybody out there can share some codes for programming the IR sensor?
    Is the signal pin from IR sensor connected to analog pin of arduino board?

  2. I wanna know what serial.println function does. i see some people using them in their codes. i used them once but i notice there is no change in the result when i m including serial.println and not including it.

Thanks in advance for anybody helping me to solve these questions. :slight_smile:

Hi Richard,
I did some researches in the meanwhile, and came to understand a bit in how to program. i tried some of the codes i found in google but the numbers in serial monitor are not showing me the actual distance when i vary the distance. Instead it keeps repeating some of the numbers. Here is one of the codes i tried:

int IRpin= 1;
int val = 0;

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

void loop ()
{
val= analogRead( IRpin );
long distance= 9462/ (val-16.92); // i get this equation from http://www.phidgets.com/products.php?product_id=3522

Serial.println (val);
delay(100);

}

Can you tell me what went wrong with this code?

here is another code i found :
The programmer stated that he used excel to get the distance relative to voltage but i was just wondering since we have already got the equation for distance vs. sensorvalue, why do we need to convert find voltage value and convert it to distance?

int IRpin = 1; // analog pin for reading the IR sensor

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

void loop() {
float volts = analogRead(IRpin)0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65
pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance); // print the distance
delay(100); // arbitary wait time.
}

Thanks in advance for your help

HI Richard,
i m sorry if i didn't provide enough information in my previous questions, and apologize first if i do not answer what u expect me to in this reply.

I used multimeter to measure the varying voltages and it worked as stated as in the datasheet, i.e. 2.5V when it's about 20cm, and the voltage decreases as the distance increases. So, i assume that my sensor works well.

I also figured out what i did wrong in the previous experiment. When i connected my sensor to arduino, the mux light which is orange didn't light up. i supposed it was the connection problem( maybe the wire loosens in some places) and when i tried today again, i am able to see all three lights, and my sensor seems to work now.

It reads appro.20 when it is about 20cm away, but below 20 cm, the sensor doesn't give the actual value. sometimes, it shows correct distance( 18, 17, but sometimes it shows 40, 37 ). i guess it's because the sensor is stated to work well in the range of 20-150? So i suppose it doesn't work for anything less than 20cn? Am i correct to say that?
Another thing i found out is that the sensor's reading is quite unstable as in if the reflected object's surface area is small and the distance to detect is large, it gives reading of 134 instead of around 60 ,but if i use bigger object to detect and move stably, the sensor seems to read correct distances.

So, overall, i suppose my sensor is working and i m seeing the values that i expect to see which is distance value.

Thank you very much for your help so far. :slight_smile:

i guess it's because the sensor is stated to work well in the range of 20-150? So i suppose it doesn't work for anything less than 20cn? Am i correct to say that?

If the datasheet says 20-150 you shouldn't really expect sensible values outside that range.

Datasheet: http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y0a02_e.pdf

There are other sensors in that family that support different ranges (eg 4-30 & 10-80). Maybe they are a better fit for your application? Or maybe the Ping))) style ultrasonic sensors would be better as they have a wider range. I'm using one currently that will happily measure down to a few cm, and up to a few metres no problem.

And any sensor - be it IR or ultrasonic will have more difficulty the smaller the target becomes. If that's a concern open your wallet and look for a laser based solution (ker-ching!)

G.

actually i m using this sensor just for learning purpose. Frankly speaking, i do not know how an infrared sensor works before i mess around with it. :-[

I m hoping to learn more about sensors and my next step would be using an ir sensor to activate something such as lit up an LED ? I may have a lot of questions to ask and hope you guys can help me in solving my questions in the future.. :slight_smile:

Thanks in advance. =D

i do not know how an infrared sensor works before i mess around with it.

Then do some googling, this is a good link:-

yep..i have come across with that page while i was trying to get my sensor work.

Anyway, thanks for yr link . :slight_smile:

So what did it say that you didn't understand or what dis it not say that you want to know?

ooops..sorry...i mean i didn't understand about sensors until i started playing with them. I googled a lot to understand more about them.

Sorry if my English confused you.