Raspberry PI Optical Gate Control Code
Optical gates are basicly an infra LED and photo sensor facing eachother. If infrawave is sensed then the optical gate is open. It is closed otherwise. If everything functions normally a closed optical gate means something is blocking the space between the infra LED and photo sensor. They are widely used for counting objects.
Required hardware
- Raspberry PI
- Optical gate
- Resistors: 100Ω, 100kΩ
Source code to install on controller
#!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) def my_callback(channel): global i i = i+1 def main(): GPIO.setup(27, GPIO.OUT) GPIO.setup(17, GPIO.IN) led = GPIO.PWM(27, 100) GPIO.add_event_detect(17, GPIO.BOTH, callback=my_callback) try: global i i = 0 led.start(50) while True: if i <= 17: print ('Signal') i = 0 time.sleep(0.1) except KeyboardInterrupt: led.stop() GPIO.cleanup() if __name__ == "__main__": main()
More information