How to read a temperature sensor with Arduino

Photo of author

By Jackson Taylor

Arduino is a versatile platform for various electronics projects, and one of the most common tasks is reading data from a temperature sensor. Whether you’re building a simple weather station or integrating temperature monitoring into a larger project, knowing how to read temperature sensors with Arduino is crucial. In this guide, we’ll walk you through the process of connecting and reading temperature sensors using Arduino, ensuring that you can easily get started with your own projects.

Introduction to Temperature Sensors and Arduino

Temperature sensors are devices used to measure temperature and provide data in a readable format. Arduino, on the other hand, is an open-source electronics platform based on simple software and hardware. By using temperature sensors with Arduino, you can monitor environmental conditions, automate systems, or even create interactive displays.

Types of Temperature Sensors Compatible with Arduino

Before diving into the wiring and code, it’s essential to understand the different types of temperature sensors compatible with Arduino. Some popular options include:

1. LM35 Temperature Sensor

The LM35 is a widely-used analog temperature sensor. It provides an output voltage directly proportional to the temperature in Celsius, which makes it easy to interface with Arduino.

2. DHT11 / DHT22

These digital sensors can measure both temperature and humidity. DHT11 is cheaper but less accurate compared to the DHT22, which offers a broader range of measurements.

3. TMP36

The TMP36 is an analog sensor with a high degree of precision, ideal for applications requiring accurate temperature readings.

4. DS18B20

A digital temperature sensor, DS18B20 communicates using the 1-Wire protocol, making it an excellent choice for projects that require multiple sensors on a single data line. Each of these sensors requires a different method of wiring and reading data, but they all work well with Arduino.

How to Wire a Temperature Sensor to an Arduino

Connecting a temperature sensor to an Arduino can vary depending on the type of sensor you’re using. Below is a general guideline for the most common sensors.
See also
How to connect Arduino to the cloud

Wiring the LM35

The LM35 has three pins: VCC, GND, and OUT. To wire it:
  1. Connect the VCC pin to the 5V pin on Arduino.
  2. Connect the GND pin to the GND pin on Arduino.
  3. Connect the OUT pin to one of Arduino’s analog input pins (e.g., A0).

Wiring the DHT11/DHT22

For the DHT series sensors, the wiring is as follows:
  1. Connect the VCC pin to the 5V pin on Arduino.
  2. Connect the GND pin to the GND pin on Arduino.
  3. Connect the DATA pin to a digital I/O pin on Arduino (e.g., D2).
  4. Optionally, connect a 10kΩ pull-up resistor between the DATA pin and VCC for stable readings.

Wiring the DS18B20

The DS18B20 operates using the 1-Wire protocol, which simplifies connections when using multiple sensors:
  1. Connect the VCC pin to the 5V pin on Arduino.
  2. Connect the GND pin to the GND pin on Arduino.
  3. Connect the DATA pin to a digital I/O pin (e.g., D3).
  4. Place a 4.7kΩ pull-up resistor between the DATA and VCC pins.

Writing Arduino Code to Read the Temperature

Once the sensor is wired to the Arduino, it’s time to write the code that will allow you to read the temperature. Below is an example of how to read temperature data from the LM35 sensor.
cpp
int sensorPin = A0; // LM35 sensor connected to A0 float temperature; // Variable to store the temperature void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate } void loop() { int sensorValue = analogRead(sensorPin); // Read the value from the sensor temperature = (sensorValue * 5.0 * 100.0) / 1024.0; // Convert the value to Celsius Serial.print(“Temperature: “); Serial.print(temperature); // Print the temperature Serial.println(” °C”); delay(1000); // Wait for a second before the next reading }
This code reads the analog value from the LM35 sensor and converts it to Celsius. It prints the result to the Serial Monitor.
See also
How to read a light sensor with Arduino

Reading Temperature from the DHT11/DHT22

To read temperature data from the DHT11 or DHT22, you’ll need to use the DHT library. Here’s an example:
cpp
#include <DHT.h> #define DHTPIN 2 // Pin where the DHT sensor is connected #define DHTTYPE DHT11 // Choose DHT11 or DHT22 depending on the sensor used DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); // Initialize the sensor } void loop() { float humidity = dht.readHumidity(); // Read humidity float temperature = dht.readTemperature(); // Read temperature in Celsius if (isnan(temperature) || isnan(humidity)) { Serial.println(“Failed to read from DHT sensor!”); return; } Serial.print(“Temperature: “); Serial.print(temperature); // Print the temperature Serial.println(” °C”); delay(2000); // Wait for 2 seconds before the next reading }
This code snippet uses the DHT library to read the temperature and humidity values from the DHT11/DHT22 sensor.

Converting Temperature Data to Different Units

Often, you’ll need to convert the temperature data from Celsius to Fahrenheit or Kelvin. Here’s how to do it:
  • Celsius to Fahrenheit:
    cpp
    float fahrenheit = (temperature * 9.0 / 5.0) + 32;
  • Celsius to Kelvin:
    cpp
    float kelvin = temperature + 273.15;
You can modify your code to print the temperature in your preferred unit by adding these conversions.

Testing Your Arduino Project

Once your code is uploaded to the Arduino, open the Serial Monitor from the Arduino IDE. You should see the temperature readings printed every second (or as set in your code). If the temperature doesn’t show up or if it’s incorrect, check your wiring and ensure the sensor is connected properly.

Troubleshooting Common Issues

  1. No readings in the Serial Monitor:
    • Ensure the sensor is wired correctly.
    • Check that you’re using the correct pin numbers in your code.
    • Verify that the sensor is compatible with the voltage level provided by Arduino (e.g., 5V).
  2. Inaccurate temperature readings:
    • Ensure the sensor is in a stable environment and not exposed to extreme conditions.
    • Calibrate your sensor if needed.
  3. Code errors or compilation issues:
    • Double-check for typos in your code.
    • Ensure that the required libraries (e.g., DHT) are installed and included in your project.
See also
How to read a humidity sensor with Arduino

Advanced Tips and Techniques

Multiple Sensors on One Bus

If you’re using the DS18B20 sensor, one of the great features is the ability to connect multiple sensors to the same data pin using the 1-Wire protocol. This allows you to measure temperature at different points in a large system with minimal wiring.

Adding an LCD or OLED Display

For projects where you need to visualize the temperature, you can connect an LCD or OLED display to your Arduino. With the right libraries, you can display the current temperature reading directly on the screen, making your project more interactive.

Conclusion

Reading a temperature sensor with Arduino is a straightforward process that opens up a wide range of possibilities for your electronics projects. Whether you’re working with analog sensors like the LM35 or digital sensors like the DHT11, the process of wiring the sensor, writing the code, and troubleshooting common issues is essential knowledge for any Arduino enthusiast. With a little practice, you’ll be able to build more complex systems involving temperature sensing and data monitoring. When following this guide, you’re well on your way to creating your own temperature-monitoring project with Arduino. Happy coding!