1

I have connected an Infrared sensor to my Arduino Uno and I've set this code up to blink whenever a distance of less than 50 cm is picked up. If there is an object less than 50 cm away, it does pick it up. However, with nothing in front of it it still blinks at random intervals. The sensor is new. Where do you think the problem is?

int sensorPin = 0;
int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  float distance = 12343.85 * pow(analogRead(sensorPin),-1.15);
  if(distance < 50){ 
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  } 
  delay(50);
}
  • 3
    Maybe some specks of dust, or just electrical noise. You could try averaging the measured value. What I generally do is take 10% of the current value, and 90% of the old averaged value. – Gerben Dec 15 '15 at 16:28
  • 1
    Have you read this reference? bildr.org/2011/03/various-proximity-sensors-arduino. It explains some of the issues with this sensor. It is great for a closed box with no "extra light". It is easy to disturb the sensor with IR from a remote or just sun light. – Mikael Patel Dec 15 '15 at 20:03
1

I see one problem with your code and how it relates to this device.

The output from the sensor is not the monotomic function you describe; not over its entire range. Your conversion function describes only a section of the response. The device is only intended to be used when an object is in range 10-80 cm.

You should check that the voltage at the analog input is valid before performing the math:

int v;

v = analogRead(sensorPin);
// sensor is valid in range 0.4 to 2.5 V
if((82 < v) && (v < 512)) {
  // voltage is in usable range, can convert
  distance = 12343.85 * pow(v,-1.15);
  if(distance < 50) { 
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  }
}
else {
  // no valid reading - probably just skip and try again on next loop
}

With that the false triggers may be eliminated.

  • Scaling to voltage is unfortunately missing; float voltage = sensorValue * (5.0 / 1023.0); arduino.cc/en/Tutorial/ReadAnalogVoltage – Mikael Patel Jan 2 '16 at 20:49
  • I am presuming that OP's formula does convert ADC counts to distance. But I have corrected that the conditional is in counts. – jdr5ca Jan 5 '16 at 8:56
1

Looks to me like there could be a signficant figures issue. Arduino can really only handle 7 digits of precision. You hit that right at your first constant in your distance variable, then you multiply by an integer to a power of -1.15. It's likely there are errors there that trip to less than 50, which would give you false positives.

As for the Sharp IR sensors, just buy an IR photodiode and LED for less than $1 USD and build and calibrate your own IR detector. You can calibrate it to a range of about 1 m. That's my recommendation. I did something similar after being frustrated by the cost of the Sharp IR Detectors. Wrote about it on my blog, if you're interested:

https://engineersportal.com/blog/2018/1/17/infrared-obstacle-detector-for-less-than-1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.