Infrared (IR) Sensor

In this project, you'll learn how to use an infrared (IR) sensor to either detect an object, or to distinguish between a black and a white color object.

The IR sensor can be used by a robot to avoid hitting a wall when it is configured for object detection, or used in a line tracing robot to follow either a black or white line on the floor when it is configured for black/white color detection.

Parts needed:
  • Arduino
  • An infrared (IR) sensor
  • Wires
IR sensor     
1 In order for the IR sensor to work correctly, you will first need to calibrate it. First, identify the LED that can be turned on and off by turning the potetiometer. This is the LED used in the calibration below.

Calibration for object detection:
  • The IR sensor can detect an object that is less than about 9 inches away.
  • Point the IR sensor to a wall that is about 2 feet away.
  • Turn the potentiometer counterclockwise until the LED on the sensor just turns off.
  • The green LED will turn on when an object is placed less than about 9 inches away.
Calibration for black/white color detection:
  • Mount the IR sensor about 1 to 2 inches above a black surface.
  • Turn the potentiometer counterclockwise until the LED on the sensor just turns off.
  • Do not change the distance between the IR sensor and the black surface. If you need to change the distance then you need to re-calibrate.
  • The LED will turn on when the sensor is above a white surface and off when it is above a black surface.
2 Making the connections
  • Connect VCC on the IR sensor to 5V on the Arduino.
  • Connect GND on the IR sensor to GND on the Arduino.
  • Connect OUT on the IR sensor to pin 6 on the Arduino.
3 Create a new Bare Minimum program and type in this program.





The OUT signal from the IR sensor is 0 when the sensor LED is on showing that it is receiving a reflected IR signal (white), and 1 when the LED is off showing that it is not receiving a reflected IR signal (black).

So to turn on the LED on pin 13, we need to invert the signal by using !v.
void setup() {
  pinMode(13, OUTPUT);
  pinMode(6, INPUT);
}

void loop() {
  int v = digitalRead(6);
  digitalWrite(13, !v);
  delay(50);
}
4 The internal LED connected to pin 13 on the Arduino will turn on and off just like the green LED on the IR sensor.