How to Interface IR Sensor with Arduino

Introduction:

IR sensor module will have 3pins VCC, GND and D0 respectively.

VCC – +5V DC
GND – Ground
D0 – Digital Out

Requirements:

Arduino UNO – 1
IR Module – 1
Jumper Wires

This module consists of two leds (looks like led but not an led), one is IR Transmitter (i.e., white one) and second is photodiode (i.e., black one). IR Tx will transmits the IR rays(Infrared rays) continuously when module is powered up and photodiode is detectes the reflected IR rays. Here photodiode will detect only objected ir rays, IR rays will be reflect when an object is strike.
So, Module is used to find the object in front the module. IR used in the application of  line following robot and robot with object finding.
If an object is there in front of the IR module, module will produce LOW signal at D0 pin or no object is there then  IR rays won’t objected so, module produce HIGH signal at D0 pin.

Below program is the sample code for object finding using IR module:

  1. #define IR 3
  2. void setup()
  3. {
  4.   Serial.begin (9600);
  5.   pinMode (IR, INPUT);
  6. }
  7. void loop()
  8. {
  9.   int val;
  10.   val = digitalRead(IR);
  11.   if(val)
  12.   Serial.println(“No object detected”);
  13.   else
  14.   Serial.println(“Object Detected”);
  15.   delay(1000);
  16. }
In the first line, declared a macro with the pin number to which we can connect the D0 pin of IR module.
In set up function configuring serial communication with the baud rate of 9600 and initializing ir pin as INPUT.
In loop function reading the sensor value by the digitalRead function, it will return the value either HIGH or LOW and storing in a variable val. Checking val variable with if condition, if val is HIGH condition goes true printing on Serial Monitor as “No Object detected”. if val is LOW then if condition goes false and control goes to the else part and printing on the Serial Monitor as “Object Detected”.

OUTPUT:

No object detected
No object detected
Object Detected
Object Detected

Note: More Information about predefined functions is explained in my previous blog click here.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: