IR Infrared Sensor With Bharat Pi

by bharatpi in Circuits > Arduino

60 Views, 0 Favorites, 0 Comments

IR Infrared Sensor With Bharat Pi

51PWiZcYOqL._AC_UF1000,1000_QL80_.jpg

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

download (1).jpeg

IR Sensor

Bharat Pi Boards

https://bharatpi.net/

Jumper wires

IR Sensor Pinout

IR-Sensor-Pinout.png

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.

How It Works

IR-Sensor-Working.gif

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

Wiring Diagram

Untitled Diagram (12).jpg

Code

Screenshot 2024-04-09 222925.png
#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;
}