ESP32 KY-032 Infrared Obstacle Avoidance Sensor Module
Overview
The KY-032 is an infrared obstacle avoidance sensor module that detects objects by emitting and receiving infrared light. It provides a digital output signal upon detecting an obstacle, making it suitable for applications like robotic navigation and object detection.
About KY-032 Infrared Obstacle Avoidance Sensor Module
The KY-032 Infrared Obstacle Avoidance Sensor Module is designed to detect obstacles using infrared light. It emits infrared light, which, when reflected by an obstacle, is detected by a photodiode. The module outputs a digital signal indicating the presence or absence of an obstacle. This sensor is commonly used in robotics for obstacle detection and avoidance.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
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 obstacle detection.EN (Enable):
Can be used to enable or disable the sensor; by default, the sensor is always enabled.
Troubleshooting Guide
Common Issues
❌ No Obstacle Detection
Issue: The sensor does not detect obstacles.
Solutions:
- Ensure proper wiring connections and verify the power supply voltage.
- Adjust the sensitivity and detection distance using the onboard potentiometers.
- Check for any obstructions or dirt on the sensor's infrared emitter and receiver.
⚠️ False Positives
Issue: The sensor indicates obstacles when none are present.
Solutions:
- Reduce the sensitivity using the potentiometer to prevent detection of distant or small objects.
- Ensure the sensor is not exposed to direct sunlight or strong ambient infrared sources.
- Verify that the sensor is securely mounted to prevent vibrations or movements that could cause false readings.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
int sensorPin = 10; // Sensor input pin
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
Serial.println("KY-032 Obstacle Detection Test");
}
void loop() {
int sensorValue = digitalRead(sensorPin);
if (sensorValue == LOW) { // Obstacle detected
Serial.println("Obstacle detected");
} else {
Serial.println("No obstacle");
}
delay(500);
}
This Arduino code initializes the sensor input pin and serial communication. In the loop, it reads the sensor value and prints "Obstacle detected" or "No obstacle" to the serial monitor based on the sensor's digital output.
ESP-IDF Example
#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-032 Obstacle Detection Test\n");
while (1) {
int sensor_value = gpio_get_level(SENSOR_PIN);
if (sensor_value == 0) { // Obstacle detected
printf("Obstacle detected\n");
} else {
printf("No obstacle\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 "Obstacle detected" or "No obstacle" to the console based on the sensor's output.
ESPHome Example
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT_PULLUP
name: "KY-032 Obstacle Sensor"
device_class: motion
filters:
- invert:
on_press:
then:
- logger.log: "Obstacle detected"
on_release:
then:
- logger.log: "No obstacle detected"
This ESPHome configuration sets up the KY-032 obstacle avoidance sensor as a binary sensor on GPIO16. It logs messages when an obstacle is detected and when it is cleared. The input is inverted to match the sensor's logic.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#include <Arduino.h>
#define SENSOR_PIN 16
void setup() {
pinMode(SENSOR_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("KY-032 Obstacle Sensor Test");
}
void loop() {
int sensor_value = digitalRead(SENSOR_PIN);
if (sensor_value == LOW) {
Serial.println("Obstacle detected");
} else {
Serial.println("No obstacle");
}
delay(500);
}
This PlatformIO code sets up GPIO16 as an input with a pull-up resistor for the KY-032 sensor. It prints "Obstacle detected" or "No obstacle" to the serial monitor based on the sensor's state.
MicroPython Example
import machine
import time
SENSOR_PIN = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if SENSOR_PIN.value() == 0:
print("Obstacle detected")
else:
print("No obstacle")
time.sleep(0.5)
This MicroPython script configures GPIO16 as an input with a pull-up resistor for the KY-032 sensor. It continuously checks for obstacles and prints the corresponding message every 500ms.
Conclusion
The ESP32 KY-032 Infrared Obstacle Avoidance Sensor Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.