Detecting obstacle with IR Sensor and Arduino

Detecting obstacle with IR Sensor and Arduino

Are you interested to become an expert of embedded systems? You definitely should know how to deal with ARM Cortex architecture and RTOSes. We have free courses to learn how to deal with STM32 and ChibiOS which would guide you step by step. Ready to do a step further?

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:

#define IR 2   
int detection = HIGH;    // no obstacle
void setup() {
  Serial.begin(9600);   
  pinMode(IR, INPUT); 
}
void loop() {  
  detection = digitalRead(IR);
  if(detection == LOW){
    Serial.print("There is an obstacle!\n");
  }
  else{ 
    Serial.print("No obstacle!\n");
  }
  delay(500);    // in ms
}

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:

#define IR 2    // digital pin input for ir sensor
int detection = HIGH;    // no obstacle
int i = 0;
// array digital pin for: green led(3,4,5) - white led (6,7,8)- red led (9,10,11)
int LedPIN[] = {3, 4, 5, 6, 7, 8, 9, 10, 11};   
 
void setup() {
  pinMode(IR, INPUT);
  for(i = 0; i < 9; i++){
    pinMode(LedPIN[i], OUTPUT);   
  } 
}
void loop() {
  detection = digitalRead(IR);
  if(detection == LOW){ 
    BlinkLED();
  }          
  else{                 
    LedOFF();
  }      
  delay(1);
}

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

Leave a Reply