Skip to main content
ESPBoards

ESP32 KY-033 Line Tracking Sensor Module

The KY-033 is a line tracking sensor module that detects light-reflective and light-absorbing surfaces using infrared technology. It provides a digital output signal upon detecting a line, making it suitable for applications like robotic navigation and line-following projects.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-033 Line Tracking Sensor Module Datasheet ButtonKY-033 Line Tracking Sensor Module Specs ButtonKY-033 Line Tracking Sensor Module Specs ButtonKY-033 Line Tracking Sensor Module Specs Button

🛒 KY-033 Price

KY-033 Line Tracking Sensor Module
Normally, the KY-033 Line Tracking Sensor Module costs around 2$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ About KY-033 Line Tracking Sensor Module

The KY-033 Line Tracking Sensor Module is designed for line-following robots and similar applications. It utilizes an infrared emitter and receiver to detect the presence of a line by differentiating between light-reflective and light-absorbing surfaces. The module outputs a digital signal indicating whether it is over a line or not. Sensitivity can be adjusted using the onboard potentiometer.

⚙️ KY-033 Sensor Technical Specifications

Below you can see the KY-033 Line Tracking Sensor 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.

  • Type: module
  • Protocol: Digital
  • Operating Voltage: 3.3V to 5V
  • Detection Distance: 1 mm to 25 mm (adjustable)
  • Detection Angle: 35°
  • Current Consumption: 20 mA
  • Operating Temperature: -10°C to 50°C
  • Dimensions: 42 x 10.5 mm

🔌 KY-033 Sensor Pinout

Below you can see the pinout for the KY-033 Line Tracking Sensor 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!

  • GND: Connects to the ground of the circuit.
  • VCC: Supplies power to the module, typically 3.3V or 5V.
  • S (Signal): Outputs a digital signal indicating line detection.

🧵 KY-033 Wiring with ESP32

Below you can see the wiring for the KY-033 Line Tracking Sensor 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.

  • GND: Connect to ESP32 GND.
  • VCC: Connect to ESP32 3.3V or 5V.
  • S (Signal): Connect to a digital input pin on the ESP32 (e.g., GPIO16).

🛠️ KY-033 Line Tracking Sensor 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 Line Detection

Issue: The sensor does not detect lines.

Solutions:

  • Ensure proper wiring connections and verify the power supply voltage.
  • Adjust the sensitivity using the onboard potentiometer to suit the surface and lighting conditions.
  • Check for any obstructions or dirt on the sensor's infrared emitter and receiver.

⚠️ False Positives

Issue: The sensor indicates a line when none is present.

Solutions:

  • Reduce the sensitivity using the potentiometer to prevent detection of unintended surfaces.
  • Ensure the sensor is not exposed to direct sunlight or strong ambient infrared sources.
  • Verify that the sensor is mounted at an appropriate height and angle relative to the surface.

💻 Code Examples

Below you can find code examples of KY-033 Line Tracking Sensor Module with ESP32 in several frameworks:

If you encounter issues while using the KY-033 Line Tracking Sensor Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-033 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-033 Line Tracking Sensor Module:

int linetracking = 10; // Declaration of the sensor input pin
int value; // Temporary variable

void setup() {
pinMode(linetracking, INPUT); // Initialization sensor pin
digitalWrite(linetracking, HIGH); // Activation of internal pull-up resistor
Serial.begin(9600); // Initialization of the serial monitor
Serial.println("KY-033 Linetracking");
}

void loop() {
// The current signal at the sensor is read out.
value = digitalRead(linetracking);
// If a signal could be detected, this is displayed on the serial monitor.
if (value == HIGH) {
Serial.println("Line recognized");
delay(200); // 200 ms break
}
}

This Arduino code initializes the sensor input pin and serial communication. In the loop, it reads the sensor value and prints "Line recognized" to the serial monitor when a line is detected. A 200 ms delay is added to debounce the sensor readings.

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.

ESP-IDF Image

ESP32 KY-033 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-033 Line Tracking Sensor 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_16

void app_main(void) {
gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << SENSOR_PIN),
.pull_up_en = GPIO_PULLUP_ENABLE
};
gpio_config(&io_conf);

printf("KY-033 Line Tracking Sensor Test\n");
while (1) {
int sensor_value = gpio_get_level(SENSOR_PIN);
if (sensor_value == 1) { // Line detected
printf("Line recognized\n");
} else {
printf("No line detected\n");
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}

This ESP-IDF code configures GPIO16 as an input with a pull-up resistor. It continuously reads the sensor's state and prints "Line recognized" or "No line detected" to the console based on the sensor's output.

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.

ESPHome Image

ESP32 KY-033 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-033 Line Tracking Sensor Module

binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT_PULLUP
name: "KY-033 Line Tracking Sensor"
device_class: motion
filters:
- invert:
on_press:
then:
- logger.log: "Line detected"
on_release:
then:
- logger.log: "No line detected"

This ESPHome configuration sets up the KY-033 line tracking sensor as a binary sensor on GPIO16. It logs messages when a line is detected and when no line is present. The input is inverted to match the sensor's logic.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-033 PlatformIO Code Example

Example in PlatformIO Framework

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-033 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-033 Line Tracking Sensor Module:

#include <Arduino.h>

#define SENSOR_PIN 16

void setup() {
pinMode(SENSOR_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("KY-033 Line Tracking Sensor Test");
}

void loop() {
int sensor_value = digitalRead(SENSOR_PIN);
if (sensor_value == HIGH) {
Serial.println("Line detected");
} else {
Serial.println("No line detected");
}
delay(500);
}

This PlatformIO code sets up GPIO16 as an input with a pull-up resistor for the KY-033 sensor. It prints "Line detected" or "No line detected" to the serial monitor based on the sensor's state.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-033 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-033 Line Tracking Sensor Module with your ESP32.

import machine
import time

SENSOR_PIN = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)

while True:
if SENSOR_PIN.value() == 1:
print("Line detected")
else:
print("No line detected")
time.sleep(0.5)

This MicroPython script configures GPIO16 as an input with a pull-up resistor for the KY-033 sensor. It continuously checks for lines and prints the corresponding message every 500ms.

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-033 Line Tracking Sensor Module, its pinout, connection with ESP32 and KY-033 Line Tracking Sensor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.