Complete guide to infrared sensors for proximity, reflectance, and object detection
IR sensors detect infrared light, typically used for proximity sensing, object detection, and reflectance measurement in the 850nm-950nm wavelength range.
| Parameter | Standard Module | Reflective | Break Beam |
|---|---|---|---|
| Detection Method | Reflective (object reflects IR back) | Reflective (surface reflectance) | Beam interruption |
| Typical Range | 2cm-30cm | 0.5cm-5cm | 5cm-5m |
| Output Type | Digital | Analog/Digital | Digital |
| Accuracy | Medium | High (for surface detection) | Very High |
| Price Range | $0.50-$2 | $1-$5 | $5-$20 |
// Basic IR Sensor Example
#define IR_SENSOR_PIN 2
void setup() {
Serial.begin(9600);
pinMode(IR_SENSOR_PIN, INPUT);
}
void loop() {
int sensorState = digitalRead(IR_SENSOR_PIN);
if(sensorState == LOW) {
Serial.println("Object detected!");
} else {
Serial.println("No object detected");
}
delay(100);
}
// Analog IR Sensor Example
#define IR_ANALOG_PIN A0
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(IR_ANALOG_PIN);
Serial.print("IR Reflectance: ");
Serial.println(sensorValue);
// Adjust threshold based on your sensor and surface
if(sensorValue > 500) {
Serial.println("Light surface detected");
} else {
Serial.println("Dark surface detected");
}
delay(200);
}
// IR Sensor Object Counter
#define IR_PIN 2
int objectCount = 0;
bool lastState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
pinMode(IR_PIN, INPUT);
}
void loop() {
int reading = digitalRead(IR_PIN);
// Debounce the input
if(reading != lastState) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
if(reading == LOW && lastState == HIGH) {
objectCount++;
Serial.print("Object count: ");
Serial.println(objectCount);
}
}
lastState = reading;
}
Use multiple IR sensors to follow a black line on white surface.
Count objects passing on a conveyor belt using IR break beam.
Alert when objects get too close to sensitive equipment.
Automatically open lid when hand is detected.
Measure and compare different material surfaces.
Use IR receiver to decode TV remote signals.
// Components needed:
- 5x IR sensors (array)
- Arduino/ESP32
- Servo motors
- OLED display
- Power supply
// Features:
1. Detect hand gestures (left, right, up, down)
2. Control devices based on gestures
3. Visual feedback on OLED
4. Calibration mode
5. Multiple control modes
// Implementation Tips:
- Arrange IR sensors in cross pattern
- Implement gesture recognition algorithm
- Add debounce for reliable detection
- Create calibration routine
- Add visual feedback for user
- Consider wireless control option
| Mistake | Consequence | Solution |
|---|---|---|
| Improper calibration | False detections or missed objects | Calibrate in actual operating environment |
| Ignoring ambient light | Unreliable operation | Shield sensor or account in software |
| No signal processing | Noisy, unstable readings | Implement filtering and debouncing |
| Wrong sensor type | Poor performance | Choose sensor appropriate for application |
| No maintenance | Degraded performance over time | Regularly clean and check calibration |