Introduction: IR Infrared Sensor With Bharat Pi

About: Bharat Pi is an IoT prototyping platform for students, innovators, startups and developers. A simplified board with an all in one compute network and storage on a single board to build rapid prototypes, colleg…

An infrared proximity sensor or IR Sensor is an electronic device that emits infrared lights to sense some aspect of the surroundings and can be employed to detect the motion of an object. As this is a passive sensor, it can only measure infrared radiation. This sensor is very common in the electronic industry and if you’ve ever tried to design an obstacle avoidance robot or any other proximity detection-based system, chances are you already know about this module, and if you don’t, then follow this article as here we will discuss everything about it.

Supplies

IR Sensor

Bharat Pi Boards

https://bharatpi.net/

Jumper wires

Step 1: IR Sensor Pinout

GND pin: connect this pin to GND (OV)

VCC pin: connect this pin to VCC (5V or 3.3v)

OUT pin: is an output pin: LOW if an obstacle is present, HIGH if no obstacle is present. This pin

needs to be connected to ESP32's input pin.

Step 2: How It Works

An infrared obstacle sensor module consists of an IR transmitter and an IR receiver. The

IR transmitter emits the IR signal while the IR receiver searches for the reflected IR signal

to determine if the object is present or not. The presence of obstacle is reflected in the

OUT pin:

If the obstacle is present, the sensor's OUT pin is LOW

If the obstacle is NOT present, the sensor's OUT pin is HIGH

Step 3: Wiring Diagram

Step 4: Code

#define SENSOR_PIN 23 // bharat pi pin GPIO18 connected to OUT pin of IR obstacle avoidance sensor


int lastState = HIGH;  // the previous state from the input pin
int currentState;      // the current reading from the input pin


void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  // initialize the boards's pin as an input
  pinMode(SENSOR_PIN, INPUT);
}


void loop() {
  // read the state of the the input pin:
  currentState = digitalRead(SENSOR_PIN);


  if (lastState == HIGH && currentState == LOW)
    Serial.println("The obstacle is detected");
  else if (lastState == LOW && currentState == HIGH)
    Serial.println("The obstacle is cleared");


  delay(50);
  // save the the last state
  lastState = currentState;
}