Home > life is fun, tech > radioshack IR emitter detector distance sensors, arduino calibration

radioshack IR emitter detector distance sensors, arduino calibration


After the realization that this sensor would not be the best for me to use, I opted to use a simple IR Led, and IR detector similar to these:

This setup relied heavily on code to do the work. I based the code off of the Arduion Calibration sketch. I modified the mapping of the sensor values (will note in code) to allow the LEDs to be in an off state until motion was detected by the IR detector. The first run using the unmodified Arduino calibration sketch looked like this:
The

Like I mentioned earlier, the code I used to create the results above was taken directly from an example Arduino sketch:

 /*
  Calibration

 Demonstrates one techinque for calibrating sensor input.  The
 sensor readings during the first five seconds of the sketch
 execution define the minimum and maximum of expected values
 attached to the sensor pin.

 The sensor minumum and maximum initial values may seem backwards.
 Initially, you set the minimum high and listen for anything 
 lower, saving it as the new minumum. Likewise, you set the
 maximum low and listen for anything higher as the new maximum.

 The circuit:
 * Analog sensor (potentiometer will do) attached to analog input 0
 * LED attached from digital pin 9 to ground

 created 29 Oct 2008
 By David A Mellis
 Modified 17 Jun 2009
 By Tom Igoe

 http://arduino.cc/en/Tutorial/Calibration

 */

// These constants won't change:
const int sensorPin = 2;     // pin that the sensor is attached to
const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value

void setup() {
  // turn on LED to signal the start of the calibration period:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds 
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);
}

void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

To gain a better understanding of how the calibration sketch works, I read over an the breakdown from http://arduino.cc/en/Tutorial/Calibration. From there, I looked into more information on the mapping function.  http://arduino.cc/en/Reference/Map

The mapping function was crucial in making my project behave the way it is intended. I switched the last 2 parameters in that function and eventually changed the last parameter from 0 to -255. The IR detector seemed to calibrate better this way.

Here is what I changed:

  // calibrate during the first 10 seconds (initally it was 5)   while (millis() < 10000) {

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 255, -255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

As I worked into the evening, I noticed my hardware started acting up. As the room became darker, the calibration process wasn’t as accurate. I tweaked this code again, resulting in a pretty different mapping scheme that seemed to work better:

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, -500);
I also left the calibration of this sketch at 5 seconds.


The need for a second version of calibration brought to mind the idea for a second input. A photodiode. This would allow the hardware to read the level of light in the room, and use one function over another based on the results.

I used schematics found on a site made by a guy named Charles Martin. He used the same IR emitter/detector to create a heart monitor.

http://cmpercussion.blogspot.com/2009/07/heartbeat-sensor.html

I also referenced the schematics linked from:
http://keywon.com/wiki/index.php?title=Project_002

My project underwent many stages. Here are some photos to illustrate:

Advertisements
Categories: life is fun, tech
  1. Lucas
    May 30, 2012 at 4:59 pm

    can we derive the distance from these simple IR emitter and receiver?

    • June 1, 2012 at 9:36 am

      yup, simple calculation:
      distance = echo_time * sonic_speed/2.

  2. July 27, 2012 at 10:36 am

    I am fairly new to Arduino and electronics and purchased these same IR sensors and can’t seem to get a steady read out with the sensors. Did you find them difficult to use? Do you know if these can go bad or burn out? Also, do you know how far apart you can set these sensors?

  3. Nelson
    February 20, 2013 at 2:41 am

    I am trying to help my sone with a project based on this setup. We have the same IR pair, resistors and wireup but our analog reads have a low sensitivity. We fluctuate at 940-943. I can see the IR led working with my camera and if I pull the analog connection I go to 0. Any ideas how I can adjust this for more range / sensitivity ( Like +/-20 off of a baseline)?

  4. dinho
    November 5, 2013 at 7:20 am

    is this project supposed to dim the led?

  5. dinho
    November 5, 2013 at 7:23 am

    i had a go at it and the LED remains on …. not dimming.. i can see the emitter light is on ( using my phone camera pointing at it) tried the codes you put up here and nothing happens…can you pls help?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: