KY-050 Ultrasonic Distance Sensor Module

The KY-050 is an ultrasonic distance sensor module capable of measuring distances ranging from 2 cm to 300 cm with a resolution of approximately 3 mm. It emits a 40 kHz ultrasonic pulse and measures the time taken for the echo to return, allowing for precise distance calculations.

KY-050 Ultrasonic Distance Sensor Module image
KY-050 · Digital
Digital
Interface
4pins
Connections
5V
Supply
2-300cm
Range
40kHz
Frequency
$3
Typical price
On this page

KY-050 pinout

4 pins · Digital

The KY-050 is a 4-pin ultrasonic distance sensor module (similar to HC-SR04):

View:
KY-050 Ultrasonic Distance Sensor Module pinout
PinTypeDescriptionNotes
VCCPowerPower supply5V (not 3.3V compatible)
TrigControlTrigger inputSend 10µs pulse to trigger measurement
EchoCommunicationEcho outputPulse width proportional to distance
GNDPowerGround connection
  • 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

Wiring the KY-050 to ESP32

4 connections · all required

To interface the KY-050 with an ESP32 for ultrasonic distance measurement:

Wiring diagram coming soon
The pin-to-pin table covers every connection.
KY-050 pinESP32 pinPurpose
VCC5VPower supply (5V required)
GNDGNDGround
TrigGPIO5Trigger output (any GPIO)
EchoGPIO18Echo 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 code examples

5 platforms
Platform:

KY-050 Arduino example

Copy
#define triggerPin 5 // Trigger output pin (GPIO5, matches the wiring above)
#define echoPin 18   // Echo input pin (GPIO18)

// Required variables are defined
int maximumRange = 300;
int minimumRange = 2;
long distance;
long duration;

void setup() {
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(115200);
  Serial.println("KY-050 Distance measurement");
}

void loop() {
  // Distance measurement is started using the 10us trigger signal
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  // Measure how long the echo input stays 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) {
    Serial.println("Distance outside the measuring range");
    Serial.println("-----------------------------------");
  } else {
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
  }
  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.

KY-050 ESP-IDF example

Copy
#include <stdio.h>
#include "esp_rom_sys.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);
        esp_rom_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.

KY-050 ESPHome example

Copy
sensor:
  - platform: ultrasonic
    trigger_pin: GPIO5
    echo_pin: GPIO18
    name: "KY-050 Distance Sensor"
    update_interval: 0.5s

This 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.

KY-050 PlatformIO example

Copy
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
src/main.cppCopy
#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.

KY-050 MicroPython example

Copy
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.

KY-050 specifications

From the datasheet
Operating Voltage
5V
Measuring Range
2 cm to 300 cm
Resolution
3 mm
Ultrasonic Frequency
40 kHz
Minimum Time Between Measurements
50 µs
Dimensions
45 mm x 20 mm x 15 mm

About the KY-050

The KY-050 is functionally the same sensor as the classic HC-SR04: a 4-pin Trig/Echo module that fires a burst of 40 kHz ultrasonic chirps and times how long the echo takes to come back, just packaged as Joy-IT’s own version of that near-universal design. Distance is derived from the round-trip time and the speed of sound (roughly 343 m/s at 20 degrees C, per Joy-IT’s own documentation of this module), which is also why accuracy drifts a little with air temperature and humidity rather than staying perfectly fixed - fine for the obstacle-avoidance and level-sensing use cases this sensor targets, less so for anything needing millimeter-stable readings.

Like every HC-SR04-family board, it needs 5V to reach its rated 300 cm range, and that means Echo comes back as a 5V pulse - feeding it straight into an ESP32 GPIO risks exceeding the pin’s 3.3V input tolerance over time. A simple resistor divider on the Echo line handles it, or a proper translator like the KY-051 if there are several signals to shift at once. For indoor, more precise, or line-of-sight-limited spots where a bulky ultrasonic module does not fit well, a time-of-flight sensor such as the VL53L0X trades range for a much smaller footprint and immunity to soft or angled surfaces that scatter ultrasonic echoes.

KY-050 troubleshooting

2 common issues

No Distance Reading

›

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.

Incorrect Distance Measurements

›

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).

Resources