0
0
AutocadHow-ToBeginner · 3 min read

How to Use IR Sensor with Arduino: Simple Guide and Example

To use an IR sensor with Arduino, connect the sensor's power, ground, and output pins to the Arduino. Then, read the sensor's output using analogRead() or digitalRead() in your code to detect objects or measure distance.
📐

Syntax

An IR sensor typically has three pins: VCC (power), GND (ground), and OUT (signal output). Connect VCC to 5V, GND to ground, and OUT to an Arduino input pin.

In code, use pinMode(pin, INPUT) to set the sensor pin as input. Then use digitalRead(pin) for digital sensors or analogRead(pin) for analog sensors to get the sensor value.

arduino
const int irPin = A0;  // IR sensor output connected to analog pin A0

void setup() {
  Serial.begin(9600);          // Start serial communication
  pinMode(irPin, INPUT);       // Set IR sensor pin as input
}

void loop() {
  int sensorValue = analogRead(irPin);  // Read sensor value
  Serial.println(sensorValue);           // Print value to serial monitor
  delay(500);                           // Wait half a second
}
💻

Example

This example reads the IR sensor's analog output and prints the value to the Serial Monitor. When an object is near, the value changes, allowing you to detect presence or distance.

arduino
const int irSensorPin = A0;  // Connect IR sensor output to analog pin A0

void setup() {
  Serial.begin(9600);          // Initialize serial communication
  pinMode(irSensorPin, INPUT); // Set sensor pin as input
}

void loop() {
  int irValue = analogRead(irSensorPin);  // Read analog value from IR sensor
  Serial.print("IR Sensor Value: ");
  Serial.println(irValue);                 // Print the value
  delay(300);                             // Small delay for readability
}
Output
IR Sensor Value: 512 IR Sensor Value: 510 IR Sensor Value: 515 ... (values change when object moves near sensor)
⚠️

Common Pitfalls

  • Incorrect wiring: Make sure VCC is connected to 5V and GND to ground; reversing these can damage the sensor.
  • Wrong pin mode: Always set the sensor pin as INPUT in setup().
  • Using digitalRead on analog sensor: Use analogRead() for analog IR sensors to get meaningful values.
  • Ignoring sensor calibration: IR sensor values vary by environment; test and calibrate thresholds for your use case.
arduino
/* Wrong way: Using digitalRead on analog pin */
const int irPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(irPin, INPUT);
}

void loop() {
  int val = digitalRead(irPin);  // Incorrect for analog sensor
  Serial.println(val);
  delay(500);
}

/* Right way: Use analogRead for analog sensor */

// int val = analogRead(irPin);  // Correct usage
📊

Quick Reference

IR Sensor Pins:

  • VCC: Connect to 5V on Arduino
  • GND: Connect to Arduino ground
  • OUT: Connect to Arduino analog or digital input pin

Arduino Functions:

  • pinMode(pin, INPUT); - Set sensor pin as input
  • analogRead(pin); - Read analog sensor value (0-1023)
  • digitalRead(pin); - Read digital sensor value (0 or 1)

Tips: Use Serial Monitor to see sensor values and adjust your code accordingly.

Key Takeaways

Connect IR sensor VCC to 5V, GND to ground, and OUT to an Arduino input pin.
Use pinMode to set the sensor pin as input before reading values.
Use analogRead for analog IR sensors to get detailed readings.
Check sensor values on Serial Monitor to understand and calibrate detection.
Avoid wiring mistakes and use correct reading functions to prevent errors.