Get tutorials Raspberry Pi Sensor Kit V2.0 for Raspberry Pi B+

Lesson 12 Photo-interrupter

Introduction

A photo-interrupter (as shown below) is a sensor with a light-emitting component and light-receiving component packaged and placed on face-to-face. It applies the principle that light is interrupted when an object passes through the sensor. Therefore, photo-interrupters are widely used in speed measurement.


 


Components

- 1 * Raspberry Pi

- 1 * Breadboard

- 4 * Jumper wires (Male to Male, 2 red and 2 black)

- 1 * Network cable (or USB wireless network adapter)

- 1 * Dual-color LED module

- 1 * Photo-interrupter module

- 2 * 3-Pin anti-reverse cable

 

Experimental Principle

Basically a photo-interrupter consists of two parts: transmitter and receiver. The transmitter (e.g., an LED or a laser) emits light and then the light goes to the receiver. If that light beam between the transmitter and receiver is interrupted by an obstacle, the receiver will detect no incident light even for a moment and the output level will change. In this experiment, we will turn an LED on or off by using this change.

 

The schematic diagram is as shown below:

 

 

Experimental Procedures

Step 1: Build the circuit


Raspberry Pi

T-Cobbler

Photo-interrupter Module

GPIO0

GPIO17

SIG

5V

5V0

VCC

GND

GND

GND

 

Raspberry Pi

T-Cobbler

Dual-color LED Module

GPIO1

GPIO18

R

GND

GND

GND

GPIO2

GPIO27

G

 

 

For C language users:

Step 2: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/C/12_photo_interrupter/

Step 3: Compile

gcc photo_interrupter.c –lwiringPi

Step 4: Run

sudo ./a.out


For Python users:

Step 2: Change directory

 cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/

Step 3: Run

sudo python 12_photo_interrupter.py


Now the LED will light up green. If you stick a piece of paper in the gap of photo interrupter, "Light was blocked" will be printed o-14n the screen and the LED will emit red light. If you remove the paper, the LED will emit green light again.


 

 

C Code


#include <wiringPi.h>
#include <stdio.h>

#define LBPin		0  // light break pin set to GPIO0
#define Gpin		1
#define Rpin		2

void LED(int color)
{
	pinMode(Gpin, OUTPUT);
	pinMode(Rpin, OUTPUT);
	if (color == 0){
		digitalWrite(Rpin, HIGH);
		digitalWrite(Gpin, LOW);
	}
	else if (color == 1){
		digitalWrite(Rpin, LOW);
		digitalWrite(Gpin, HIGH);
	}
}

void Print(int x){
	if ( x == 0 ){
		printf("Light was blocked\n");
	}
}

int main(void){

	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}

	pinMode(LBPin, INPUT);
	int temp;
	while(1){
		//Reverse the input of LBPin
		if ( digitalRead(LBPin) == 0 ){  
			temp = 1;
		}
		if ( digitalRead(LBPin) == 1 ){
			temp = 0;
		}

		LED(temp);
		Print(temp);
	}
	return 0;
}



Python Code


#!/usr/bin/env python
import RPi.GPIO as GPIO

PIPin  = 11
Gpin   = 12
Rpin   = 13

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
	GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output
	GPIO.setup(PIPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # Set BtnPin's mode is input, and pull up to high level(3.3V)
	GPIO.add_event_detect(PIPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x):
	if x == 0:
		GPIO.output(Rpin, 1)
		GPIO.output(Gpin, 0)
	if x == 1:
		GPIO.output(Rpin, 0)
		GPIO.output(Gpin, 1)

def Print(x):
	if x == 1:
		print '    *************************'
		print '    *   Light was blocked   *'
		print '    *************************'

def detect(chn):
	Led(GPIO.input(PIPin))
	Print(GPIO.input(PIPin))

def loop():
	while True:
		pass

def destroy():
	GPIO.output(Gpin, GPIO.HIGH)       # Green led off
	GPIO.output(Rpin, GPIO.HIGH)       # Red led off
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()



 

 


Copyright © 2012 - 2016 SunFounder. All Rights Reserved.


Previous chapter: Lesson 11 Reed Switch

Next chapter: Lesson 13 PCF8591

SunFounder
Apr 14 2017 at 09:35 am



© Developed by CommerceLab