Detecting obstacle with IR Sensor and Arduino

Introduction

An object can be detected with an infrared system consisting of an infrared transmitter and a receiver. More in detail an IR transmitter, also known as IR LED, sends an infrared signal with a certain frequency compatible with an IR receiver which has the task to detect it. There are different kind of IR sensors for different type of application. IR technology is used, for example, in proximity sensors to detect a near object, in contrast sensors to find a path or in counting sensors to count objects.

Principle of operation

IR sensor principle of operation
IR sensor principle of operation with/without object.

The IR transmitter sends an infrared signal that, in case of a reflecting surface (e.g. white color), bounces off in some directions including that of the IR receiver that captures the signal detecting the object.

When the surface is absorbent (e.g. black color) the IR signal isn’t reflected and the object cannot be detected by the sensor. This result would occur even if the object is absent.

IR transmitter and IR receiver

The IR transmitter is a particular LED that emits radiation in the frequency range of infrared, invisible to the naked eye. An infrared LED just works as a simple LED with a voltage of 3V DC and a current consumption of about 20mA. The IR receiver, such as a photodiode or a phototransistor, is capable of detect infrared radiation emitted from the IR transmitter. Aesthetically it is similar to a LED but the external capsule can be wrapped by a dark color film.

IR Sensor FC-51

FC-51 Pinmap
Pin map of the FC-51 sensor.

The sensor used in our demo is model FC-51. It is a cheap sensor easily available on the internet for less than 2$ but unfortunately we didn’t find the datasheet. In any case we will explain the operation of the related electronic circuit and subsequently implement some demo to test its functioning.

 

Pinout and schematic

FC-51 schematic
This is the schematic of the IR sensor FC-51.

The package has three connection pins:

  1. Vcc to the power supply 3-5V DC;
  2. Gnd to the ground reference;
  3. Out for the digital output signal of the sensor.

This sensor detects objects at a distance in range between 2~30cm. With the potentiometer you can calibrate the sensitivity according to the application and environmental conditions (e.g. brightness). The IC LM393 is an open-collector voltage comparator which provides an output if there is a pull-up R between the output of the IC (DO) and the power supply Vcc (R=10KΩ). The output DO is:

  • high if the object is not detected;
  • low if the object is detected.

 

Proposed Demos explained

Test IR sensor FC-51 with serial terminal (Demo 01)

Demo 1 schematic
Demo 1 schematic.

In the first demo, through the connection between the Arduino serial port and the PC, we will read about the detection of the object.

Lets take a look to steps required by this demo:

  1. We connect the OUT pin of the sensor to digital pin 2 of Arduino called IR.
  2. The setup() function is performed only once before the main loop. We insert here the initialization code which enables serial port Arduino and sets the digital pin 2 as input.
  3. loop() is the main function and is cyclically repeated until you turn off the Arduino board. We convert in C language the operation of the electronic circuit analyzed before. We save in the variable detection the value taken from the pin IR with the specific function digitalRead, if the value is low there is an object otherwise there isn’t.

Lets take a look to our code:

IR sensor FC-51 and LED (Demo 02)

Demo 2 schematic
Demo 2 schematic

In this demo we associate an input to each operation state of IR sensor. The required components are:

  • IR sensor FC-51;
  • 3 x Green LEDs;
  • 3 x R=150Ω;
  • 3 x White LEDs;
  • 3 x R=100Ω;
  • 3 x Red LEDs;
  • 3 x R=160Ω.

Remember that the I/O pins can absorb/disburse up to 40mA max, total maximum 200mA (see ATmega328P datasheet at page 313).

Lets take a look to steps required by this demo:

  1. We connect the OUT pin of the sensor to digital pin 2 of the Arduino. We define the digital pins of LEDs as an array of pins, from 3 to 11 called LedPIN.
  2. The setup() function is executed only once before the main loop. In addition to the initialization already seen, we call the 9 LED as output using a for loop.
  3. The loop() function is the main function and is cyclically repeated until you turn off the Arduino board. We save in the variable detection the value taken from pin IR with the specific function digitalRead(). This value can be low, if there is an object, or high if there is no object. We do this loop every millisecond.

Lets take a look to our code:

Download for Arduino IDE

These demos have been tested under Arduino IDE 1.6.7:

DEMO01_IRsensor DEMO02_IRsensorLED

Replies to Detecting obstacle with IR Sensor and Arduino

  • Hi I just wanted to know how to detect like a train passing on a model railway and then initiate the boom gates using my IR obstacle sensor.
    thanks

    • Hi, thanks for visiting us.
      You can follow the principles explained in the demos of this article. You should look for a transiction of the variable Detection from LOW to HIGH or viceversa.

Leave a Reply