Introduction :
An IR sensor is an electronic device that measures and detects infrared radiation in its environment. IR sensors are commonly used for object detection, distance measurement, and line following in robotics.
There are different types of IR sensors:
- IR Transmitter and Receiver Pair: Used for object detection.
- IR Proximity Sensor: Measures the distance to an object.
- IR Reflective Sensor: Used in line-following robots.
Pin Connections:
- VCC: Connect to 5V on the Arduino.
- GND: Connect to GND on the Arduino.
- OUT: Connect to a digital input pin on the Arduino (e.g., pin 2)
Code:
#define IR_PIN 2 // Pin connected to the IR sensor OUT pin
void setup() {
pinMode(IR_PIN, INPUT); // Set IR sensor pin as input
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int irState = digitalRead(IR_PIN); // Read the state of the IR sensor
if (irState == LOW) {
// Object detected
Serial.println(“Object detected!”);
} else {
// No object detected
Serial.println(“No object.”);
}
delay(500); // Wait for 500 ms before checking again
}
https://github.com/makertribe/IoT-Codes/blob/68620130d0d5cb97e89ac367d857ce6abbf88895/IR%20sensor
Output :