www.joy-it.net
Pascalstr. 8 47506 Neukirchen-Vluyn
2. Code example
This sample code shows in the console whether the sensor is above a line
or not. This is done by infrared, because if the sensor is on the line, the
transmitted infrared signal does not reach the receiver.
# 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 ("LineTracker is above the line")
else:
print ("LineTracker is out of a line")
print ("---------------------------------------")
# Reset + Delay
time.sleep(delayTime)
# Clean up after the program has finished
except KeyboardInterrupt:
GPIO.cleanup()
You can also download this sample code here.
Furthermore, the sensor has a potentiometer with which the
sensitivity can be adjusted. A LED also lights up when the sensor receives
an infrared signal, i.e. leaves the line.
But first you have to install this package:
sudo apt install python3-rpi.gpio
Now, you can either use the following commands to download and unzip
the file :
wget http://sensorkit.joy-it.net/images/c/cf/KY-033_RPi_Trackingsensor.zip
unzip KY-033_RPi_Trackingsensor.zip