By Stuart Ball, Embedded Systems Programming
Sensors help microprocessor-based systems find their bearings. Where would embedded
systems be without them?
Sensors provide the windows through which microprocessor-based systems can
observe their environment. Optical and magnetic sensors are used to detect the
presence, absence, or motion of an object. Typical applications include sensing
when a door is open or closed, or measuring the speed of a rotating shaft. This
article looks at these two broad classes of sensors, their applications, and how
they interface to processors.
Figure 1: Optical switches
Figure 1a shows a slotted optical switch. An LED is
mounted in a plastic housing, facing a phototransistor, but separated by a gap.
If something moves into the gap, it blocks the light path between the LED and
Slotted switches are often used to detect motor speed by placing a slotted
wheel on the motor shaft; as the shaft rotates, it alternately blocks and
unblocks the light path. Slotted switches are also used as indicators when a
door or hood is open or closed. A flag on the door drops into the slot and
blocks the light when the door is closed.
Figure 1b shows a reflective sensor, which works similarly. The
phototransistor in a reflective sensor picks up reflected light from whatever is
in front of the switch. Most reflective sensors have a focal length, which is
the optimum distance for the object to be detected to be placed, usually between
0.1 and 0.5 inches. A reflective sensor is typically used to detect motor motion
by painting or anodizing the motor shaft black, then placing a strip of
reflective material on the shaft. As the shaft rotates, the sensor sees no
reflection from the part of the shaft that is black, then high reflection from
the reflective strip.
Slotted and reflective optical sensors share a schematic symbol, which is
shown in Figure 1c. Both types also have some common characteristics that must
be taken into account when designing them into a system.
Current transfer ratio
The LED and phototransistor pair have
limited gain, usually less than one. The amount of current generated in the
phototransistor collector for a given current through the LED is called the
current transfer ratio (CTR). A typical CTR for a slotted switch is 0.1. This
means that 10mA of current in the LED will result in 1mA of current in the
collector. The CTR is sometimes specified as a ratio and sometimes in a table
that shows the collector current for various values of LED current. The CTR is
dependent on the LED and phototransistor characteristics and can vary widely
from one optical sensor to the next.
Figure 2: Circuit for connecting an optical sensor to a
digital I/O pin
The current transfer ratio has several implications when you want to
interface an optical sensor to a processor. First, if you want to connect the
switch directly to a digital input as shown in Figure 2, the transistor output
has to swing between valid logic levels. To ensure that the phototransistor
saturates, the value of the pullup resistor is limited. For example, if you're
driving the LED with 10mA and the CTR has a minimum value of 0.1, then the
pullup resistor will need to be around 5kΩ.
A smaller resistor would provide better noise immunity (lower impedance) and
possibly faster speed, but wouldn't work with all optical sensors. The
transistor must be able to sink enough current to ensure a valid logic low
level. If you want to use a smaller pullup, you could either use an optical
switch with a higher CTR or drive the LED with more current.
Optical switches are available with darlington transistor outputs, which
often have a CTR higher than 1. However, they are typically only 20% as fast as
a single transistor output and have a higher saturation voltage.
Reflective sensors also have a CTR. Since the sensor depends on reflected
light, the CTR is dependent on the type of surface and the distance of that
surface from the sensor. The CTR of a reflective sensor is normally established
with a standard reflective surface, placed at the specified focal length from
The CTR of a reflective sensor varies from device to device and with the
application. If your sensor is aimed at a surface that switches between gray and
black, you will not get the same CTR you get with the white reference used by
the manufacturer. Your design has to accommodate the actual CTR resulting from
your application of the sensor. One way to determine the range of CTR is to
measure the CTR in your application, then compare that to the CTR of the same
sensor using the same white reference used by the sensor manufacturer. This will
give you an idea of the CTR range you can expect to see.
Since the CTR of an optical sensor has a wide range, you may want to connect
the output of the sensor to an analog-to-digital converter (ADC). This allows
the software to look for changes in the output level, rather than depending on
the ability of the part to generate digital logic levels. The price for this
capability, of course, is the cost of adding an ADC and the need for more time
for ADC sampling.
The phototransistor in any optical sensor is fairly slow.
This limits the maximum speed that can be detected. Typical numbers are 8ms
turnon time and 50ms turnoff time.
If the drive LED in a sensor is software-controlled, the software must take
the turnon and turnoff delay into account when reading the output of the sensor.
Mechanical jitter can cause strange results
with reflective sensors. I saw a system once that used a reflective sensor to
count rotations by looking at a shiny strip on a flat black motor shaft. The
output of this sensor circuit generated an interrupt. Occasionally, the motor
would stop with the shiny strip right at the edge of the detection area for the
sensor. Machine vibration would then generate enormous numbers of interrupts to
the processor, effectively shutting it down.
You could envision a similar situation with a slotted sensor, if the flag
that interrupts the light path only partially obscures the phototransistor. This
could leave the phototransistor halfway on, causing an ambiguous output. Good
hardware design, such as the use of hysteresis in comparator circuits, will
minimize these problems.
Reflective sensors require some additional considerations. Reflective sensors
are frequently used to sense objects of differing types. A good example would be
paper in a high-speed sorting mechanism. The paper has varying quality, color,
and reflective properties. The sensor system must be designed to handle all the
types of material used. In the motor speed application, what happens if a film
of oil coats the flat black portion of the shaft? What does that do to the
sensor's ability to detect rotation?
In some cases, you may have to add hardware or software (or both) to detect
unusual conditions. In the example, where a reflective sensor generated
excessive interrupts, the software might have a timer that keeps track of the
time between interrupts. If the sensor interrupt service routine is exited and
immediately reentered, the interrupt service routine could disable the interrupt
and set a flag to tell the rest of the system that something is wrong.
In systems where safety is an issue, be sure that a
failed sensor doesn't cause the system to operate in an unsafe manner. A typical
example would be a safety hood that must be closed before the machine can start.
The idea is that all the dangerous moving parts are under the hood, so if the
hood is closed you know the operator's hands are out of the way.
You could solve this problem with a slotted optical switch and a flag that
blocks the light path when the hood is closed. You then connect the
phototransistor emitter to ground and pull the collector up with a resistor.
When the flag is blocking the sensor, the transistor is off and the output is
The problem with this approach is that an open or disconnected LED would
appear the same to the system as a closed hood. The system might then try to
start with the hood open. In a case like this, use a flag that instead clears
the path when the hood is closed. A failed LED then looks like an open hood and
everything is safe.
An even safer method would use two sensors, one that's blocked when the hood
is open and one that's blocked when the hood is closed. For complete operator
safety, the machine isn't allowed to start unless both sensors are in the
correct (hood closed) state.
You occasionally need to know if the LED in an optical sensor has failed. Say
you use a slotted switch to determine if a motor is turning. If the motor
appears to stop, you might need to know if the motor is jammed or if the sensor
LED has failed (or been disconnected) so you can display the correct diagnostic
Figure 3: Detecting a failed LED
Figure 3 illustrates a simple way to detect a failed LED. A comparator senses
the voltage at the LED anode. When the LED is on, it will have a voltage drop
around 1.2V (typical), so the comparator output will be high. If the LED opens,
the voltage at the anode will rise to Vcc. (For this to work, Vcc must be
greater than 3V.)
The circuit as shown is for an LED that's on all the time. You can also use
this method for a switched LED, but you've got to take the voltage drop across
the switching transistor into account when selecting the reference voltage. The
software must ignore the comparator output when the LED is turned off.
Although a disconnected LED is much more likely than a shorted LED, you could
also add a second comparator to detect the shorted condition. The reference
voltage would be around 0.6V, and the software would declare an error if the
voltage were to drop below the reference.
addition to slotted switches and reflective sensors, optical sensors are also
used in optical isolators, and discrete optical emitters/receivers are also
An optical isolator (also called an
optoisolator or optocoupler) houses an LED and a phototransistor
in a package like an IC. An optical isolator isn't used to detect mechanical
motion. Instead it provides electrical isolation between two electrical
circuits. The optical isolator is sealed, so there's no way to break the light
A common use for optoisolators is to isolate a high-voltage circuit from the
microprocessor that controls it. Musical instrument digital interface (MIDI)
technology uses optical isolation to interconnect electronic musical
instruments, preventing problems caused by differing ground potentials.
Figure 4: An optical isolator
Figure 4 shows how an optoisolator can be used to pass signals from one
circuit to another. The ground and power connections for the system may be
completely separate. Even in a single system where the grounds are nominally the
same, an optoisolator may be used to prevent ground loops, or to keep noise in
one ground (such as pulse-width modulation motor noise) out of the logic/analog
Some optoisolators are available with logic outputs instead of
phototransistor outputs. These devices have additional circuitry inside the IC
to convert the analog output into a digital level.
Optoisolators have the same gain and speed issues that optical sensors do.
However, the CTR of an optoisolator can be higher, typically in the 0.2 to 1
range, because the LED is closer to the phototransistor base.
The speed of an optoisolator is usually better than for an optical switch.
The common 4N35 optoisolator has turnon and turnoff times of 10ms each, so it
can pass signals over 10kHz. For high-speed isolation, a fast optoisolator is
normally used. The 6N136 is capable of speeds up to about 1MHz. This part uses a
photodiode coupled to a transistor to achieve high speed.
Discrete optical sensors
A design occasionally calls for the use of
discrete optical parts"an LED and a phototransistor. These are usually infrared
parts, like those in packaged optical switches. They are normally used to detect
when an object is blocking the light between the LED and phototransistor, but in
places where physical characteristics preclude using a slotted switch.
Discrete parts are connected and used in the same way as an optical switch or
optoisolator, but with a few additional considerations. Since the distance
between sensor and phototransistor is usually larger, the CTR is lower. The
circuit often needs a software adjustment for LED current or sensing threshold
for reliable and repeatable operation. In some cases, a lens may be required on
one of the parts to focus the light. A software adjustment/gain scheme can also
compensate for cases where the LED and phototransistor are on separate
mechanical assemblies and the tolerance stackup causes misalignment.
In a packaged optical switch, the LED and phototransistor are matched to the
same infrared (IR) wavelength. Although most IR phototransistors and LEDs will
work together, these parts do operate at different peak wavelengths in the IR
range. When using discrete parts, it's best to select an LED and phototransistor
that are designed for the same IR range. If the parts have different ranges,
then an LED at one end of its range and a phototransistor at the other end of
its range may result in a system with a lowered CTR.
Other optical sensors
Other optical sensors include photodiodes and
charged coupled device arrays. We unfortunately don't have room here to discuss
those devices, but you can obtain more information about them in my book,
Analog Interfacing to Embedded Microprocessors, (Woburn, MA: Newnes,
Probably the simplest magnetic sensor to use in an
embedded application is a Hall effect sensor. The Hall effect, discovered
by Dr. Edwin Hall in 1879, is the production of a voltage in a current-carrying
semiconductor in the presence of a magnetic field. This voltage is proportional
to the current flowing in the sheet and the magnetic flux density.
A Hall effect sensor is made from silicon, and the voltage produced is only a
few microvolts per gauss. Consequently, a high gain amplifier is required to
bring the signal from the Hall element to a useable range. Hall effect sensors
integrate the amplifier into the same package as the sensor element.
Hall effect sensors are available as sensors that produce an output
proportional to the magnetic field, or as switches that change state when the
magnetic field exceeds a certain level. Analog Hall effect sensors are suited to
applications where you need to know how close a magnet is to the sensor"such as
sensing whether an oscillating arm is really moving. Hall effect switches are
best for applications where you just need to know if a magnet is near the
sensor, such as sensing whether a safety hood is closed or open.
The output of an analog Hall effect sensor can be connected to a comparator
or ADC like any other voltage-output sensor. One caution"some analog output
sensors provide an output that's proportional to the supply voltage. For an
accurate noise-free output, you must power the sensor from a noise-free,
well-regulated supply. A typical analog Hall effect sensor will produce an
output that is halfway between the supply voltage and ground when no magnetic
field is present. When a north pole is near the sensor, the voltage moves toward
ground, and when a south pole is near the sensor the voltage moves toward the
Hall effect switches produce a digital output to indicate the presence of a
magnetic field. They drive the output active when a certain magnetic strength
(the operate point) is sensed, then drive the output inactive when the
magnetic field drops below a certain level (the release point). There's
some hysteresis in the range, where the release point is less than the operate
Hall effect switches come in two varieties"unipolar and bipolar, sometimes
called nonlatching and latching. Bipolar switches have a positive (south pole)
operate point and a negative (north pole) release point. Unipolar switches have
a positive (south pole) operate point and a less-positive release point. In both
cases, the actual operate and release points vary with temperature. Both bipolar
and unipolar switches typically have an open-collector output that has to be
pulled up with an external resistor.
Hall effect sensors are commonly available in 3-lead packages similar to the
TO-92 transistor package. The three leads are power, ground, and output. Typical
supply voltages are 5 to 10V, although some sensors operate up to 30V or more.
When using a Hall effect sensor, remember to account for stray magnetic fields.
If using a magnet on, say, a rotating shaft, be sure that the magnet doesn't
excessively magnetize the shaft itself, or this may affect the output of the
Remember that the magnetic field falls off with the approximate square of the
distance. The output of an analog Hall effect sensor may be linear with respect
to the strength of the magnetic field, but it will not be linear with respect to
Figure 5: A geartooth Hall effect sensor
Geartooth Hall effect sensors include a magnet and Hall effect sensor
in one package. They're designed to measure the rotation of a geared device by
placing the sensor near the gearteeth as shown in Figure 5. As each geartooth
moves past the sensor, it affects the magnetic field between the magnet and the
Hall effect sensor, causing an output pulse to be generated.
Other magnetic sensors include the variable reluctance sensor (VRS), linear
variable differential transformer (LVDT), and devices such as a Hall effect
potentiometer made by Clarostat.
Optoisolators and magnetic sensors provide a versatile
solution to numerous position and motion sensing needs, and there's probably one
that's well-suited to your application.
Stuart Ball is an electrical engineer with over 20 years of experience
designing embedded systems. He is the author of three books, Embedded
Microprocessor Systems: Real World Design, Debugging Embedded Microprocessor
Systems, and Analog Interfacing to Embedded Systems, all published by
Butterworth-Heinemann. His e-mail address is email@example.com.
Ball, Stuart R. Analog
Interfacing to Embedded Microprocessors, Woburn, MA: Newnes,
Kodak CCD Primer
information, from CentroVision Inc.