Introduction
A photo-interrupter is a sensor that arranges light-emitting component and light-receiving component face-to-face and packages them together. 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 * SunFounder Uno board
– 1 * USB data cable
– 1 * Photo-interrupter module
– Several jumper wires
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 incoming 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.
Experimental Procedures
Step 1: Build the circuit
Photo-interrupter Module SunFounder Uno
S ————————————— A0
– ————————————– GND
+ ————————————— 5V
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, stick a piece of paper in the gap of the sensor, and the LED attached to pin 13 on the SunFounder Uno will go out; remove the paper, and then the LED will light up again.
Code
/***************************************************/ const int ledPin = 13; //the number of the led pin int val = 0; //variable to store the value from photo interrupter /***************************************************/ void setup() { pinMode(ledPin,OUTPUT); //initialize led as an output Serial.begin(9600); } /***************************************************/ void loop() { val = analogRead(0); //read the value from photo interrupter Serial.println(val); if(val > 400) //when interrupted { digitalWrite(ledPin,HIGH); //turn the led on } else { digitalWrite(ledPin,LOW); //turn the led off } } /******************************************************/ |