Reflecting Sensor
Reflecting Sensor
Reflecting Sensor
1. Overview
2. Applications
1. Overview
An infrared emitter is an LED made from gallium arsenide, which emits near-infrared
energy at about 880nm. The infrared phototransistor acts as a transistor with the base
voltage determined by the amount of light hitting the transistor. Hence it acts as a
variable current source. Greater amount of IR light cause greater currents to flow through
the collector-emitter leads. As shown in the diagram below, the phototransistor is wired
in a similar configuration to the voltage divider. The variable current traveling through
the resistor causes a voltage drop in the pull-up resistor. This voltage is measured as the
output of the device.
An IR emitter An IR phototransistor
2. Applications
The diagram on the right shows an example of a infrared reflectance sensor. For this
particular example, the IR detector being used can be ordered from Mouser Electronics
( Part# 512-QSE113) and the IR emitter can be ordered form Digi-Key (Part# LN175PA-
ND). The resistors being used in this example are chosen to match the electrical
properties of the IR dectector and emitter. You might want to choose different resistors if
you use different dectectors and emitters. Different resistor values affect the sensitivity of
the infrared reflectance sensor.
The following is a test IC program used to illustrate how to use the IR emitter and
detector with the Handy Board. The program will switch on a servo motor when the the
IR detector receives enough Infra-red light. The program uses the analog input 6 of the
Handy Board to read from the IR reflectance sensor. (The lower the value of the variable
num is, the higher the intensity of the IR light)
void mot()
{ float period=0.0;
int k; servo_on();
k = servo_deg(period);
sleep(0.1);
}
void main(){
int num;
while(1){
num = analog(6);
printf("intensity level is %d\n", num);
if (num < 100){
mot();
}
else servo_off();
}
}
2.2 IR slotted optical switch
An infrared slotted optical switch is a device similar to the photo-reflector except that the
emitter is pointed directly into the phototransistor. The slotted optical switch can be used
to build shaft encoders. Shaft encoders can give the robot feedback on how far its wheels
have turned or on synchronizing two wheels' velocity. A shaft encoder usually consists of
a slotted optical switch and a striped wheel with a palette of radically alternating holes or
slots on it. The palette of stripes will alternately reflect or not reflect light to the
phototransistor, yielding a pulse-train output. The robot can then tell how far its wheels
have rotated by counting the pulses.
The wiring for the slotted optical switch is straightforward. The white dot on the optical
switch corresponds to pin 2 in the pinout diagram and you can figure out the pins using
the pinout diagram shown above. The emitter LED is powered by te Handy Board's +5V
supply, with a 330 ohm resistor in series to limit the current through the LED to an
appropriate value. In fact, there is a pull-up resistor of 4.7K ohm built in the Handy
Board and it is not shown in the diagram. Different varieties of phototransistor, however,
may perform better with a smaller resistor value than the on-board 47K resistor. If the
sensitivity of the device is poor, you can try connecting the signal line to the power
supply through another resistor to determine the best response.
In order to use the shaft-encoder sensors in some sort of velocity control scheme for the
robot, we must first interface the photoreflectors to the microprocessor and store the
ensuing counts for each wheel in 2 variables. One shaft encoder is fed into the pulse
accumulator on port A PA7, corresponding to the digital input PAI/PA7 on the Handy
Board, and the other shaft encoder is fed into PA0, which corresponds to the digital input
TIC3/PA0 on the Handy Board.
The pulse accumulator, associated with port A pin PA7, is an 8-bit counter register and
that makes it very easy to count the number of rising or falling edges input to that pin. In
order to use the pulse accumulator, we have to initialize the pulse accumulator control
register, PACTL, properly. Details of the control register is skipped here, and only the C
code you have to write is presented here. The following C code initializes the pulse-
accumulator system and returns the number of pulses since the last reading.
Once the pulse accumulator hardware has been initialized, it will run in the background,
automatically incrementing the count every time a stripe on the encoder wheel moves
past the photoreflector. The robot's main program does not have to keep track of this
activity but is free to attend to other sensors and actuators. When it needs to know the
encoder count, the main program calls the function get_left_vel( ).
For the encoder wheel connected to port A pin PA0(digital input PA0), more software
complexity is in store. We must use an interrupt to count encoder clicks from the right
wheel because there is only one pulse accumulator in the 68HC11 microprocessor. We
will use the IC3 register associated with PA0 to generate an interrupt on every rising
edge. The interrupt-handler routine, which automatically runs whenever a rising edge is
detected, must increment a counter, clear the interrupt flag, and return from the interrupt.
In order to use the interrupt handler, we have to initialize a few associated registers in a
way similar to setting up the pulse-accumulator. The following C code fragment will
initialize the associated registers properly:
You can then insert these codes into your variable declaration section and the subroutine
that initialize the pulse-accumulator, e.g. init_velocity( ).
To write the interrupt-handler routine for the inpuut capture register IC3, you have to
write the handler in the 68HC11 assembly language.. The Interative C compiler has a
means of interfacing to assembly language routines. The following code fragment counts
the shaft-encoder pulses and stores the running sum in a global variable right_clicks.
subroutine_initialize_module:
ldd #IC3_interrupt_handler ; 16 bit address of interrupt
handler
std $FFEA ;Store in IC3 interrupt vector
cli ;Enable interrupts generally
rts ;return from subroutine
variable_right_clicks:
fdb 0 ;fill double byte, 16 bits right_clicks = 0
IC3_interrupt_handler:
ldd variable_right_clicks
addd #1 ;add one more encoder count
std variable_right_clicks
ldaa #%00000001 ;Clear the IC3 flag by writing a one
staa TFLG1 ;Store in TFLG1 to clear IC3 flag
rti ;return from interrupt
To use the above assembly program with the Handy Board, you have to copy the code
fragment to the online ICB complier at http://www.newtonlabs.com/ic/icb.html and then
assemble it. Then, you should save the resulting icb file to a file with an extension .icb.
You can then download the .icb file just like any IC files.
To complete the C program, you have to write another subroutine to return the number of
clicks from the global variable. The following subroutine will handle this:
{ int vel;
vel = right_clicks;
right_clicks = 0; /*reset for next call*/
return (vel) ; }