 This module is ideally suited for distance measurement in a range between 2 cm and 3 m. With a resolution of about 3 mm, distances can thus be measured by ultrasonic signal. If a signal (falling edge) is applied to the trigger input, a distance measurement is performed and output at the echo output as a PWM TTL signal. The ultrasonic distance sensor is particularly suitable for obstacle detection, distance measurement, as a level indicator and for industrial applications.

### Technical specifications

Measurable distance 2 cm - 300 cm
Measurement resolution 3 mm
min. time between measurements 50 µs

### principle of operation

This module shows how an ultrasonic loudspeaker and a microphone can be used to measure the distance to an object without contact. The principle is based on the fact that the speed of sound in air remains almost constant at constant temperature - at 20 °C it is 343.2 m / s.

From this, the distance measurement can be converted into a time measurement, which can then easily be taken over by microcontrollers. When triggered, the ultrasonic loudspeaker (transducer) emits an ultrasonic noise of maximum 200µs. The ultrasonic loudspeaker emits a 40 kHz signal. This means that 8 periods (edge changes) are emitted within the 200µs in which the sensor emits its ultrasonic noise. In order to arrive at these 8 periods of the 40 kHz signal mathematically, one calculates the whole thing as shown in the following.

Number of periods in one second = 40000
Time = 1s
Duration of a single period:
1s / 40000 = 25µs**

Maximum length of ultrasonic sound = 200us
Duration of a single period = 25us
Number of periods in ultrasonic sound:
200µs / 25µs = 8

The principle is kept simple, an ultrasonic sound is emitted from the speaker on the PCB, which is then reflected from an object and picked up by the microphone on the PCB. Ultrasound is used because it is outside the hearing range of the human ear (about 20Hz-22kHz).

Transmission of the ultrasonic signal is started when a 10µs long start signal (ActiveHigh) is received at the "Trigger input pin". After transmission, the signal is activated at the "Echo output signal pin" (ActiveHigh). If the reflected signal is now picked up again at the microphone, the echo signal is deactivated again after detection. The time between activation and deactivation of the echo signal can be measured and converted to distance, as this also corresponds to how long it takes the ultrasonic signal to cover the distance between loudspeaker-> reflecting wall -> microphone in the air. The conversion is then made by approximating a constant air velocity - the distance is then half the distance traveled. ### pin assignment ## Code example Arduino

### Pin assignment Arduino Arduino Sensor
Pin 7 Echo
5 V + V
Ground GND
Pin 8 Trigger

The example program activates the distance measurement according to the above principle and measures the time until the ultrasonic signal is picked up by the microphone with the help of the Arduino function pulseIn . This time is then taken as the basis for the conversion of the distance - the result is then output in the serial output. If the signal is outside the measuring range, a corresponding error message is output.

``````#define Echo_EingangsPin 7 // Echo input pin
#define Trigger_AusgangsPin 8 // Trigger output pin

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

void setup () {
pinMode (Trigger_AusgangsPin, OUTPUT);
pinMode (Echo_EingangsPin, INPUT);
Serial.begin (9600);
}

void loop () {

// Distance measurement is started by means of the 10us long trigger signal
digitalWrite (Trigger_AusgangsPin, HIGH);
delayMicroseconds (10);
digitalWrite (Trigger_AusgangsPin, LOW);

// Now we wait at the echo input until the signal has been activated
// and then the time measured how long it remains activated
Duration = pulseIn (Echo_EingangsPin, 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 output.
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);
}
``````

KY050-Arduino.zip

## Code example Raspberry Pi

### Pin assignment Raspberry Pi Raspberry Pi Sensor
5 V [pin 2] + V
Ground [pin 6] GND
Sensor KY-051
Trigger B1
Echo B2
Raspberry Pi KY-051
3.3 V [pin 1] Vcca
5 V [pin 2] Vccb
Ground [pin 6] GND
Raspberry Pi KY-051
GPIO 17 [pin 11] A1
GPIO 27 [pin 13] A2

Programming example in the Python programming language

5V voltage level, therefore the following has to be considered: The Raspberry Pi works with its ARM processor core, unlike the Atmel Atmega based Arduino, with 3.3V voltage level instead of 5V. However, this sensor only works with the higher voltage level. If the sensor would be used without any restrictions on the Raspberry Pi, without any precautions, this could cause permanent damage to the Raspberry Pi.

To prevent this, it is recommended to connect a so-called voltage translator in between, which adjusts the voltage levels accordingly and ensures a safe operation. Our KY-051 Voltage Translator is perfectly suited for this purpose.

The example program activates the distance measurement according to the above principle and measures the time how long the ultrasonic signal is in the air. This time is then taken as a basis for the conversion of the distance - the result is then output in the cosnole. If the signal is outside the measuring range, a corresponding error message is output.

``````# coding = utf-8
# Required modules are inserted and configured
import time
import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)

# The respective input / output pins can be selected here
Trigger_AusgangsPin = 27
Echo_EingangsPin = 17

# The pause between the individual measurements can be set here in seconds
sleeptime = 0.8

# The input / output pins are configured here
GPIO.setup (Trigger_AusgangsPin, GPIO.OUT)
GPIO.setup (Echo_EingangsPin, GPIO.IN)
GPIO.output (Trigger_AusgangsPin, False)

# Main program loop
try:
while True:
# Distance measurement is started by means of the 10us long trigger signal
GPIO.output (Trigger_AusgangsPin, True)
time.sleep (0.00001)
GPIO.output (Trigger_AusgangsPin, False)

# The stopwatch is started here
Switch-on time = time.time ()
while GPIO.input (Echo_EingangsPin) == 0:
Switch-on time = time.time () # The current time is saved until the signal is activated

while GPIO.input (Echo_EingangsPin) == 1:
Switch-off time = time.time () # The last time when the signal was active is recorded

# The difference between the two times gives the duration you are looking for
Duration = switch-off time - switch-on time
# This can now be used to calculate the distance based on the speed of sound
Distance = (duration * 34300) / 2

# Check whether the measured value is within the permissible distance
if distance <2 or (round (distance)> 300):
# If not, an error message is issued
print ("distance outside the measuring range")
print ("------------------------------")
else:
# The space is formatted to two places behind the comma
Distance = format ((duration * 34300) / 2, '.2f')
# The calculated distance is output on the console
print ("The distance is:"), distance, ("cm")
print ("------------------------------")

# Pause between the individual measurements
time.sleep (sleeptime)

# Clean up after the program has ended
except KeyboardInterrupt:
GPIO.cleanup ()
``````

KY050-RPi.zip

``````sudo python3 KY050-RPi.py
``````

## Code example Micro: Bit

### Pin assignment Micro: Bit: Micro: bit Sensor
3V + V
Ground [External and Micro: Bit] GND
Sensor KY-051
Trigger B1
Echo B2
Micro: bit KY-051
3 V Vcca
5 V [Ext.] Vccb
Ground [External and Micro: Bit] GND
Micro: bit KY-051
Pin 2 A1
Pin 1 A2

For the following code example an additional library is needed:

pxt-sonar by Microsoft | published under the MIT License

Add the library to your IDE by clicking on "Extensions" and entering the following URL in the search box: https: //github.com/microsoft/pxt-sonar.git. Confirm the search with [Enter]. 