Advertisement
microrobotics

TMP36 Precision Temperature Sensor

Mar 31st, 2023
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The TMP36 is an analog temperature sensor that outputs a voltage proportional to the temperature it measures. Here's an example of how to use the TMP36 with an Arduino to read the temperature and display it on the Serial Monitor:
  3.  
  4. To wire the TMP36 sensor, connect its left pin (when facing the flat side) to the 5V pin on the Arduino, the middle pin to the TMP36_PIN (analog pin A0), and the right pin to the ground.
  5.  
  6. This code will continuously read the temperature from the TMP36 sensor and print it on the Serial Monitor in degrees Celsius.
  7. */
  8.  
  9. const int TMP36_PIN = A0; // TMP36 connected to Arduino analog pin A0
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13. }
  14.  
  15. void loop() {
  16.   float temperature = readTMP36Temperature();
  17.  
  18.   Serial.print("Temperature: ");
  19.   Serial.print(temperature);
  20.   Serial.println(" °C");
  21.  
  22.   delay(1000); // Wait 1 second between readings
  23. }
  24.  
  25. float readTMP36Temperature() {
  26.   int sensorValue = analogRead(TMP36_PIN);
  27.   float voltage = sensorValue * (5.0 / 1023.0); // Convert sensor value to voltage
  28.   float temperature = (voltage - 0.5) * 100.0; // Convert voltage to temperature in Celsius
  29.   return temperature;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement