Home Code KY-010 Photo Interrupter Module and Raspberry Pi

KY-010 Photo Interrupter Module and Raspberry Pi

by shedboy71

In this example we take a look at the KY-010 Photo Interrupter Module and connect this to a Raspberry Pi.

The sensor uses a beam of light between the emitter and the detector to check if the path between both is being blocked by an object

 

Parts List

Name link
Raspberry Pi 4 Aliexpress product link

Amazon. com link

Ebay search

37 sensor kit AliExpress Product link

Amazon link

ebay link

connecting wire Aliexpress product link

Amazon.com link

Ebay link

 

Layout

We use pin 40 (GPIO 21) in this example, you can use another GPIO pin.

KY-010 module Raspberry Pi
– (left) GND
middle +3v3
S (right) Pin 40
pi and ky-010 layout

pi and ky-010 layout

 

pi and ky-010 schematic

pi and ky-010 schematic

 

Code Examples

# Needed modules will be imported and configured
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# The input pin which is connected with the sensor
GPIO_PIN = 21
GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
print "KY-010 Sensor Test [press ctrl+c to end the test]"
def outputFunction(null):
print("Sensor is blocked")
# signal detection (raising edge).
GPIO.add_event_detect(GPIO_PIN, GPIO.RISING, callback=outputFunction, bouncetime=100)
# Main program loop
try:
while True:
time.sleep(1)
# Scavenging work after the end of the program
except KeyboardInterrupt:
GPIO.cleanup()

view raw
ky010.py
hosted with ❤ by GitHub

Download

code on github

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More