62 November 2014
lot more of the computer’s resources to do calculations
that do integers. Using the integer simplification would
probably be good enough for most applications and
would save us some serious computing. However, since
we aren’t doing a lot of extra work with this program, we
will go ahead and write the code using floating point data
types. (To rephrase: In a more constrained program that
does a lot of work and takes up a lot of space, we
probably wouldn’t use floating point, but accept the error
and say that two ADC steps equals 1°C.)
Or, saying this another way, we know that the
temperature sensor will output voltages between 100 mV
and 1,750 mV to indicate - 40°C to +125°C. The ADC
outputs values from 0 to 1023 in steps representing
4.88 mV per step. This tells us that the lowest valid ADC
reading will be for 100 mV giving us: 100 mV/4.88
mV/step = 20. 49 steps. Of course, these steps are integers
so we will say that the lowest valid step value is 21, which
equals 21 steps 4.88 mV/step = 102.48 mV.
We know the temperature sensor outputs 100 mV for
- 40°C, so 102.48 mV is slightly above that. We know that
the sensor outputs 10 mV per °C, so we can calculate
2. 48 mV / 10 mV/ °C = .248°C, giving us a temperature of
- 40°C + .248 °C = - 39. 75°C. Of course, our real accuracy
isn’t quite that good, but for our purposes here, we’ll say
that our systems lowest accurate temperature is - 39. 75°C.
We can repeat this logic to calculate the highest
accurate temperature reading: 1750 mV / 4.88 mV/step
= 358.61 steps (359 steps since it is an integer). However,
when we calculate 359 steps 4.88 mV/step = 1751.92
mV, we see that the 359 takes us above the highest
accurate temperature output (that is, 125°C at 1750 mV),
so let’s drop the maximum step to 358 and calculate 358
steps 4.88 mV/step = 1747.04 mV, which is in range.
We now see that the range of accurate readings from
our ADC will be 21 to 358 out of a possible range of 0 to
1023. For our purposes, we should consider any ADC
readings below 21 or above 358 as invtalid, and ignore
them when we do our software conversions of the ADC
readings to voltage and then to °C.
Since this discussion is fairly complex, we will have a
lab to use all of this in a test program to convert fake ADC
readings to voltage and °C.
Lab 1: Sensing Relative Light Levels
This lab implements the light measurement discussed
previously. This circuit works like the one back in Figure 3,
except we will use our hand instead of clouds to vary the
light falling on the sensor. We will then use the Arduino
ADC to measure the changing voltage across the resistor.
1 USB cable
1 Arduino proto shield and jumper wires
1 CdS light sensor
1 10,000 Ω resistor
Estimated time for this lab: 30 minutes
Check off when complete:
❏ Build the circuit shown in Figures 7, 8, and 9.
❏ Load the following program into the Arduino IDE
■ FIGURE 8: CdS sensor circuit schematic.
■ FIGURE 7: