Infrared Obstacle Avoidance Sensor Tutorial

Infrared Sensor Tutorial with Arduino and NodeMCU

This is a very popular infrared sensor since it is easy to use and very cheap. Most Arduino learning kit even has this sensor. This sensor can detect objects or obstacles in front of it. I have written many tutorials about sensors. You can find it here.

How This Sensor Works

It has 2 LEDs, the transparent one is the LED that transmits infrared light. And the black LED is the photodiode that can sense the IR light.
When it is powered up, it always transmits the light. So when there is an object in front of the LED, the infrared light will bounce back. And when the photodiode receives the infrared light it will give a signal to the circuit.
In this circuit, there’s a comparator, when the signal is above the threshold, the circuit will give output LOW. When no signal or the signal is below the threshold it will output HIGH.


We can adjust the threshold or the sensitivity or the distance of the sensor by rotating the trimpot or potentiometer on the circuit. To make the sensor detect farther we should rotate to right, to make it closer you can adjust the trim-pot to left. To use this with a microcontroller like Arduino, we can easily just connect the sensor pins to our board.
It has 3 pins, the VCC, GND and also Signal.

Infrared Sensor Wiring with Arduino and Little Experiment

Infrared Sensor with arduino
Infrared Sensor with arduino

After a little experiment, I found that this sensor depends on how the object reflects the light back to the sensor. Black objects tend to absorb more than reflect it back, and so is the white paper and glossy metal. It is more reflective, so at longer distances, the sensor already got reflected. So you need to be more careful with the use of this sensor. Different materials and colors can affect the distance of the sensor.

And also when this sensor is outdoors, it always receives infrared light from the sun, so it like always detecting obstacles. This means this sensor cannot be used outdoors. For more clear explanation, you can watch the video.

Adjusting The Obstacle Distance

If you want to detect objects farther, you need to rotate the trimpot on the sensor to right. When you want to detect at closer range, you need to rotate the trimpot to left.

Infrared Sensor Code with Arduino

I modify the arduion example code of digitalRead. This code will gives us 0 on the serial monitor when an obstacle detected, and will gives us 1 when no obstacle detected.

/*
 Infrared Sensor Digital Read Example by miliohm.com
*/

// digital pin 2 has a Infrared attached to it.
int IR = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(IR, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int IRState = digitalRead(IR);
  Serial.println(IRState);
  delay(1);        // delay in between reads for stability
}

Wiring Diagram with a NodeMCU/ESP8266

Using the NodeMCU or another board is the same way. In this example, I use a NodeMCU too.

Infrared Sensor with NodeMCU ESP8266
Infrared Sensor with NodeMCU ESP8266

Infrared Sensor Code With NodeMCU

The only difference between this code and the Arduino is the pin name, in this example, I attached the sensor to pin D3 of NodeMCU.

/*
 Infrared Sensor Digital Read Example by miliohm.com
*/

// digital pin D3 has a Infrared attached to it.
int IR = D3;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(IR, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int IRState = digitalRead(IR);
  Serial.println(IRState);
  delay(1);        // delay in between reads for stability
}

This code also print 0 when a obstacle detected and 1 if nothing detected.

Turn ON and OFF Lamp with Infrared Sensor

I made a simple example project with this sensor, this project will trigger a relay that has a lamp as a load. So when something is in front of the sensor. The lamp will be turned on.

Infrared Sensor Trigger Lamp Schematic

Infrared Sensor to Trigger lamp with arduino and relay
Infrared Sensor to Trigger lamp with arduino and relay

Infrared Sensor Trigger Lamp Arduino Code

/*
 Infrared Sensor Trigger Lamp Example by miliohm.com
*/

// digital pin 2 has a Infrared attached to it. And relay on pin 3.
int IR = 2;
int relay = 3;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(IR, INPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int IRState = digitalRead(IR);
  Serial.println(IRState);
  if (IRState==0){
    Serial.println("Hey move out!");
    digitalWrite(relay, LOW);
  } else {
    digitalWrite(relay, HIGH);
  }
  delay(1);        // delay in between reads for stability
}

Infrared Trigger Lamp with NodeMCU

Infrared Sensor Trigger Relay NodeMCU ESP8266
Infrared Sensor Trigger Relay NodeMCU ESP8266
/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int IR = D3;
int relay = D4;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(IR, INPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int IRState = digitalRead(IR);
  Serial.println(IRState);
  if (IRState==0){
    Serial.println("Hey move out!");
    digitalWrite(relay, LOW);
  } else {
    digitalWrite(relay, HIGH);
  }
  delay(1);        // delay in between reads for stability
}

So that’s it, the tutorial on how to use the Infrared Sensor with Arduino and ESP-based boards like NodeMCU. And also a simple example project using the sensor.

Here’s the complete video on this tutorial.