Measure temperature with the TMP36

FunduinoLast updated on

Analogwerte mit map() in Grad Celsius umrechnen

Aufgabe: Mit dem Temperatursensor TMP36 liest du die Temperatur aus und zeigst sie im Seriellen Monitor an. Anschließend erweiterst du den Sketch um ein Warnsignal, das bei 30 °C ertönt.

Schematic with TMP36 temperature sensor on Arduino UNO analog pin A0
Circuit diagram: TMP36 with the Arduino UNO

Connect the TMP36

The sensor has three connections. When looking at the flat side of the sensor:

  • 5V on the left
  • the temperature signal pin in the middle
  • GND on the right

The sensor outputs a voltage between 0 V and 2.0 V on the signal pin: 0 V corresponds to -50 °C, and 2.0 V corresponds to 150 °C. According to the manufacturer, the sensor is reasonably accurate (±2 °C) between -40 °C and +125 °C. The microcontroller board reads the voltage on this pin and converts it into a temperature value.

When setting it up, you should use an external power supply if possible (a 9V power adapter or 9V battery), as this significantly improves the sensor's accuracy.

CAUTION: If you connect the sensor incorrectly, it will burn out. Check the pinout before switching it on.

Schematic with TMP36 temperature sensor on Arduino UNO analog pin A0
Circuit diagram: TMP36 with the Arduino UNO

The map command - from measured value to temperature

For this sketch, you need the map command. It appears in the line temperatur = map(sensorwert, 0, 410, -50, 150);

The general syntax map(a, b, c, d, e) makes the function easier to understand: A value a—such as a measured value—is expected to be within a specific range between the two values b and c. The map command then converts the value a into another value corresponding to the range between d and e.

  • a = number to be converted
  • b = minimum of the measurement range
  • c = maximum of the measurement range
  • d = minimum of the output value
  • e = maximum of the output value

In our sketch, the following happens: The TMP36 outputs the measured value as a voltage between 0 V and 2 V on the middle pin. This voltage range corresponds to the measurable temperature range from -50 °C to +150 °C. The Arduino board's analog input pin detects this voltage range as a numerical value between 0 and 410 using the command analogRead(TMP36) and stores it in the variable sensorwert. The map command converts this number back into a temperature value between -50 °C and +150 °C.

You then send the final temperature value to the Serial Monitor with Serial.print(temperatur); and read it on your PC.

Read and display temperature

After opening the Serial Monitor, the result should look like the image.

Output in the Serial Monitor with temperature readings and the unit degrees Celsius
The recorded temperature values in the Serial Monitor
Program code - read and display temperature
int TMP36 = A0; // The sensor is connected to analog pin A0. From now on, we’ll call this pin "TMP36".
int sensorwert;
int temperatur = 0; // The temperature value will later be stored in the variable "temperatur".
int t = 500; // The value of "t" determines the time intervals between individual measurements.
void setup()
{
  Serial.begin(9600); // Serial communication starts in setup so that the temperature can be sent to the Serial Monitor. The board sends the measured values to the computer via serial communication. In the Arduino software, start the "Serial Monitor" under "Tools" to see the measured values.
}
void loop()
{
  sensorwert = analogRead(TMP36); // Read the sensor value.
  temperatur = map(sensorwert, 0, 410, -50, 150); // Convert the sensor value using the "map" command.
  delay(t); // A short pause lasting "t" milliseconds follows each measurement.
  Serial.print(temperatur); // The value "temperatur" is now sent to the PC via serial communication.
  Serial.println(" Degrees Celsius"); // The unit is displayed after the temperature in the Serial Monitor.
}

Extension: warning signal for exceeding the temperature limit

As soon as the temperature reaches 30 °C, a warning signal should sound. To do this, connect a speaker Connect the speaker’s positive terminal to pin 5 on the Arduino board. Connect the speaker’s other pin to GND.

This sketch keeps the speaker switched on with digitalWrite. This only produces a tone with an active buzzer - a passive speaker remains silent. With a passive speaker, use the tone() command instead.

Program code - Warning signal when temperature is exceeded
int TMP36 = A0;
int sensorwert;
int temperatur = 0;
int t = 500;
int piezo = 5; // Das Wort "piezo" steht jetzt für die Zahl 5, an Pin 5 wird also der Lautsprecher angeschlossen.
void setup()
{
  Serial.begin(9600);
  pinMode(piezo, OUTPUT); // Der Pin für den Lautsprecher wird als Ausgang definiert, da zum Piepsen eine Spannung nötig ist.
}
void loop()
{
  sensorwert = analogRead(TMP36);
  temperatur = map(sensorwert, 0, 410, -50, 150);
  delay(t);
  Serial.print(temperatur);
  Serial.println(" Grad Celsius");
  if (temperatur >= 30) // Eine IF-Bedingung: Wenn der Wert für die Temperatur größer oder gleich 30 ist, dann ...
  {
    digitalWrite(piezo, HIGH); // ... fange an zu piepsen.
  }
  else // Und wenn das nicht so ist ...
  {
    digitalWrite(piezo, LOW); // ... dann sei leise.
  }
}
Rate the instructions

Comments

1 post
Mohammad Qisanieh

War interessant!!!!!

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.