To use it, we only have to determine the current temperature t in Celsius. We
will use the TMP36 voltage output temperature sensor from Analog Devices.
3
It’s cheap and easy to use.
To connect the TMP36 to the Arduino, connect the Arduino’s ground and
power to the corresponding pins of the TMP36. Then connect the sensor’s
signal pin to the pin A0—that is, the analog pin number 0:
As you might’ve guessed from its vendor’s name, the TMP36 is an analog
device: it changes the voltage on its signal pin corresponding to the current
temperature. The higher the temperature, the higher the voltage. For us, it’s
an excellent opportunity to learn how to use the Arduino’s analog IO pins.
So, let’s see some code that uses the sensor:
InputDevices/Temperature/SensorTest/SensorTest.ino
const unsigned int TEMP_SENSOR_PIN = A0;
Line 1
const float SUPPLY_VOLTAGE = 5.0;
-
const unsigned int BAUD_RATE = 9600;
-
-
void setup() {
5
Serial.begin(BAUD_RATE);
-
}
-
-
void loop() {
-
const float tempC = get_temperature();
10
const float tempF = (tempC * 9.0 / 5.0) + 32.0;
-
Serial.print(tempC);
-
Serial.print(" C, ");
-
Serial.print(tempF);
-
Serial.println(" F");
15
delay(1000);
-
}
-
-
const float get_temperature() {
-
const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);
20
const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;
-
return (voltage * 1000 - 500) / 10;
-
}
-
3.
http://tinyurl.com/msard-analog
report erratum • discuss
Increasing Precision Using a Temperature Sensor • 87
www.it-ebooks.info