Utility Bar

ESP32 Detecting Objects with an IR Sensor Module - Lesson 9

ESP32 Detecting Objects with an IR Sensor Module

Ethan Zaitchik |

Table of Contents

    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.

    IR Sensor Module Pinout

    Step 2 - Wiring the IR sensor and LED

    1. Place the IR sensor on the breadboard
    2. Connect VCC to 3.3 V on the ESP32
    3. Connect GND to GND on the ESP32
    4. Connect the OUT pin to GPIO 23 on the ESP32
    5. Place the LED on the breadboard
    6. Connect a 220 Ω resistor to the LED anode (long leg)
    7. Connect the other end of the resistor to GPIO 2
    8. Connect the LED cathode (short leg) to GND
    ESP32 IR Sensor Wiring Diagram

    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

    1. Connect your ESP32 to your computer using the USB cable
    2. Open Arduino IDE and ensure the correct board and port are selected
    3. Click Upload and wait for the upload to finish
    4. Once uploaded, the ESP32 will begin running the program automatically

    Step 5 - Testing object detection

    ESP32 IR Sensor Serial Monitor Output
    1. Open the Serial Monitor at 115200 baud
    2. Move an object in front of the IR sensor
    3. The LED should turn on and the Serial Monitor should display “Object detected”
    4. Remove the object and the LED should turn off and the Serial Monitor should display “No object”

    ESP32 Lessons

    Lesson complete

    You have successfully used the IR sensor with your ESP32 to detect objects and trigger an output.

    Leave a comment

    Please note: comments must be approved before they are published.