Line tracking sensor with RPi

Use a Line Tracker to detect if a door is open, or if a line is crossed with only one sensor and a raspberry pi


A simple exemple to create a line tracker or detect if a door is opened with only one KY-033 module and a raspberry PI, sensor from the “Elegoo37-1 Sensor Kit v2″ that Elegoo sent me.

This project is also compatible with the HW-006 v1.2 and some others tracker modules. This project includes a python code that is really simple to use.

Connect sensor through the GPIO

There is a very little manipulation to connect 3 pins of the sensor on the GPIO. Let’s see :

S is for Signal got to GPIO24[Pin 18] +V is for voltage go to 3, 3V [Pin 1],G is for ground and go to GND [Pin 6] (See shematic)

Install and start script

Python is required. Install it before doing anything.

Clone code exemple in this gist where you want in your RPi.

git clone

Open your command line and launch the script :


Your code should display “Line detected ” when a dark line is near the sensor like in this exemple :



Needed modules will be imported and configured

import RPi.GPIO as GPIO
import time


Declaration of the input pin which is connected with the sensor

GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)

Break between the results will be defined here (in seconds)

delayTime = 0.2

print “#— Hackster project line tracker exemple —#”

main loop

while True:
if GPIO.input(GPIO_PIN) == False:
print “Line detected”

        # Reset + Delay

Scavenging work after the end of the program

except KeyboardInterrupt:

Source: Line tracking sensor with RPi

About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top