I then used a trendline to fit the points and and came up with a polynomial equation: f(x)=polynomial, where f(x)=distance and x=reading(volts). Here's my code (PS the trendline almost fits exact)
int IRpin = 7; // analog pin for reading the IR sensor
void setup() {
Serial.begin(9600); // start the serial port
}
void loop() {
float volts = analogRead(IRpin)*.0048828125;
// value from sensor * (5/1024) for ea. step
float distance = -(3.078pow(volts,5))+(29.645pow(volts,4))-(110.68pow(volts,3))+(201.94(volts,2))-(186.84*pow(volts,1))+81.524;
// calibrated using 5th degree polynomial
I'm getting numbers in the 100-500 range and can't understand why it won't give me distance in cm if I directly calculated the distance using the voltage output.
I also noticed that with different surfaces I got dramatically different readings. I need a sensor to be able to accurately give close proximity readings without having to worry about surface differences. Can anyone help, and is this the best sensor for the job, or is ultrasonic better?
I'm fairly new at this, so i apologize for sounding ignorant to this problem. For this part of my project, I need to accurately measure the distance from an object. I've seen projects where people take the distance output and create a 360 degree schematic of the surrounding environment. I want to take it a step further and make a 3D environment.
I have a known origin that acts as my constant, and I want to use the IR sensor to measure the distance from the object so I can calculate the distance from the origin - ultimately using those numbers to create an object in a 3D cad program. Everything I will be measuring is no more then 5 inches away.
When you ask for RAW data are you asking or the Serial Monitor output or actual measurements compared to the serial output data...
In the meantime I will compare my results with the datasheet, keeping in mind that you're right and that the datasheet can be somewhat misleading.
Right, its a fifth degree polynomial that fits the curve of the line (ignoring the first few "jumpy" points). I know the equation is pretty accurate (assuming the datasheet numbers are accurate). I've been having to do this graph stuff in school now for years. Which makes me believe that the error is in how I coded it or this part:
You're right Rich, it would be dumb to reinvent. I've looked around the WWW for a couple days now and find similar results, none of which are anything close to my goal with this IR sensor. I'll keep looking. I want to thank you guys for replying. Simply reading your responses helps me understand different thought processes.
When i read the volt output, it matches up with my+the datasheet data. meaning that my mistake isn't where i mentioned above, its how im coding the polynomial.
Rugged, Richard,
I found the mistake, and got it to work. If you notice below at the original code I posted, (201.94*(volts,2)) should read (201.94*pow(volts,2)). I fixed that and it gives pretty accurate reading.
I have 3 more of these same sensors I plan on using in conjunction with this one. I will take the data from the 4 different IR sensors, and average them together for a finial reading. Sounds like a lot, and I might only need 2 sensors to get an accurate reading, but can I connect 4 of these sensors to a single Mega board and have them work together?
You're right Paul, I actually caught that mistake before I realized I forgot "pow*" in the operation before that. Thanks though, I did not know that it took up a lot of resources - i just simply fixed it because it was un-needed.