KY-050 Ultrasonic Distance Sensor Module

View on Amazon
Overview
About KY-050 Ultrasonic Distance Sensor Module
The KY-050 Ultrasonic Distance Sensor Module is designed for non-contact distance measurements. It operates by emitting an ultrasonic pulse and measuring the time it takes for the echo to return after reflecting off an object. This module is ideal for applications such as obstacle detection, distance measurement, and level sensing in various projects.
KY-050 Specifications
Complete technical specification details for KY-050 Ultrasonic Distance Sensor Module
๐ Technical Parameters
KY-050 Pinout
The **KY-050** is a 4-pin ultrasonic distance sensor module (similar to HC-SR04):
Visual Pinout Diagram

Pin Types
Quick Tips
**Interface**: Trigger/Echo protocol (ultrasonic time-of-flight),๐ **Range**: 2 cm to 300 cm measurement range
**Resolution**: ~3mm precision,๐ก **Frequency**: 40 kHz ultrasonic pulse
**Power**: Requires 5V (not 3.3V),๐ฏ **Applications**: Obstacle detection, distance measurement, level sensing, parking sensors
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 VCC | Power | Power supply | 5V (not 3.3V compatible) |
2 Trig | Control | Trigger input | Send 10ยตs pulse to trigger measurement |
3 Echo | Communication | Echo output | Pulse width proportional to distance |
4 GND | Power | Ground connection |
Wiring KY-050 to ESP32
To interface the **KY-050** with an **ESP32** for ultrasonic distance measurement:
Pin Connections
| KY-050 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 VCC Required | 5V | Power supply (5V required) | |
2 GND Required | GND | Ground | |
3 Trig Required | GPIO5 | Trigger output (any GPIO) | |
4 Echo Required | GPIO18 | Echo input (any GPIO) |
**Trigger Pulse**: Send 10ยตs HIGH pulse to Trig pin to start measurement
**5V Power**: Module requires 5V, but Echo pin outputs 5V (may need level shifter for ESP32)
**GPIO Selection**: Any GPIO pins work, shown pins are examples
**Distance Calculation**: Distance (cm) = (pulse duration ร 0.034) / 2
**Level Shifter**: Consider voltage divider or level shifter for Echo pin (5Vโ3.3V)
KY-050 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: The sensor does not provide any distance measurements.
Solutions:
- Ensure all connections are secure and correctly wired according to the pinout diagram.
- Verify that the microcontroller's pins are properly configured in the code.
- Check for proper power supply voltage (5V) to the module.
- Ensure that the trigger pulse is at least 10ยตs long.
Issue: The sensor provides inaccurate or fluctuating distance readings.
Solutions:
- Ensure there are no obstacles or reflective surfaces interfering with the ultrasonic signal.
- Check for noise or interference on the power supply lines; consider adding decoupling capacitors if necessary.
- Verify that the sensor is operating within its specified range (2 cm to 300 cm).
Debugging Tips
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.
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
KY-050 Programming Examples
Ready-to-use code examples for different platforms and frameworks
#define echoPin 7 // Echo input pin
#define triggerPin 8 // Trigger output pin
// Required variables are defined
int maximumRange = 300;
int minimumRange = 2;
long distance;
long duration;
void setup() {
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Serial.println("KY-050 Distance measurement");
}
void loop() {
// Distance measurement is started using the 10us trigger signal
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Now wait at the echo input until the signal has been activated
// and then the time is measured to see how long it remains activated
duration = pulseIn(echoPin, HIGH);
// Now the distance is calculated using the recorded time
distance = duration / 58.2;
// Check whether the measured value is within the permissible distance
if (distance >= maximumRange || distance <= minimumRange) {
// If not, an error message is displayed.
Serial.println("Distance outside the measuring range");
Serial.println("-----------------------------------");
}
else {
// The calculated distance is output in the serial output
Serial.print("The distance is: ");
Serial.print(distance);
Serial.println(" cm");
Serial.println("-----------------------------------");
}
// Pause between the individual measurements
delay(500);
}This Arduino code sets up the KY-050 ultrasonic distance sensor by defining the echo and trigger pins. It initializes these pins and starts the serial communication. In the loop, the code sends a 10ยตs pulse to the trigger pin to initiate the measurement. It then measures the duration of the high signal on the echo pin, which corresponds to the time taken for the ultrasonic pulse to travel to the object and back. The distance is calculated by dividing the duration by 58.2 to convert the time into centimeters. If the measured distance is within the sensor's range (2 cm to 300 cm), it is displayed in the serial monitor. Otherwise, an error message is printed. The measurement is repeated every 500 ms.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#define TRIGGER_PIN GPIO_NUM_5
#define ECHO_PIN GPIO_NUM_18
void measure_distance(void *pvParameter) {
while (1) {
gpio_set_level(TRIGGER_PIN, 1);
ets_delay_us(10);
gpio_set_level(TRIGGER_PIN, 0);
while (gpio_get_level(ECHO_PIN) == 0);
int64_t start_time = esp_timer_get_time();
while (gpio_get_level(ECHO_PIN) == 1);
int64_t end_time = esp_timer_get_time();
int64_t duration = end_time - start_time;
float distance = duration / 58.2;
if (distance >= 2 && distance <= 300) {
printf("Distance: %.2f cm\n", distance);
} else {
printf("Out of range\n");
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void app_main() {
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = (1ULL << TRIGGER_PIN)
};
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL << ECHO_PIN);
gpio_config(&io_conf);
printf("KY-050 Ultrasonic Distance Sensor Test\n");
xTaskCreate(measure_distance, "measure_distance", 2048, NULL, 5, NULL);
}This ESP-IDF code configures GPIO5 as an output (Trigger) and GPIO18 as an input (Echo) for the KY-050 ultrasonic sensor. It sends a 10ยตs pulse to trigger the sensor and then measures the time taken for the echo to return using esp_timer_get_time(). The distance is calculated by dividing the duration by 58.2. If the measured value is within the valid range (2 cm to 300 cm), it is printed to the console. Otherwise, an out-of-range message is displayed. Measurements are taken every 500 ms.
sensor:
- platform: ultrasonic
trigger_pin: GPIO5
echo_pin: GPIO18
name: "KY-050 Distance Sensor"
update_interval: 0.5sThis ESPHome configuration sets up the KY-050 ultrasonic sensor with GPIO5 as the trigger pin and GPIO18 as the echo pin. It measures the distance and updates the value every 500 milliseconds.
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduinomain.cpp
#include <Arduino.h>
#define TRIGGER_PIN 5
#define ECHO_PIN 18
void setup() {
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(115200);
Serial.println("KY-050 Ultrasonic Distance Sensor Test");
}
void loop() {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration / 58.2;
if (distance >= 2 && distance <= 300) {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
} else {
Serial.println("Out of range");
}
delay(500);
}This PlatformIO code sets up GPIO5 as the trigger pin and GPIO18 as the echo pin for the KY-050 ultrasonic sensor. It sends a 10ยตs pulse, measures the echo time, and calculates the distance. If the measured value is within the valid range (2 cm to 300 cm), it is printed to the serial monitor. Otherwise, an out-of-range message is displayed. The loop repeats every 500 ms.
import machine
import time
TRIGGER_PIN = machine.Pin(5, machine.Pin.OUT)
ECHO_PIN = machine.Pin(18, machine.Pin.IN)
def measure_distance():
TRIGGER_PIN.off()
time.sleep_us(2)
TRIGGER_PIN.on()
time.sleep_us(10)
TRIGGER_PIN.off()
while ECHO_PIN.value() == 0:
start_time = time.ticks_us()
while ECHO_PIN.value() == 1:
end_time = time.ticks_us()
duration = end_time - start_time
distance = duration / 58.2
return distance
while True:
dist = measure_distance()
if 2 <= dist <= 300:
print("Distance:", dist, "cm")
else:
print("Out of range")
time.sleep(0.5)This MicroPython script configures GPIO5 as an output (Trigger) and GPIO18 as an input (Echo) for the KY-050 ultrasonic sensor. It sends a 10ยตs pulse to trigger the sensor, measures the echo duration, and calculates the distance. If the distance is within the valid range (2 cm to 300 cm), it prints the value; otherwise, an out-of-range message is displayed. The measurement is repeated every 500 milliseconds.
Wrapping Up KY-050
The ESP32 KY-050 Ultrasonic Distance 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.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the KY-050 into your ESP32 project and bring your ideas to life!
Explore Alternative Sensors
Looking for alternatives to the KY-050? Check out these similar sensors that might fit your project needs.

KY-024 Linear Magnetic Hall Sensor Module
The KY-024 is a linear magnetic Hall sensor module that provides both analog and digital outputs. It is equipped with a potentiometer to...

KY-026 Flame Sensor Module
The KY-026 is a flame sensor module capable of detecting light in the 760 nm to 1100 nm wavelength range. It offers both analog and digital...

KY-003 Hall Magnetic Sensor Module
The KY-003 is a Hall Magnetic Sensor Module that detects magnetic fields using the A3144 Hall-effect sensor. It provides a digital output...




