// starting the measurement with the 10us long trigger signal
digitalWrite(Trigger_AusgangsPin, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger_AusgangsPin, LOW);
// waiting on the Echo-Input until the signal is active // and measuring how long the signal stays active
Dauer = pulseIn(Echo_EingangsPin, HIGH);
// calculating the distance with the measured time
Abstand = Dauer/58.2;
// checking if the value is within the rangeif (Abstand >= maximumRange || Abstand <= minimumRange) {
// Printing error if not
Serial.println("Abstand ausserhalb des Messbereichs");
Serial.println("-----------------------------------");
}
else {
// Printing the calculated distance
Serial.print("Der Abstand betraegt:");
Serial.print(Abstand);
Serial.println("cm");
Serial.println("-----------------------------------");
}
// break between the measurements
delay(500);
}
Export 03.11.2017 Copyright by Joy-IT 5
Joy-IT® Ultrasonic Disance Sensor
3
2.Using with a Raspberry Pi
2.1Installing the System
You can skip this step if you are already using the latest Raspbian soware on your Raspberry Pi.
If not, please follow the instrucons.
Install the latest Raspbian System-Image on your SD-Card.
You can download the image here.
You can trasnfer the image with a suitable program (e.g. Etcher) to your card.
You can insert the SD-Card into your Raspberry Pi aer nishing the transfer.
Export 03.11.2017 Copyright by Joy-IT 6
Joy-IT® Ultrasonic Disance Sensor
3
2.2Connecting the module
Connect the ultrasonic module, as seen in the following image and in the following table, to your
Raspberry Pi.
Warning! The Raspberry Pi is working, with its ARM processor core, on a voltage level of 3.3V.
But the ultrasonic distance sensor is working on a higher level.
If you are using the sensor on the Raspberry Pi without any protecon, it can lead to serious damage.
To avoid any damage you can use a voltage translator to adjust the voltage level and ensure a safe
operaon.
Therefore you can use our KY-051 voltage translator from our SensorKit X40.
In the following, we will describe how to use the sensor with the voltage translator.
Export 03.11.2017 Copyright by Joy-IT 7
3
Distance Sensor Voltage Translator
VCC
Trig B1
Echo B2
GND GND
Distance Sensor Raspberry Pi
VCC 5V (Pin 2)
Joy-IT® Ultrasonic Disance Sensor
Voltage Translator Raspberry Pi
5.5V 5V (Pin 3)
3.6V 3.3V (Pin 1)
A1 GPIO17 (Pin 11)
A2 GPIO27 (Pin 13)
GND GND (Pin 6)
1.2Code-Example
You can nd a Code-Example next, which is demonstrang the sensors funconality.
Please completely transfer the code to your Raspberry Pi.
whileTrue:
# distance measurement with 10us long trigger signal
GPIO.output(Trigger_AusgangsPin, True)
time.sleep(0.00001)
GPIO.output(Trigger_AusgangsPin, False)
EinschaltZeit = time.time() while GPIO.input(Echo_EingangsPin) ==0:
EinschaltZeit = time.time() while GPIO.input(Echo_EingangsPin) ==1:
AusschaltZeit = time.time()
# checking if value is within the possible distanceif Abstand <2or (round(Abstand) >300):
# if not - print errorprint("Measurement out of range")
print("------------------------------")
Export 03.11.2017 Copyright by Joy-IT 9
Joy-IT® Ultrasonic Disance Sensor
3
else:
# formating the result
Abstand =format((Dauer *34300) /2, '.2f')
# printing the distanceprint("The distance is:"), Abstand,("cm")
print("------------------------------")
# break between measurement
time.sleep(sleeptime)
# cleanup after program is closed
except KeyboardInterrupt:
GPIO.cleanup()
Export 03.11.2017 Copyright by Joy-IT 10
Joy-IT® Ultrasonic Disance Sensor
3
3. Support
We also support you aer your purchase.
If there are any quesons le or if you encounter any problems, please feel free to contact us by mail,
phone or by our cket-supportsystem on our website.
E-Mail: service@joy-it.net
Ticket-System: hp://support.joy-it.net
Phone: +49 (0)2845 98469 – 66 (11- 18 Uhr)
Please visit our website for more informaons:
www.joy-it.net
Export 03.11.2017 Copyright by Joy-IT 11
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.