Datasheet

Chapter 6 Light-Sensitive Navigation with Phototransistors
184Robotics with the BOE Shield-Bot
Example: the analogRead function returns 645; how many volts is that? Answer:
V 3.15
V 253.14941406
V
V
=
×=
1024
5
645
The sketches have been calling the
volts function with volts(A3). When they do that, they
pass
A3 to its adPin parameter. Inside the function, analogRead(adPin)
becomes
analogRead(A3). It returns a value in the 0 to 1023 range that represents the
voltage applied to A3. The
analogRead call returns an integer, but since it is nested
in
float(analogRead(adPin), that integer value gets converted to floating point. Then, it’s
multiplied by the floating point value 5.0 and divided by 1024.0, which converts it to a
voltmeter value (just like we converted 645 to 3.15 V).
float volts(int adPin) // Measures volts at adPin
{ // Returns floating point voltage
return float(analogRead(adPin)) * 5.0 / 1024.0;
}
Since return is to the left of the calculation in the volts function block, the result gets
returned to the function call. The sketch PhototransistorVoltage displays the value returned
by the
volts function with Serial.print(volts(A3)).
HaltUnderBrightLight uses that value in the
if(volts(A3) > 3.5) expression to bring the
BOE Shield-Bot to a halt under the light.
Binary vs. Analog and Digital
A binary sensor can transmit two different states, typically to indicate the presence or absence of
something. For example, a whisker sends a high signal if it is not pressed, or a low signal if it is
pressed.
An analog sensor sends a continuous range of values that correspond to a continuous range of
measurements. The phototransistor circuits in this activity are examples of analog sensors. They
provide continuous ranges of values that correspond to continuous ranges of light levels.
A digital value is a number expressed by digits. Computers and microcontrollers store analog
measurements as digital values. The process of measuring an analog sensor and storing that
measurement as a digital value is called analog to digital conversion. The measurement is called a
digitized measurement. Analog to digital conversion documents will also call them quantized
measurements.