In this lesson you will learn how to use an infrared (IR) sensor module with your ESP32. The IR sensor detects nearby objects by emitting infrared light and measuring its reflection. This is useful for simple obstacle detection and proximity sensing.
What you will learn
- How an IR sensor works
- How to wire the IR sensor to the ESP32
- How to read digital input from the sensor
- How to trigger an LED or buzzer when an object is detected
Components needed
Get All the Parts You Need
This tutorial is part of our comprehensive ESP32 learning series. Instead of buying components individually, save time and money with our ESP32 Basic Starter Kit. It includes everything you need for this lesson and 20+ other projects.
What's Included: ESP32 board, OLED display, sensors (DHT11, PIR, LDR), relay module, buzzers, LEDs, buttons, breadboard, resistors, and all cables, plus access to our complete lesson plans.
View ESP32 Starter Kit →Step 1 - Understanding the IR sensor
The IR sensor module has three pins: VCC, GND, and OUT. It works by emitting infrared light from an IR LED. When an object is placed in front of the sensor, the light reflects back and is detected by the receiver.
When an object is detected, the OUT pin goes LOW. When no object is present, the OUT pin stays HIGH.

Step 2 - Wiring the IR sensor and LED
- Place the IR sensor on the breadboard
- Connect VCC to 3.3 V on the ESP32
- Connect GND to GND on the ESP32
- Connect the OUT pin to GPIO 23 on the ESP32
- Place the LED on the breadboard
- Connect a 220 Ω resistor to the LED anode (long leg)
- Connect the other end of the resistor to GPIO 2
- Connect the LED cathode (short leg) to GND
Step 3 - Writing the code to detect objects
Open Arduino IDE, enter and upload the following code. The LED turns on when an object is detected and off when no object is present.
int irPin = 23; // GPIO connected to IR OUT int ledPin = 2; // GPIO connected to LED int irState = 0; void setup() { pinMode(irPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(115200); } void loop() { irState = digitalRead(irPin); // Read IR sensor if (irState == LOW) { digitalWrite(ledPin, HIGH); // Turn LED on Serial.println("Object detected"); } else { digitalWrite(ledPin, LOW); // Turn LED off Serial.println("No object"); } delay(200); }
How the Code Works
This program reads the IR sensor and turns the LED on when an object is detected.
int irPin = 23;This line stores the GPIO pin number connected to the IR sensor output.
int ledPin = 2;This line stores the GPIO pin number connected to the LED.
int irState = 0;This variable stores the current state of the IR sensor.
void setup()This function runs once when the ESP32 starts.
pinMode(irPin, INPUT);Sets the IR sensor pin as an input.
pinMode(ledPin, OUTPUT);Sets the LED pin as an output.
Serial.begin(115200);Starts serial communication so you can monitor object detection in the Serial Monitor.
void loop()
This function runs continuously.
-
digitalRead(irPin)reads the current state of the IR sensor. HIGH means an object is detected, LOW means no object is detected - If the sensor output is HIGH, the LED turns on and
Serial.println("Object detected")prints a message to the Serial Monitor - If the sensor output is HIGH, the LED turns off and
Serial.println("No object")prints a message -
delay(200)pauses for 200 milliseconds before checking the sensor again
Step 4 - Uploading the code
- Connect your ESP32 to your computer using the USB cable
- Open Arduino IDE and ensure the correct board and port are selected
- Click Upload and wait for the upload to finish
- Once uploaded, the ESP32 will begin running the program automatically
Step 5 - Testing object detection

- Open the Serial Monitor at 115200 baud
- Move an object in front of the IR sensor
- The LED should turn on and the Serial Monitor should display “Object detected”
- Remove the object and the LED should turn off and the Serial Monitor should display “No object”
ESP32 Lessons
- ESP32 Tutorials Hub
- Lesson 1: ESP32 Setup using Arduino IDE
- Lesson 2: ESP32 Libraries and Using the Serial Monitor
- Lesson 3: LED Blink
- Lesson 4: Passive Buzzer
- Lesson 5: Reading a Photosensitive Resistor (LDR)
- Lesson 6: Using a Push Button with ESP32
- Lesson 7: Using DHT11 Temperature & Humidity Sensor
- Lesson 8: ESP32 Detecting Motion With HC-SR501 PIR Sensor
- Lesson 9: Detecting Objects with an IR Sensor Module
Lesson complete
You have successfully used the IR sensor with your ESP32 to detect objects and trigger an output.