We can turn the analog pin’s output into a voltage value by dividing it by
1024 and by multiplying it by the supply voltage, which we do in line 21.
We now have to convert the voltage the sensor delivers into degrees Celsius.
In the sensor’s data sheet, we find the following formula:
T = ((sensor output in mV) - 500) / 10
We have to subtract 500 millivolts because the sensor always outputs a pos-
itive voltage. This way, we can represent negative temperatures, too. The
sensor’s resolution is 10 millivolts, so we have to divide by 10. A voltage value
of 750 millivolts corresponds to a temperature of (750 - 500) / 10 = 25°C. See
it implemented in line 22.
Compile the program, upload it to the Arduino, and you’ll see something like
the following in your serial monitor:
20.80 C, 69.44 F
20.80 C, 69.44 F
20.31 C, 68.56 F
20.80 C, 69.44 F
20.80 C, 69.44 F
As you can see, the sensor needs some time to calibrate, but its results get
stable fairly quickly. By the way, you’ll always need to insert a short delay
between two calls to
analogRead
, because the Arduino’s internal analog system
needs some time (0.0001 seconds on the Uno) between two readings. We use
a delay of a whole second to make the output easier to read and because we
don’t expect the temperature to change rapidly. Otherwise, a delay of a single
millisecond would be enough.
Now we have two separate circuits: one for measuring distances and one for
measuring temperatures. See them combined to a single circuit in Figure 17,
The TMP36 and the PING))) sensors working together, on page 90 and Figure
18, Photo of final circuit, on page 90.
Use the following program to bring the circuit to life:
InputDevices/Ultrasonic/PreciseSensor/PreciseSensor.ino
const unsigned int TEMP_SENSOR_PIN = A0;
Line 1
const float SUPPLY_VOLTAGE = 5.0;
-
const unsigned int PING_SENSOR_IO_PIN = 7;
-
const float SENSOR_GAP = 0.2;
-
const unsigned int BAUD_RATE = 9600;
5
float current_temperature = 0.0;
-
unsigned long last_measurement = millis();
-
-
-
report erratum • discuss
Increasing Precision Using a Temperature Sensor • 89
www.it-ebooks.info