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.
This code reads the analog value from the LM35 sensor and converts it to Celsius. It prints the result to the Serial Monitor.
This code snippet uses the DHT library to read the temperature and humidity values from the DHT11/DHT22 sensor.
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.Wiring the LM35
The LM35 has three pins: VCC, GND, and OUT. To wire it:- Connect the VCC pin to the 5V pin on Arduino.
- Connect the GND pin to the GND pin on Arduino.
- 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:- Connect the VCC pin to the 5V pin on Arduino.
- Connect the GND pin to the GND pin on Arduino.
- Connect the DATA pin to a digital I/O pin on Arduino (e.g., D2).
- 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:- Connect the VCC pin to the 5V pin on Arduino.
- Connect the GND pin to the GND pin on Arduino.
- Connect the DATA pin to a digital I/O pin (e.g., D3).
- 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 }
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 }
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;
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
- 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).
- Inaccurate temperature readings:
- Ensure the sensor is in a stable environment and not exposed to extreme conditions.
- Calibrate your sensor if needed.
- 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.