Explore
Engineering at UD
Introduction to Sensors
1. Sensors and Transducers
A sensor is a device that responds to a stimulus, such as heat, light, or pressure, and generates a signal that can be measured or interpreted. We are mainly interested in sensors that change their electrical properties in response to some environmental change or stimulus. Electrical or electronic sensors are particularly useful because we can readily connect them to a computer and use their output to control a system. This allows a computer to “sense” its environment in some way vary its output accordingly. Using a computer (referred to as a microcontroller in this context) equipped with one or more sensors to control a system, is something you will find in just about every modern device you can think of including your refrigerator, oven, cell phone, the climate control in your house and this building, a car engine (fuel, water, etc). Today, we are going to explore the use of sensors in robotics!
A class of devices that are intimately related to sensors are transducers. A transducer is a device that converts one type of energy into another. An example of a transducer is a microphone, which is also a sensor (an acoustic or sound sensor). A microphone converts acoustic pressure waves (sound) into a time varying voltage. A speaker is also a transducer (not usually considered a sensor, but we will see that it can be used that way). It converts time varying voltage into sound. We will begin our exploration of sensors by experimenting with microphones and speakers. We will need to use a powerful piece of equipment, called an oscilloscope, to view the time-varying voltages (signals) involved.
More on sensors can be found here: http://en.wikipedia.org/wiki/Sensor.
Here is an extensive list of sensors used in robotics:
http://www.andrew.cmu.edu/user/rjg/websensors/robot_sensors2.html
1.1 Oscilloscope
We use an oscilloscope, shown in Figure 1, to display and measure time-varying voltages. Time is represented on the horizontal axis and voltage is shown on the vertical axis. Connect the signal generator to the oscilloscope using a coaxial cable with BNC connectors (cable TV connectors). Some additional information on oscilloscopes can be found here: http://www.doctronics.co.uk/scope.htm.

Figure 1:
Typical oscilloscope and its basic controls.
1.2 Speakers
In the early 1800’s, Michael Faraday observed that when current flows through a wire, a magnetic field is created that can be observed by placing iron filings nearby and watching them align with the magnetic field (like they do when placed around a fixed magnet). Furthermore, Faraday determined that when a magnet is moved near a wire, an impetus for charge to move is observed (i.e., a voltage, the driving force for current). The same affect is observed if the wire moves relative to a fixed magnet. His work brought about the widespread understanding that electric and magnet forces are inexorably linked. Faraday’s insightful observations have led to inductors, transformers, electric motors and generators, relays, as well as a variety of transducers such as speakers and microphones. These transducers convert electrical signals into acoustic signals and vice versa.
A speaker can be made by attaching a paper cone to an electromagnet. This electromagnet is simply a coil of wire wrapped around an iron core. The electromagnet is set inside a fixed magnet. When current flows through the electromagnet coil, it becomes a magnet which wants to move inside the fixed magnet, in order to satisfy its natural tendency to align its North-pole to the fix magnet’s South-pole. The amount of movement is proportional to the current. This gives us a way to convert an electrical signal into mechanical movement, which creates an acoustic signal. By the way, a speaker can be used as a microphone by operating it in reverse. Talking into a speaker causes the cone to move, which moves the wires of the electromagnet with respect to the fixed magnet. This induces a voltage. It is not an efficient microphone, because the cone requires a strong acoustic signal to make it move, but it does work.
Here is a nice illustration of the operation of a magnet-based speaker:
http://electronics.howstuffworks.com/speaker6.htm
Turn down the amplitude of the
signal generator to nearly zero and set the frequency to 440Hz (“A” above middle “C” on a piano). Connect a speaker to the output of the signal
generator (along with the oscilloscope in parallel). Press the autoscale
button on the oscilloscope. Slowly and
carefully increase the amplitude until you can hear the signal. Sweep the frequency and observe the output on
the oscilloscope and note the tone you hear.
Does what you hear and what you see on the
oscilloscope correlate? What is the minimum and maximum frequency you can hear
(WITHOUT increasing the amplitude)?
1.3 Microphone
A microphone, simply put, is a speaker operating in reverse. Acoustic energy is converted into a time-varying voltage. Connect a microphone to the oscilloscope and observe the output. Hum into the microphone and press the autoscale button on the oscilloscope as you continue to hum. Have fun with it. Can you relate what you see with what you hear? Note that one can build a sound sensor for the RCX and make a robot respond to sound. The instructor will demonstrate such a robot. Here is a design for a Lego RCX robot sound sensor:
http://www.plazaearth.com/usr/gasperi/sound.htm.
1.4 Using a speaker as microphone
Connect the
speaker as is if it were a microphone and see if you can detect the signal
formed by speaking into the speaker cone.
The sensor input circuit for the RCX can be accurately modeled using the circuit in Figure 2. The RCX produces a 5V signal that passes through an internal 10k Ohm resistor to the output terminals. The voltage across the output terminals (labeled V0) is measured automatically and converted into a raw number that ranges from 0 to 1023. This means that 0Volts maps to 0, and 5Volts maps to 1023. The output voltage, V0, will vary depending on what is attached (forming a sensor).
Using circuit analysis, we can show that the output voltage is given by the “voltage divider” equation
.
When Rx is very high, the output voltage is approximately 5V and the numerical value returned by the sensor is approximately 1023. When Rx is small (or even 0 Ohms for an ideal wire), the output voltage is approximately 0V, and the sensor output is approximately 0. Thus, the sensor output is proportional to the resistance Rx.
2.1 Rotation angle
sensor
We are going to first create a rotation sensor using a variable resistor (or potentiometer). As you turn the potentiometer, the resistance between the middle and end terminals varies. This variable resistance will be attached to the sensor terminals forming Rx. Connect the potentiometer to the bread board and connect it to the RCX middle sensor as instructed. Press the “view” button on the RCX until the middle sensor input has an arrow under it. Now, turn the potentiometer to change Rx and observe the number display. This is the sensor reading that can be used to control the robot in a program. If time permits, may also wish to use the Ohm meter to measure the varying resistance of the potentiometer.

Figure
2: RCX
Touch sensor input model. The Voltage V0
at the terminals is measured and quantized in a 10 bit analog to digital
converter yielding raw values ranging from 0 to 1023.
2.2 Seeing the Light!
Next we will explore how to make an ambient light sensor using a device known as a photoresistor put in place of Rx (where the potentiometer was). Note that a photoresistor is a resistor whose resistance depends on the number of photons striking its cell. So, observe the numerical value for your middle sensor when the photoresistor is exposed to bright light and low light. What is the relationship? How can you use such a sensor?
2.3 Programming the
robot using the light sensor
The instructors will guide you through a few programs for your robot that use the light sensor you just created.
Program 1: Light
Activated Robot
task main()
{
SetSensorType(SENSOR_2,SENSOR_TYPE_TOUCH);
SetSensorMode(SENSOR_2,SENSOR_MODE_RAW);
while(true)
{
if(SENSOR_2
> 80)
{
OnFwd(OUT_A+OUT_C);
}
else
{
Off(OUT_A+OUT_C);
}
}
}
Program 2: Light
Activated Line Follower Robot
task main()
{
setup_sensors()
while(true)
{
if(SENSOR_2
<80)
{
track_line();
}
else
{
stop_robot();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// functions (subroutines) called above to perform specific tasks //
//////////////////////////////////////////////////////////////////////////////////////////////////
void setup_sensors()
{
SetSensor(SENSOR_1,SENSOR_LIGHT);
SetSensorType(SENSOR_2,SENSOR_TOUCH);
SetSensorMode(SENSOR_2,SENSOR_MODE_RAW);
SetSensor(SENSOR_3,SENSOR_LIGHT);
}
void track_line()
{
if(SENSOR_1
< 45)
{
OnFwd(OUT_C);
Off(OUT_A);
}
else if(SENSOR_3 <
45)
{
OnFwd(OUT_A);
Off(OUT_C);
}
else
{
OnFwd(OUT_A+OUT_C);
}
}
void stop_robot()
{
Off(OUT_A+OUT_C);
}
Program 3:
Firefighter robot
task main()
{
setup_sensors()
while(true)
{
if(SENSOR_2
>100) // Is bumper free (nor
pressed)?
{
track_light();
}
else
{
extinguish_candle();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// functions (subroutines) called above to perform specific tasks //
//////////////////////////////////////////////////////////////////////////////////////////////////
void setup_sensors()
{
SetSensor(SENSOR_1,SENSOR_LIGHT);
SetSensor(SENSOR_2,SENSOR_TOUCH);
SetSensorMode(SENSOR_2,SENSOR_MODE_RAW);
SetSensor(SENSOR_3,SENSOR_LIGHT);
}
void track_light()
{
Off(OUT_B); //
Make sure fan is off
if(SENSOR_1 > SENSOR_3) // Right side is dark, turn on right
motor
{
OnFwd(OUT_C);
Off(OUT_A);
}
else // Left side is dark, turn on left
motor
{
OnFwd(OUT_A);
Off(OUT_C);
}
}
void extinguish_candle()
{
// Stop robot and turn on fan
Off(OUT_A+OUT_C);
OnFwd(OUT_B);
}
To run the firefighter robot program successfully, you will need to aim the two light sensors forward (build a mounting structure for them). Next, disconnect the photoresistor from sensor Input 2 at the RCX and connect the bumper sensor. For the dual bumper robots, connect both bumper sensors to Input 2 on the RCX (connecting them in parallel). You will also need to build a mount to hold the fan so that it points forward and is clear off all of the wires when it spins. Finally, you will need to connect the fan to the RCX Output B port and two 1.5V batteries in series (forming a 3V source) according to the schematic below. The instructors will guide you through this process.

Figure 3: Transistor
used as an electronic switch. The RCX
motor output is connected through a 47 (or 100) Ohm resistor to the base of the
transistor. A battery is connected
across the motor and transistor in series.
Only when the transistor is conducting will current flow
trough the motor. The transistor
conducts when current enters the base from the RCX.
Appendix
Resistors and
Resistance
To better understand how the Lego RCX brick uses sensors to measure light, touch, temperature, rotation etc., and to build our own sensors, we must understand some basic circuit analysis. We begin by introducing a circuit component called a resistor. A resistor is any physical component that impedes the flow of electrical charge. Resistance is measured in units of Ohms. A resistor can be a device such as an incandescent light bulb, heating elements in a toaster or hair dryer, photoresistor, or even a simple length of wire, etc. In addition to these types of resistors, we often want a dedicated element whose only job is to provide a specific fixed resistance for circuits that we design and construct. One commonly used type of dedicated resistor is a device made of ceramic with metal leads as shown in Figure 2. The color code (stripes) on such components indicates the resistance value, as shown in Figure 2. A ceramic resistor like this is what most people mean when they refer to a “resistor.”

Figure 3: Resistor color chart. The color bands indicate the nominal resistance and tolerance (potential deviation) of a given resistor. Note that a 1k Ohm resistor should be blown-black-red (10x100 Ohms) with a gold or silver band at the end to indicate 5% or 10% tolerance.
Breadboard
You will be using a breadboard to construct and test circuits. The breadboards (shown in Figure 3), are included in your tackle boxes. To build circuits, insert components and stripped-end wires into holes. Wires/components plugged into the same row segments will be electrically connected as shown in Figure 3.

Figure 4: Breadboard used to quickly construct and test circuits. Insert components and stripped ends of wires into holes. Wires/components plugged into the same row segments will be electrically connected.