I have obtained a new sharp IR sensor to measure from range 10 to 80 cm for my project,
upon connected the sensor as designated in the datasheet and used various codes to measure distances ,
I can tell from the readings has a maximum for 30 cm but not 80 cm as declared in the data sheet, as after 30 cm it gives no more than 30 CM.
here is an example of the used code
#define ir A5
void setup() {
Serial.begin(9600);
}
void loop() {
float val = analogRead(ir)*0.0048828125;
int distanceX = 29.988*pow(val,-1.173);
//Serial.println(val);
Serial.println(distanceX);
delay(1000);
}
and here are example of the readings at different distances above 30 CM
Please post a link to the sensor product page and state which Arduino you are using.
The maximum range depends on the reflectivity of the object, which should be a smooth flat surface. What are you using as a target?
Looking at the graph (below), taken from the GP2Y0A21YK0F data sheet (if that is what you have) , I would be very skeptical of the accuracy of a formula like that in your code.
the object is a flat white box, and I'm using Arduino nano , and this is the exact sensor I have.
if you have better formula or code you could post it here.
However, in order to have accurate voltage readings, the ADC on the Classic Nano must be calibrated to the actual ADC reference voltage, which is usually Vcc and can be anywhere from 3.3V to over 5V. Which of the 6 or so "Arduino Nano" versions do you actually have?
The formula is same as used in library and seems giving the same result as in the GP2Y0A21YK table.
For every conversion try printing also the analogread result and the val ( and measuring with a multimeter you should se output dropping below 1V for distancies greater than 30cm )
Are you powering the sensor with a good 5V supply?
Ok, but measure with a multimeter the voltage on the 5volt supplied by arduino.
I say that because if you power the arduino via usb the voltage on the 5V pin you have the usb voltage less a diode drop, and it should be in the 'range of 4.5V' if the usb voltage is 5V ( or even lower is your usb voltage is less than 5V ).
The minimum voltage for the sharp diode is 4.5V, so it could be you are actually powering it at a voltage below the minimum guaranteed functioning voltage
So a safer solution could be powering the arduino via Vin ( at least 7V, to take in account for the drop of the regulator ), and you should have a steady 5V to power the sensor.
Or even better using a separete 5V supply only for the sensor ( you need to connect both negatives/gnd together ).
@davidefa
Sure I'm using a usb to power the Nano , where it powers the sensor from the 5v pin on the Nano.
I've used a voltmeter to measure the difference between the 5v pin and the ground pin
and it gave me 4.36 volt below the required 4.5 volt .
So what are my options now knowing that the my aduino board have other soldered components ?
#include <SharpDistSensor.h>
// Analog pin to which the sensor is connected
const byte sensorPin = A5;
// Window size of the median filter (odd number, 1 = no filtering)
const byte medianFilterWindowSize = 5;
// Create an object instance of the SharpDistSensor class
SharpDistSensor sensor(sensorPin, medianFilterWindowSize);
void setup() {
Serial.begin(9600);
// Set sensor model
sensor.setModel(SharpDistSensor::GP2Y0A21F_5V_DS);
}
void loop() {
// Get distance from sensor
unsigned int distance = sensor.getDist();
// Print distance to Serial
Serial.println(distance);
// Wait some time
delay(1000);
}
Looks like you have bad soldering/wiring (possible open analog input), or a bad sensor. You won't get anywhere with such large measurement variations. Post a close up, focused picture of your setup.
as you can see the IR sensor is a part of a project to measure weight and height and the wires from the sensors are connected to the the ground , 5volt , and A5 wires from The Nano Board .
as I Measured the voltage running the sensor from the Nano board seems to be less than the required 4.5 volt necessary to power the sensor .
I used a battery pack of 3 AAA batteries to supply more than 4.5 volt as you can see here.
as you can see when connecting the sensor to the battery pack resulted in completely faulty measurements
I also I found the in the documentation that a 10 micro farad capacitor is needed between the ground and the power source to stabilize the power supply to the sensor,
I found this one in the utility store nearby, is the the correct one and how to wire it
If the measures you posted was taken with the box in fixed position I'd say there is something 'unstable', wondering if these variations are in the sensor output or 'in the acquisition' ?
So using the multimeter do you have stable readings that correspond to the table below ( that should represent the device response )?
The battery pack you are using is good in the fact it is stable and has no 'switching noise' ( as could have a switching regulator ), but I'd try also with a proper 5V supply.
The capacitor you have is good ( till 250V, more than acceptable ; - ), connect between gnd/vcc of the sensor ( near the sensor ), it is polarized, so the 'white striped -' to gnd ( add also, if you have, a 100nF capacitor in parallel to the polarized one )
Sounds like the battery pack is the problem. Electrically, they can be very noisy, and that is a problem for any sensor. I suggest to change to a stable, well regulated power source.
Breadboards are also unreliable.
Please post a close up, focused photo of the Arduino and the sensor wiring.
@drmina2023
I've used that sensor. It works very well with a regulated 5V supply. I soldered a small 10uF tantalum capacitor to the backside of the connector. Had no trouble getting the 10cm-80cm range but it MUST be linerized.
One thing not explained in the data sheet is that the output occurs in discrete voltage steps, it is not continuous. I have to look but it may have only been 256 steps
I used a large target when I linearized because since it's IR it's hard to know where it's pointing