How to Test Arduino TMP36 Temperature Sensor
In this tutorial you will learn how to connect and use a TMP36 temperature sensor to measure the temperature in Celsius and Fahrenheit using an Arduino.
Parts List
if you want to complete Arduino TMP36 Temperature Sensor test tutorial, you have to need the following component
- Arduino
- TMP36 temperature sensor
- Breadboard
- Connecting Wire
Schematic Diagram
Arduino — — — — — — -TMP36
TMP36 — — — — — — — -5V
GND — — — — — — — -GND
Analog pin 0 — — — — — — — -Vout
Program
int TMP36Pin = A0;
int RawValue = 0;
float VoltageValue = 0;
const float UnitsToVolts = 5.0/1024.0;
void setup()
{
Serial.begin(9600);
Serial.println(“Arduino TMP36 Test
…”);
}
float GetTempCelsius(float Voltage)
{
float temp = 0;
temp = (Voltage-0.5)*100;
return temp;
}
float GetTempFarenheit(float Voltage)
{
float temp = 0;
float CTemp = GetTempCelsius(Voltage);
temp = CTemp*(9.0/5.0)+32.0;
return temp;
}
void loop()
{
RawValue = analogRead(TMP36Pin);
VoltageValue = RawValue * UnitsToVolts;
Serial.print(“Celsius Temp: “);
Serial.print(GetTempCelsius
(VoltageValue));
Serial.print(“ , Farenheit Temp: “);
Serial.println(GetTempFarenheit
(VoltageValue));
}
Upload your Arduino program and launch the Serial Monitor. The temperature should be displayed in Celsius and Fahrenheit. By adding a delay statement, such as delay(1000), at the end of loop(), you can try to slow down your temperature readings. This delays the printing by 1000 milliseconds or 1 second of the next temperature reading. The delay value can be adjusted to your own personal taste.
Program Analysis
int TMP36Pin = A0;
Assigning the analog pin 0 of the Arduino to the Vout pin on the TMP36.
int RawValue = 0;
Initializing the variable that reads the raw value from the Vout pin to 0.
float VoltageValue = 0;
Initializing the variable that holds the voltage value read from the Vout pin to 0.
// Conversion: 5 volts/1024 units
const float UnitsToVolts = 5.0/1024.0;
Defining the UnitsToVolts variable so that it converts units read by the Arduino from the Vout pin to voltage. The conversion rate is 5 V = 1,024 units.
void setup()
Defining the setup() function
Serial.begin(9600);
Initializing the Serial Monitor with a speed of 9,600 baud
Serial.println(“Arduino TMP36 Test
…”);
Printing out a message to the Serial Monitor indicating that the program has started.
float GetTempCelsius(float Voltage)
float temp = 0;
temp = (Voltage-0.5)*100;
return temp;
Specify the GetTempCelsius function and return the Celsius temperature based on the input voltage parameters. The equation for the calculation of the temperature is linear and was derived in the previous section.
float GetTempFarenheit(float Voltage)
float temp = 0;
float CTemp = GetTempCelsius(Voltage);
temp = CTemp*(9.0/5.0)+32.0;
return temp;
Set the GetTempFarenheit(float voltage) function and restore the Fahrenheit temperature based on the input voltage parameter. The GetTempCelsius() function is first called in Celsius to change temperature from Celsius to Fahrenheit.
void loop()
Defining the loop() function
RawValue = analogRead(TMP36Pin);
Reads the raw value from the Vout pin of the TMP36
VoltageValue = RawValue * UnitsToVolts;
Converts the raw value from step 1 into a voltage value.
Serial.print(“Celsius Temp: “);
Serial.print(GetTempCelsius
(VoltageValue));
Prints the temperature in Celsius to the Serial Monitor using the GetTempCelsius(VoltageValue) function, with the input parameter being the voltage value from step 2.
Serial.print(“ , Farenheit Temp: “);
Serial.println(GetTempFarenheit
(VoltageValue));
Using the GetTempFarenheit(voltage value) function, the input parameter is the voltage value from step 2 and is printed to the serial monitor.