03-04 PhotoInterrupter
From Manuals
A simple program to test KTIR slotted optosensor. It switches an LED according to state of sensor. The LED is connected to D0 of J9 and the optosenser is connected to D2.
Please see schematic below for further details.
/*Program Example 3.4: Simple program to test KTIR slotted optosensor. Switches an LED according to state of sensor
*/
#include "mbed.h"
DigitalOut redled(D0);
DigitalIn opto_switch(D2);
int main()
{
while(1) {
if (opto_switch==1) //input = 1 if beam interrupted
redled = 1; //switch led on if beam interrupted
else
redled = 0; //led off if no interruption
} //end of while
}
The following image shows the built circuit.


