Skip to content

Interfacing IR sensor with ESP32 using ESP-IDF

Introduction

In this guide, we will learn how to interface an IR (Infrared) proximity sensor with the ESP32 microcontroller. The IR proximity sensor can detect the presence of an object in its range by emitting and receiving infrared signals. We will use the ESP32's GPIO pins to connect the IR sensor and control a buzzer based on the object detection status.

Problem Statement

The objective of this project is to interface an IR proximity sensor with the ESP32 and control a buzzer. When an object is detected by the sensor, the buzzer should be turned on, indicating the presence of the object.

Requirements

To successfully complete this project, you will need the following components:

  1. ESP32 development board
  2. IR proximity sensor
  3. Buzzer
  4. Jumper wires
  5. Breadboard

Circuit Diagram

Follow the circuit diagram below to connect the IR module and buzzer with ESP32 development board:

  1. Connect the VCC and GND of IR and Buzzer to the 3v3 pin and GND pin of the ESP32 respectively.
  2. Connect the OUT pin of the IR module to GPIO 2 (D2) and the input pin of the buzzer to GPIO 4 (D4) of the ESP32.
ESP8266 & ESP32

Code:

Now let's understand and write the C code to detect the presence of the object. Follow the steps below:

Open VS Code and create a new sample project with the name ir_proximity (Follow previous guides to see how to do it).

Now navigate to the "Main" folder of the project and then open the Main.c file.

Copy and paste the following code into Main.c or download the code from here :

(Buzzer and IR sensor used in this case were active low, please change the code according to your components)

c
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#define IR_PIN GPIO_NUM_2
#define BUZZER_PIN GPIO_NUM_4

void app_main(void)
{
    // Intialise GPIO2 as input for IR sensor
    gpio_set_direction(IR_PIN, GPIO_MODE_INPUT);
    // Intialise GPIO4 as output for buzzer
    gpio_set_direction(BUZZER_PIN, GPIO_MODE_OUTPUT);
    while (1)
    {
        // If object is detected, turn on buzzer
        if (!gpio_get_level(IR_PIN))
        {
            gpio_set_level(BUZZER_PIN, 0);
            printf("Object detected\n");
        }
        else
        {
            gpio_set_level(BUZZER_PIN, 1);
        }
        vTaskDelay(10); // 10ms delay
    }
}
  1. Now at the bottom navbar of VS Code select the port, flash method and click on the Build, flash and monitor icon.

Note: During the flashing process of your board, you may encounter an error. If this happens, there's no need to worry. Simply flash the board again and when you see the message "connecting..........." appear, press and hold the boot button on your board for 1 or 2 seconds. Keep holding it until the dots on the screen start disappearing or the further flashing process begins. This action will help resolve any issues and ensure a successful flashing of your board.

Output

After flashing the code to the ESP32, the IR proximity sensor will continuously monitor its surroundings. When an object is detected within its range, the buzzer will be turned on, and the message "Object detected" will be printed to the console. If no object is detected, the buzzer will remain off.

Conclusion

By following this guide, you have successfully interfaced an IR proximity sensor with the ESP32 microcontroller. The IR sensor can now detect the presence of an object, and the buzzer can provide an audible indication based on the detection status. This project can be extended to various applications, such as object detection systems, security systems, or automation projects.