Hey guys,
Im new to using sensors with Arduino, and I've tried searching around for a library or something that will get accurate readings from my sensor. I've found simple programs that just spit out the values into the serial monitor, but for my program I need one accurate distance.
Do you guys have a library or some code to suggest that would get a more accurate reading? I looked around and the Sharp IR library isn't for my sensor.
Thank you,
Stephen
Q: What is the accuracy you are after?
you often get more accurate readings if you take multiple readings and average them.
Use 4 or 16 measurements, just see at what level the noise is within acceptable range.
if the datatype in the code is not accurate enough e.g. it measures in CM you could redo the code in MM .
Note:
the speed of sound 340 m/s (at some temp/humid) ==> 1 millimetre is done in about 3 microseconds,
Arduino typically measures time in steps of 4 microseconds. .......
Q: Can you post your code so far?
Q: Can you post a link to the datasheet?
I've found simple programs that just spit out the values into the serial monitor, but for my program I need one accurate distance.
So you modify the program to not spit out values to the serial port but take that value and use it for your application what ever it is.
Accuracy is limited to the IR reflectance of the surface and the resolution of the A/D converter in the Arduino. As the voltage out of that sensor does not go over 3V then feeding the reference voltage with 3V3 will increase the accuracy of the readings. Make sure you tell the code you are using an external Vref before you take a reading with 3V3 attached to the Vref input.
robtillaart:
Q: What is the accuracy you are after?
you often get more accurate readings if you take multiple readings and average them.
Use 4 or 16 measurements, just see at what level the noise is within acceptable range.
if the datatype in the code is not accurate enough e.g. it measures in CM you could redo the code in MM .
Note:
the speed of sound 340 m/s (at some temp/humid) ==> 1 millimetre is done in about 3 microseconds,
Arduino typically measures time in steps of 4 microseconds. .......
Q: Can you post your code so far?
Q: Can you post a link to the datasheet?
here is the datasheet http://www.sharpsma.com/webfm_send/1487
The code I was using was directly from this website, I would have written something myself but I needed to quickly test the sensor for the upcomming project. http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/
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.
}
I need it to be accurate to within 5 cm, but with the current code I wasnt reaching that goal.
How can I improve on this code to get it more accurate?
Thanks again,
Stephen
stephencampbell:
I need it to be accurate to within 5 cm, but with the current code I wasnt reaching that goal.
How can I improve on this code to get it more accurate?
By fitting a better line to the curve.
(Or switching to an ultrasound sensor, where there is a clearer relationship between time and distance.)
Edit.... look how flat the curve is from about a metre out: very little change in volts for significant change in distance. You can't expect any real accuracy there.
step one - add average
int IRpin = 1; // analog pin for reading the IR sensor
void setup()
{
Serial.begin(9600); // start the serial port
}
void loop()
{
float sum = 0;
for (int i=0; i<16; i++)
{
sum += analogRead(IRpin);
}
float volts = sum/16 * 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);
delay(100);
}
is 5V 5.000volt?
The second issue is that you assume you have 5.0 volts.
you should check the voltage of the IR sensor with a DMM to see if it really is 5.0 volts.
If it is 4.9 or 5.1 you will have a structural error.
There are ways to let the Arduino measure the voltage over the sensor, bit more important is the
ANALOG REFERENCE
Note the max voltage the sensor gives is about 3 volt. If you use the AnalogReference pin and set this to 3V (voltage divider) the accuracy will improve by about 40% (1 step is not 0.0048... volt but 0.002932551 Volt . This means the distances are also in smaller steps, which is very useful on the right side of the curve.
Example: the diff between a raw measurement of 100 and 101 :
5V
volts = 100 * 0.0048828125; = 0.48828125 distance = 65pow(volts, -1.10); = 143.013
volts = 101 * 0.0048828125; = 0.493164062 distance = 65pow(volts, -1.10); = 141.456
delta = 1.557 cm
3V (raw reads would be higher
volts = 166 * 0.002932551; = 0.486803466 distance = 65pow(volts, -1.10); = 143.490
volts = 167 * 0.002932551; = 0.489736017 distance = 65pow(volts, -1.10); = 142.546
delta = 0.945 cm
robtillaart:
step one - add average
int IRpin = 1; // analog pin for reading the IR sensor
void setup()
{
Serial.begin(9600); // start the serial port
}
void loop()
{
float sum = 0;
for (int i=0; i<16; i++)
{
sum += analogRead(IRpin);
}
float volts = sum/16 * 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);
delay(100);
}
is 5V 5.000volt?
===============
The second issue is that you assume you have 5.0 volts.
you should check the voltage of the IR sensor with a DMM to see if it really is 5.0 volts.
If it is 4.9 or 5.1 you will have a structural error.
There are ways to let the Arduino measure the voltage over the sensor, bit more important is the
ANALOG REFERENCE
================
Note the max voltage the sensor gives is about 3 volt. If you use the AnalogReference pin and set this to 3V (voltage divider) the accuracy will improve by about 40% (1 step is not 0.0048... volt but 0.002932551 Volt . This means the distances are also in smaller steps, which is very useful on the right side of the curve.
Example: the diff between a raw measurement of 100 and 101 :
5V
volts = 100 * 0.0048828125; = 0.48828125 distance = 65*pow(volts, -1.10); = 143.013
volts = 101 * 0.0048828125; = 0.493164062 distance = 65*pow(volts, -1.10); = 141.456
delta = 1.557 cm
3V (raw reads would be higher
volts = 166 * 0.002932551; = 0.486803466 distance = 65*pow(volts, -1.10); = 143.490
volts = 167 * 0.002932551; = 0.489736017 distance = 65*pow(volts, -1.10); = 142.546
delta = 0.945 cm
Because of my lack of knowledge, I don't quite understand what you said.
I understand this sounds very lazy, but are there any good prewritten programs getting distances from my sensor? It seems inefficient for me, considering I'm a huge noob to try to write this part of the program myself.
Thanks,
Stephen
- try the code example I provided to see if the results are more stable.
- measure the voltage of the sensor to see if it is exact 5.00Volts
If you don't have a digital multimeter you should buy one asap.
Even the less tahn 10$ ones are good enough to start.
robtillaart:
- try the code example I provided to see if the results are more stable.
- measure the voltage of the sensor to see if it is exact 5.00Volts
If you don't have a digital multimeter you should buy one asap.
Even the less tahn 10$ ones are good enough to start.
Your code works beautifully, a multimeter is ordered and on its way!
Thank you so much,
Stephen
robtillaart:
- try the code example I provided to see if the results are more stable.
- measure the voltage of the sensor to see if it is exact 5.00Volts
If you don't have a digital multimeter you should buy one asap.
Even the less tahn 10$ ones are good enough to start.
Sorry to bring back this old post,
But I cant get values closer than around 20 cm. Once I put an object closer than 20cm to the sensor, it starts going up all the way to around 50 cm and higher. Is there anyway to fix the closer readings accuracy?
Thanks,
Stephen
20cm is the specified near-limit in the datasheet.
Is there anyway to fix the closer readings accuracy?
Look at the distance / voltage curve. You could have another sensor that roughly detects the distance and uses that to decide what side of the curve you are on.
Otherwise there is nothing you can do with the sensor you have to change the characteristics of it.
Grumpy_Mike:
Is there anyway to fix the closer readings accuracy?
Look at the distance / voltage curve. You could have another sensor that roughly detects the distance and uses that to decide what side of the curve you are on.
Otherwise there is nothing you can do with the sensor you have to change the characteristics of it.
By another sensor what do you mean? What type of sensor?
Thanks again,
Stephen
By another sensor what do you mean?
Well you have one sensor, now you need an other sensor.
What type of sensor?
Any sort you want.
The problem with what you have got it the minimum range and the downturn of the graph on shorter ranges. You want to work at less than the minimum range of the sensor, it is called the minimum range for a good reason. For any voltage reading you have two potential distances. Use another sensor to tell you which of those two readings is the true one.
This could be another sensor of the same type only mounted further away so it is working on the unambitious side of the curve. Or it could be something like an ultrasonic sensor that by itself is not accurate enough for you by itself.