The C library function analog (port-#) is used to return the value of a particular
analog sensor port. For example, the IC statement
val = analog(27);
sets the value of the variable val equal to the output of port #27.
Many devices used as digital sensors are wired to be active low, meaning that
they generate 0 volts when they are active (or true). The digital inputs on the RoboBoard
have a pull-up resistor that makes the voltage input equal to 5 volts when
nothing is connected. A closed or depressed touch switch connected to a digital
port would change that voltage to 0 volts by shorting the input to ground. The
resulting outputs: open switch = 1, and closed switch = 0, are the logical opposite
of what we usually want. That is, we would prefer the output of the digital port to
have value 0 or False normally, and change to 1 or True only when the switch hit
something (like a wall or another robot) and was depressed. The IC library function
digital (port-#), used to read a True-or-False value associated with a particular
sensor port, performs this logical inversion of the signal measured on a digital port.
Hence, the depressed touch switch (measuring 0 volts on the hardware) causes the
digital () function to return a 1 (logic True) or logical True value.
FIGURE 3.32 Generic sensor wiring.
For example, the C statement
if (digital(2)) do_it();
returns a True value (the number 1) and calls the function do_it() if the value at
port #2 was 0 volts (indicating a depressed switch).
Connector Plug Standard
The standard plug configuration used to connect sensors to the RoboBoard is
shown in Figure 3.32. Notice that the plug is asymmetric (made by removing one
pin from a four-pin section of the male header), and is therefore polarized. The
plug can only be inserted in the RoboBoard port in one orientation, so once the
plug is wired correctly, it cannot be inserted into a sensor port backward. This
makes the plug much easier to use correctly, but, of course, if you wire it incorrectly,
you must rewire it since you cannot turn the plug around.
Generally, the sensor is connected to the plug with three wires. Two of the
wires supply 5-volt power from the RoboBoard, labeled +5v and Gnd. The
third wire, labeled Signal is the voltage output of the sensor. It is the job of
the sensor to use the power and ground connections (if necessary) and return its
answer, as a voltage, on the Signal wire.
Sensor Wiring
Figure 3.33 shows a diagram of circuitry associated with each sensor. This circuitry,
residing on the RoboBoard, is replicated for each sensor input channel.
The key thing to notice is the pull-up resistor wired from the sensor input signal
leads to the 5-volt power supply.
There are two reasons why this resistor is used. First, it provides a default
value for the sensor input a value when no sensor is plugged in. Many ICs, such
as those on the board that read and interpret the sensor voltage, do not perform
FIGURE 3.33 Sensor input port circuitry.
well when their inputs are left unconnected. With this circuit, when nothing is
plugged into the sensor port, the pull-up resistor provides a 5-volt bias voltage
on the sensor input line. Thus, the default output value of an analog port is 255,
while the default output value of a digital port is 0 or logic False. (Remember
that the 5-volt default input value would lead to a digital value of 1 except that
this value is inverted by the digital () library function, as explained earlier.)
Second, the pull-up resistor is also used as part of the voltage divider circuit
required for some of the analog sensors, as explained in the following section.
The resistors on the RoboBoard eliminate the need for an additional resistor on
each of the sensors.
The Voltage Divider Circuit
Most of the sensors used in the RoboBoard kit make use of the voltage divider
circuit shown in Figure 3.34. In the voltage divider, the voltage measured at the
common point of the two resistors, Vout, is a function of the input voltage, Vin
(5 volts in this case), and the values of the two resistors, R1 and R2. This voltage
can be calculated using Ohm s law, V=I R. The current, I, flowing through the
circuit shown in the diagram, is Vin / R1+R2 (calculated using the rule that series
resistances add). Then Vout, the voltage drop across R2, is R2 i, which yields the
result:
Vout = Vin (R2 / R1+R2).
In ELEC 201 applications, R1 has a fixed or constant value (as shown in
Figure 3.34), while R2 is the variable resistance produced by the sensor. Vin is
the positive voltage supply, fixed at 5 volts. Thus, the Vout signal can be directly
FIGURE 3.34 Voltage divider schematic.
computed from R2, the resistive sensor. From looking at the equation, it is easy
to see that if R2 is large with respect to R1, the output voltage will be large, and
if R2 is small with respect to R1, the output voltage will be small. The minimum
and maximum possible voltage values are 0 and 5 volts, matching the RoboBoard
input circuitry range.
Tactile Sensors
The primary sensors in the ELEC 201 kit used to detect tactile contact are
simple push-button or lever-actuated switches. Microswitch is the brand name
of a variety of switches that are so widely used that the brand name has become
the generic term for this type of switch, which is now manufactured by many
companies. A microswitch is housed in a rectangular body and has a very small
button (the switch nub ) which is the external switching point. A lever arm on
the switch reduces the force needed to actuate the switch (see Figure 3.35).
Microswitches are an especially good type of switch to use for making touch
sensors.
FIGURE 3.35 A typical microswitch.
FIGURE 3.36 Robotic platform employing a bumper coupled to a touch sensor.
Often, the switch is simply mounted on a robot so that when the robot runs into
something, the switch is depressed, and the microprocessor can detect that the robot
has made contact with some object and take appropriate action. However, creative
mechanical design of the bumper-switch mechanism is required so that contact with
various objects (a wall, a robot, etc.) over a range of angles will be consistently detected.
A very sensitive touch bumper can be made by connecting a mechanism as
an extension of the microswitch s lever arm, as illustrated in Figure 3.36.
Limit Switch
Touch sensors can also serve as limit switches to determine when some movable
part of the robot has reached the desired position. For example, if a robot arm is
driven by a motor, perhaps using a gear rack, touch switches could detect when
the arm reached the limit of travel on the rack in each direction.
Switch Circuitry
Figure 3.37 shows how a switch is wired to a sensor input port. When the switch
is open (as it is shown in the diagram), the sensor input is connected to the
5-volt supply by the pull-up resistor. When the switch is closed, the input is
connected directly to ground, generating a 0-volt signal (and causing current to
flow through the resistor and switch).
Most push-button-style switches are normally open, meaning that the
switch contacts are in the open-circuit position when the switch has not been
pressed. Microswitches often have both normally open and normally closed contacts
along with a common contact. When wiring a microswitch, it is customary
to use the normally open contacts. Also, this configuration is the active-low
mode expected by the standard library software used to read the output values
FIGURE 3.37 Touch switch circuit.
from digital sensor ports. However, you can wire the switch differently to perform
some special function. In particular, several switches can be wired in series
or parallel and connected to a single digital input port. For example, a touch
bumper might have two switches, and the robot only needs to know if either of
them (#1 OR #2) are closed. It takes less code and less time to check just one
digital port and to use parallel switch wiring to implement the logic OR function
in hardware.
Mercury Tilt Switch
As the name suggests, a mercury tilt switch contains a small amount of mercury
inside a glass bulb. The operation of the switch is based on the unique
properties of mercury: it is both a conductor and a liquid. When the switch
tilts mercury flows to the bottom of the bulb closing the circuit between two
metal pins.
The mercury tilt switch can be used in any application to sense inclination.
For example, the tilt switch could be used to adjust the position of an arm or
ramp. Most thermostats contain a mercury tilt switch mounted on a temperature
sensitive spring. Changes in temperature tilt the switch, turning the furnace or
air conditioner on or off.
Light Sensors
Measurement of light provides very valuable information about the environment.
Often, particular features of the game board (such as goals) are marked
by a strong light beacon. The board surface has contrasting lines that can be
detected by the difference in the amount of light they reflect. A variety of light
sensors are provided in the ELEC 201 kit:
Infrared Reflectance Sensor
This device combines an infrared LED light source and a phototransistor light
detector into a single package. The LED and the detector point out of the
package, almost parallel to each other. The detector will measure the light
scattered or reflected by a surface a short distance away. The package also
contains an optical filter (colored plastic) that transmits primarily only the infrared
light from the LED; this reduces, but does not eliminate, the sensitivity
to ambient light.
Infrared Slotted Optical Switch
This device is similar to the IR reflectance sensor, in that it contains both an infrared
source and a filtered infrared phototransistor detector. However, the two
are mounted exactly opposite each other with a small, open gap between them.
The sensor is designed to detect the presence of an object in the gap that blocks
the light.
Modulated Infrared Light Detector
This device senses the presence of infrared light that has been modulated (e.g.,
blinks on and off) at a particular frequency. These devices are typically used to
decode the signals of TV remote controls, but are used in the ELEC 201 application
to detect the infrared beacon of the opponent robot.
Photocell
This device is a light-dependent resistor. It is most sensitive to visible light in
the red.
Photocells are made from a compound called cadmium sulfide (CdS) that
changes resistance when exposed to varying degrees of light. Cadmium sulfide
photocells are most sensitive to visible red light, with some sensitivity to other
wavelengths.
Photocells have a relatively slow response to changes in light. The characteristic
blinking of overhead fluorescent lamps, which turn on and off at the
60 Hertz line frequency, is not detected by photocells. This is in contrast to
phototransistors, which have frequency responses easily reaching above 10,000
Hertz and more. Therefore, if both sensors were used to measure the same fluorescent
lamp, the photocell would show the light to be always on and the phototransistor
would show the light to be blinking on and off.
Photocells are commonly used to detect the incandescent lamp that acts as
a contest start indicator. They are also used to find the light beacons marking
certain parts of the board, such as the goals. While they can be used to measure
the reflectivity of the game board surface if coupled with a light source such as a
red LED or an incandescent lamp, the IR reflectance sensors are usually better
at this function. Photocells are sensitive to ambient lighting and usually need to
be shielded. Certain parts of the game board might be marked with polarized
light sources. An array of photocells with polarizing filters at different orientations
could be used to detect the polarization angle of polarized light and locate
those board features.
The photocell acts as resistor R2 in the voltage divider configuration. The
resistance of a photocell decreases with an increase in illumination (an inverse
relationship). Because of the wiring of the voltage divider (the photocell is on
the lower side of the voltage divider), an increase in light will correspond to a
decrease in sensor voltage and a lower analog value.
Infrared Reflectance Sensor
The infrared reflectance sensor is a small rectangular device that contains a
phototransistor (sensitive to infrared light) and an infrared emitter. The amount
of light reflected from the emitter into the phototransistor yields a measurement
of a surface s reflectance, for example, to determine whether the surface
is black or white. The phototransistor has peak sensitivity at the wavelength of
the emitter (a near-visible infrared), but is also sensitive to visible light and infrared
light emitted by visible light sources. For this reason, the device should
be shielded from ambient lighting as much as possible in order to obtain reliable
results.
The amount of light reflected from the emitter into the phototransistor
yields a measurement of a surface s reflectance (when other factors, such as the
distance from the sensor to the surface, are held constant). The reflectance sensor
can also be used to measure distance, provided that the surface reflectance is
constant. A reflectance sensor can be used to detect features drawn on a surface
FIGURE 3.38 Phototransistor and infrared emitter circuit.
or segments on a wheel used to encode rotations of a shaft. It is important to
remember that the reflectivity measurement indicates the surface s reflectivity at
a particular wavelength of light (the near-visible infrared). A surface s properties
with respect to visible light may or may not be indicators of infrared light reflectance.
In general, though, surfaces that absorb visible light (making them appear
dark to the eye) will absorb infrared light as well. The sensor part (the phototransistor) can be used alone as a light sensor, for example, to detect the starting light, and it is usually much more sensitive than the photocell.
Phototransistor
The light falling on a phototransistor creates charge carriers in the base region
of a transistor, effectively providing base current. The intensity of the light determines
the effective base drive and thus the conductivity of the transistor.
Greater amounts of light cause greater currents to flow through the collector-emitter
leads. Because a transistor is an active element having current gain, the
phototransistor is more sensitive than a simple photoresistor. However, the increased
sensitivity comes at the price of reduced dynamic range. Dynamic range
is the difference between the lowest and highest levels that can be measured.
The RoboBoard analog sensor inputs have a range of 0 5 volts, and relatively
small variations in light can cause the phototransistor output to change through
this range. The exact range depends on the circuit used.
As shown in Figure 3.38, the phototransistor is wired in a configuration similar
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.
Infrared Emitter
The Light Emitting Element (an LED) uses a resistor to limit the current
that can flow through the device to the proper value of about 10 milliamps.
Normally the emitter is always on, but it could be wired to one of the LED
output ports if you wanted to control it separately. In this way you could use
the same sensor to detect the starting light (using the phototransistor with
the emitter off) and then to follow a line on the board (normal operation with
the emitter on).
Infrared Slotted Optical Switch
The infrared slotted optical switch is similar to the infrared reflectance sensor
except that the emitter is pointed directly at the phototransistor across a small
gap. As the name implies, the slotted optical switch is a digital sensor, designed
to provide only two output states. The output of the switch changes if something
opaque enters the gap and blocks the light path. The slotted optical switch is
commonly used to build shaft encoders, which count the revolutions of a shaft.
A gear or other type of wheel with holes or slots is placed in the gap between the
emitter and detector. The light pulses created by the turning wheel can be detected
and counted with special software to yield rotation or distance data. This
detector also might be used to detect when an arm or other part of the robot has
reached a particular position by attaching a piece of cardboard to the arm so that
it entered the gap at the desired arm position.
The slotted optical switch operates in the same fashion as the infrared reflectance sensor, with the exception that a different value of pull-up resistor must
be added externally for the particular model of optical switch we use.
Modulated Infrared Light Detector
The modulated infrared light detector is a device that combines an infrared phototransistor
with specialized signal-processing circuitry to detect only light that is
pulsing at a particular rate. The ELEC 201 kit includes the Sharp GP1U52 sensor,
which detects the presence of infrared light modulated (pulsed) at 40,000
Hz. Normal room light, which is not modulated, does not affect the sensor, a big
advantage. This type of sensor is used for the remote control function on televisions,
VCRs, etc. In ELEC 201 this sensor is used to detect the specially modulated
infrared light emitted by the beacon on the opponent robot. The software
can distinguish different pulse patterns in order to distinguish between the beacons
on the two robots. (In a television remote, different pulse patterns would
correspond to different functions, such as changing the channel up or down.)
The principles of operation and use are explained later, when we discuss the
circuit used to create the modulated infrared light for the beacon.
Other Sensors
Magnetic Field SensoAvrs
The ELEC 201 kit contains both an analog sensor that provides information
about the strength of the magnetic field and a digital sensor, a magnetic
switch.
A device called a hall-effect sensor can be used to detect the presence and
strength of magnetic fields. The hall-effect sensors have an output voltage even
when no magnetic field is present, and the output changes when a magnetic
field is present, the direction of change depending on the polarity of the field.
The digital magnetic sensors are simple switches that are open or closed. Internally the switches have an arm made of magnetic material that is attracted
to a magnet and moves to short out the switch contacts. These switches are
commonly used as door and window position sensors in home security systems.
The switch will close when it comes within 1" of its companion magnet.