ESP32 KY-010 Photo Interrupter Module
The KY-010 is a photo interrupter module that detects objects by sensing interruptions in an infrared beam. It's ideal for applications requiring non-contact object detection, such as counting or speed measurement.
⬇️ Jump to Code Examples
🔗 Quick Links
🛒 KY-010 Price
ℹ️ About KY-010 Photo Interrupter Module
The KY-010 Photo Interrupter Module is a digital sensor that detects the presence or absence of an object within its slot. It consists of an infrared emitter and receiver facing each other, forming a light barrier. When an object passes through the slot, it interrupts the infrared beam, causing a change in the output signal. This module operates at a voltage range of 3.3V to 5V, making it compatible with various microcontrollers such as Arduino and ESP32. It's commonly used in applications like object counting, edge detection, and motor speed measurement.⚙️ KY-010 Sensor Technical Specifications
Below you can see the KY-010 Photo Interrupter Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: Digital
- Operating Voltage: 3.3V - 5V
- Slot Width: 5 mm
- Output Type: Digital
- Dimensions: 19 mm x 15 mm
🔌 KY-010 Sensor Pinout
Below you can see the pinout for the KY-010 Photo Interrupter Module. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
Pin (-):
Ground (GND).Pin (+):
Power supply (VCC), typically 3.3V or 5V.Pin (S):
Digital output signal.
🧵 KY-010 Wiring with ESP32
Below you can see the wiring for the KY-010 Photo Interrupter Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
KY-010 Pin (-):
Connect to ESP32GND
.KY-010 Pin (+):
Connect to ESP323.3V
or5V
.KY-010 Pin (S):
Connect to a digital GPIO pin on ESP32 (e.g.,GPIO15
).
🛠️ KY-010 Photo Interrupter Module Troubleshooting
This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.
❌ No Signal Detection
Issue: The module does not detect any object passing through the slot.
Solutions:
- Ensure the module is properly powered with the correct voltage.
- Verify that the signal pin is correctly connected to the microcontroller's GPIO pin.
- Check for any obstructions or debris within the slot that could affect detection.
⚠️ False Triggering
Issue: The module outputs signals without any object present.
Solutions:
- Ensure there is no ambient infrared light interfering with the sensor.
- Check for loose connections or unstable power supply.
- Verify that the microcontroller's input pin is properly configured with pull-up or pull-down resistors as needed.
💻 Code Examples
Below you can find code examples of KY-010 Photo Interrupter Module with ESP32 in several frameworks:
If you encounter issues while using the KY-010 Photo Interrupter Module, check the Common Issues Troubleshooting Guide.

ESP32 KY-010 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the KY-010 Photo Interrupter Module:
int sensorPin = 10; // Sensor connected to digital pin 10
int ledPin = 13; // LED connected to digital pin 13
int sensorValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH); // Enable internal pull-up resistor
Serial.begin(9600);
}
void loop() {
sensorValue = digitalRead(sensorPin);
if (sensorValue == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
Serial.println("Object detected");
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}
delay(100);
}
This Arduino code sets up the KY-010 sensor on digital pin 10 and an LED on pin 13. The internal pull-up resistor is enabled for the sensor pin. When an object interrupts the sensor's beam, the LED lights up, and a message is printed to the serial monitor.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP32 KY-010 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the KY-010 Photo Interrupter Module, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define SENSOR_PIN GPIO_NUM_15
#define LED_PIN GPIO_NUM_2
void app_main(void) {
// Configure sensor pin as input with pull-up resistor
gpio_config_t sensor_io_conf = {
.pin_bit_mask = (1ULL << SENSOR_PIN),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&sensor_io_conf);
// Configure LED pin as output
gpio_config_t led_io_conf = {
.pin_bit_mask = (1ULL << LED_PIN),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&led_io_conf);
printf("KY-010 Photo Interrupter Module Test\n");
while (1) {
int sensor_value = gpio_get_level(SENSOR_PIN);
if (sensor_value == 1) {
gpio_set_level(LED_PIN, 1); // Turn on LED
printf("Object detected\n");
} else {
gpio_set_level(LED_PIN, 0); // Turn off LED
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
This ESP-IDF code configures the KY-010 Photo Interrupter Module on GPIO15 and an LED on GPIO2. The sensor pin is set up as an input with an internal pull-up resistor, while the LED pin is configured as an output. The main loop continuously checks if an object interrupts the infrared beam. If an object is detected, the LED turns on, and a message is printed to the console.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESP32 KY-010 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the KY-010 Photo Interrupter Module
binary_sensor:
- platform: gpio
pin:
number: GPIO15
mode: INPUT_PULLUP
name: "KY-010 Object Detector"
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_press:
- then:
- lambda: |-
ESP_LOGD("sensor", "Object detected!");
This ESPHome configuration sets up the KY-010 Photo Interrupter Module as a binary sensor on GPIO15 with an internal pull-up resistor. It applies filtering to debounce false triggers and logs when an object is detected.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.

ESP32 KY-010 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
ESP32 KY-010 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the KY-010 Photo Interrupter Module:
#include <Arduino.h>
#define SENSOR_PIN 15
#define LED_PIN 2
void setup() {
pinMode(SENSOR_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-010 Photo Interrupter Module Test");
}
void loop() {
if (digitalRead(SENSOR_PIN) == HIGH) {
Serial.println("Object detected");
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(100);
}
This PlatformIO code configures the KY-010 sensor on GPIO15 and an LED on GPIO2. When an object interrupts the infrared beam, the LED turns on, and a message is printed to the serial monitor.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.

ESP32 KY-010 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the KY-010 Photo Interrupter Module with your ESP32.
import machine
import time
SENSOR_PIN = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
LED_PIN = machine.Pin(2, machine.Pin.OUT)
while True:
if SENSOR_PIN.value() == 1:
print("Object detected")
LED_PIN.on()
else:
LED_PIN.off()
time.sleep(0.1)
This MicroPython script configures the KY-010 Photo Interrupter Module on GPIO15 and an LED on GPIO2. When an object interrupts the sensor's beam, the LED turns on, and a message is printed to the console.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of KY-010 Photo Interrupter Module, its pinout, connection with ESP32 and KY-010 Photo Interrupter Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.