photo interrupter is transmission type photo sensor that integrates optical receiving and transmitting elements in a single package please see the diagram below. Since the method involves light blocking commonly named photo interrupter. A long life infrared photo diodes is applied for output while a single photo transistor and photo IC are integrated on the other side for light detection.
Component Required
- Raspberry Pi / Banana Pi / Orange Pi (If your using Banana Pi or Orange See first the GPIO Pin Mapping)
- Photo Interrupter Light Blocking Module
- Solder Less Bread Board
- Jumper Wire / DuPont Wire
Wiring Guide (Click the Image to Enlarge)
Compiling & Uploading the Code
C Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/* 14CORE Test Code for: Light Blocking Switch on C Code .:+osysso++:` `+yhs/-` `-+s+` `:/+++++++++` .:/++ooo++/-` .ooooooooooo+: :///////////- `odh/` `:y+` /ddhsooooooo+ /hddhsooooydddh sdddoooooosdddy` `////////////` -hds` `sy. +ddy` .ddd: sddy.dddo +ddd- .-----------` `hds :sssss/ -ossssso-yy` `hdd: oddy `ddd/oddd:......+dddo .++++++++++++ +dd` :sdddh` :ydddddddo +y/ .ddd+........ `hddy:.....:yddy.hdddddddddddy+. ```````````` ydy .hddd/+hdddhydddh/.+yo /hdddddddddd. :shddddddddhs/`+ddd:````-yddy- :ooooooooooo+ odh` sdddooyyyyyhddddyy.sy+ ` `......... ``.... ....` ```...` ` `` `` ` -dd+`::::` .:::- /yy. -oos+:-oos+--oos+: /o `+y o/: o:+/ h:`yos /+-`/+/+ s:y/y. /dd/ `+yy: +//+ +/:+ +/:+ -. `/ -:o.:/:`//+- + +:- :`-+:`://: +`o`+. -yds. `/yys- .`-.. .``` ```` .`` ` ` ``` ``-..` ` :ydy/.`````.-/oyhs: `+++oo+oo+:.+-++/-/ooo+o +:o/oo///:+/ .:oyhhhhhhhso:` `. ``` */ #include <wiringPi.h> #include <stdio.h> #define SensorPin 0 #define LedOutput 1 void INOUT(char* ledstate) { pinMode(Rpin, OUTPUT); if (ledstate == "OUTPUT") { digitalWrite(LedOutput, HIGH); } else if (ledstate == "INPUT") { digitalWrite(LedOutput, LOW); } else printf("Error Led Output"); } int main(void) { if(wiringPiSetup() == -1) //If no response from wiringpi.h print { printf("Wiring Pi Initialization Faild"); return 1; } pinMode(SensorPin, INPUT); INOUT("INPUT"); while(1){ if(0 == digitalRead(SensorPin)){ delay(10); if(0 == digitalRead(SensorPin)){ INOUT("OUTPUT"); printf("Detected!\n"); } } else if(1 == digitalRead(SensorPin)){ delay(10); if(1 == digitalRead(SensorPin)){ while(!digitalRead(SensorPin)); INOUT("INPUT"); } } } return 0; } |
Python Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#!/usr/bin/env python import RPi.GPIO as GPIO LightInPin = 11 //Set GPIO Pin 11 OutLed = 12 // Set GPIO Pin 12 def setup(): GPIO.setmode(GPIO.BOARD) # Set GPIO as PIN Numbers GPIO.setup(OutLed, GPIO.OUT) # Set Green Led Pin mode to output GPIO.setup(LightInPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pull up to high level(3.3V) GPIO.add_event_detect(LightInPin, GPIO.BOTH, callback=detect, bouncetime=200) def Led(x): if x == 0: GPIO.output(OutLed, 0) if x == 1: GPIO.output(OutLed, 1) def Print(x): if x == 1: print ' 14CORE | Light Interrupter' print ' --------------------------' print ' Light has been interrupted' print ' --------------------------' def detect(chn): Led(GPIO.input(LightInPin)) Print(GPIO.input(LightInPin)) def loop(): while True: pass def destroy(): GPIO.output(OutLed, GPIO.HIGH) # Green led off GPIO.output(Rpin, GPIO.HIGH) # Red led off GPIO.cleanup() # Release resource if __name__ == '__main__': # Set the Program start from here setup() try: loop() except KeyboardInterrupt: # When pressed 'Ctrl+C' child program destroy() will be executed. destroy() |
Wiring Photo Interrupter Light Blocking Switch with Raspberry Pi