Instructions

www.joy-it.net
Pascalstr. 8 47506 Neukirchen-Vluyn
This code reads the current status of the sensor and displays in the
console whether an obstacle is present or not. If the sensor detects an
obstacle, another LED (Sled) lights up.
The sensor has two potentiometers with which the
sensitivity of the receiver as well as the transmitter can be adjusted.
You can also download this sample code here.
First, you have to install this package:
wget http://sensorkit.joy-it.net/images/d/d8/KY-032_RPi_HindernisDetektor.zip
unzip KY-032_RPi_HindernisDetektor.zip
2. Code example
# Required modules are imported and set up
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# The input pin to which the sensor is connected, is declared here.
GPIO_PIN = 24
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Pause between the output of the result is defined (in seconds)
delayTime = 0.5
print ("Sensor-Test [druecken Sie STRG+C, um den Test zu beenden]")
# Main loop of the program
try:
while True:
if GPIO.input(GPIO_PIN) == True:
print ("No obstacle")
else:
print ("Obstacle detected")
print ("---------------------------------------")
# Reset + Delay
time.sleep(delayTime)
# Clean up after the program has finished
except KeyboardInterrupt:
GPIO.cleanup()
sudo apt install python3-rpi.gpio
Now, you can either use the following commands to download and unzip
the file :
Or you can create a new file with the following command and copy the
example code described into it:
sudo nano KY-032_RPi_HindernisDetektor.py