An Infrared (IR) Sensor is an electronic device that detects infrared radiation from its surroundings. It is commonly used in various applications such as proximity sensing, object detection, motion detection, and in security systems. IR sensors are popular in robotics for obstacle avoidance and in consumer electronics for remote control functionality.
Pin Name | Description |
---|---|
VCC | Power supply (3.3V to 5V) |
GND | Ground |
OUT | Digital output signal (0V or 5V) |
AO | Analog output (proportional to IR level) |
// Define the IR sensor pin
const int IRSensorPin = 2; // Digital pin connected to the IR sensor's OUT pin
void setup() {
pinMode(IRSensorPin, INPUT); // Initialize the IR sensor pin as an input
Serial.begin(9600); // Start serial communication at 9600 baud rate
}
void loop() {
int sensorValue = digitalRead(IRSensorPin); // Read the sensor value
if (sensorValue == LOW) { // Check if the sensor is detecting an obstacle
// IR sensor has detected an obstacle
Serial.println("Obstacle detected!");
} else {
// No obstacle detected
Serial.println("Path is clear.");
}
delay(200); // Wait for 200 milliseconds before reading again
}
analogRead()
function and calibrate the sensor using known distances to improve accuracy.Q: Can the IR sensor detect color? A: No, IR sensors are not sensitive to color. They detect IR radiation, which is beyond the visible spectrum.
Q: How can I increase the detection range of the sensor? A: You can adjust the potentiometer on the sensor module to increase or decrease the detection range. However, the maximum range is limited by the sensor's design.
Q: Is it possible to use multiple IR sensors together? A: Yes, you can use multiple IR sensors in a project. Ensure that each sensor has its own dedicated input pin on the Arduino UNO and that they are spaced apart to avoid cross-talk.