Choosing the right Arduino temperature sensor can make or break your project. The LM35, DHT11, and DS18B20 are the three most popular temperature sensors used with Arduino in India, but they differ significantly in accuracy, interface, operating range, and cost. This comprehensive guide compares all three with wiring diagrams, working code, and clear recommendations for every use case.
Table of Contents
Quick Comparison Table
| Feature | LM35 | DHT11 | DS18B20 |
|---|---|---|---|
| Type | Analog IC | Digital module | Digital IC (1-Wire) |
| Temperature Range | -55°C to +150°C | 0°C to +50°C | -55°C to +125°C |
| Accuracy | ±1°C (typical) | ±2°C | ±0.5°C |
| Measures Humidity | No | Yes (±5% RH) | No |
| Interface / Protocol | Analog (ADC) | Single-wire digital | 1-Wire protocol |
| Resolution | ~0.1°C (10-bit ADC) | 1°C | 0.0625°C (12-bit) |
| Pins Needed | 1 analog pin | 1 digital pin | 1 digital pin |
| Multiple on one pin | No | No | Yes (up to 127!) |
| Waterproof Version | No | No | Yes (DS18B20 probe) |
| Response Time | Fast (<1 sec) | Slow (2 sec min) | Medium (750 ms) |
| Price in India | ₹15–₹30 | ₹50–₹120 | ₹50–₹150 |
| Library Required | None (use analogRead) | DHT library (Adafruit) | OneWire + DallasTemperature |
| Best For | Simple analog reads | Indoor weather monitoring | Accurate, multi-point, outdoor |
LM35 Temperature Sensor
The LM35 is a classic analog temperature sensor IC manufactured by Texas Instruments. It outputs a voltage that is linearly proportional to temperature — specifically, 10 millivolts per degree Celsius. At 25°C, the output is 250 mV; at 100°C, it is 1,000 mV (1V).
Key characteristics:
- No calibration needed — factory calibrated in Celsius (unlike thermistors which require calibration)
- Wide operating range: -55°C to +150°C (though readings below 0°C require a modified circuit)
- Powered directly from the Arduino’s 5V pin
- Requires only one analog input pin on the Arduino
- Very low self-heating (0.1°C in still air)
- Cheapest of the three sensors — available for as little as ₹15 per piece
Limitations: The analog output means readings are affected by electrical noise, especially in industrial environments or with long wire runs. Resolution is limited by Arduino’s 10-bit ADC (about 0.1°C at 5V reference). Cannot measure humidity.
Wiring (LM35 TO-92 package):
- Flat side facing you: Left pin → 5V, Middle pin → Arduino A0, Right pin → GND
Arduino Code for LM35:
// LM35 Temperature Sensor
// Output: 10mV per degree Celsius
// Connected to analog pin A0
const int LM35_PIN = A0;
void setup() {
Serial.begin(9600);
Serial.println("LM35 Temperature Monitor");
analogReference(DEFAULT); // 5V reference
}
void loop() {
int adcValue = analogRead(LM35_PIN);
// Convert ADC reading to voltage (mV)
// Arduino 10-bit ADC: 0-1023 maps to 0-5000mV
float millivolts = (adcValue / 1023.0) * 5000.0;
// LM35: 10mV per degree Celsius
float temperatureC = millivolts / 10.0;
// Convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print("ADC: "); Serial.print(adcValue);
Serial.print(" | Temp: "); Serial.print(temperatureC, 1);
Serial.print(" C | "); Serial.print(temperatureF, 1);
Serial.println(" F");
delay(1000);
}
// Tip: For better accuracy, use INTERNAL reference (1.1V):
// analogReference(INTERNAL); and change 5000.0 to 1100.0
DHT11 Temperature and Humidity Sensor
The DHT11 is the most popular temperature and humidity sensor in the Arduino maker community, and for good reason — it is the only sensor in this comparison that measures both temperature AND relative humidity with a single module. It communicates via a single-wire proprietary digital protocol.
Key characteristics:
- Measures both temperature (0–50°C ±2°C) and relative humidity (20–90% RH ±5%)
- Digital output — immune to noise over cable runs up to a meter or so
- Available as a bare sensor (4-pin) or as a module (3-pin with onboard resistor)
- Very popular with extensive library support and tutorials
- Low cost (₹50–₹120 at Zbotic.in)
Limitations: Slow sampling rate — cannot read more than once every 2 seconds. Resolution is only 1°C for temperature. Range of 0–50°C means it cannot measure sub-zero temperatures. For better accuracy and wider range, consider the DHT22 (±0.5°C, -40 to +80°C, but about 3x the price).
Wiring (3-pin module):
- VCC → Arduino 5V
- GND → Arduino GND
- DATA → Arduino Pin 2
- Note: If using the 4-pin bare sensor (no module), add a 10kΩ pull-up resistor between DATA and VCC
Library Installation: Open Arduino IDE → Sketch → Include Library → Manage Libraries → search “DHT sensor library” by Adafruit → Install. Also install “Adafruit Unified Sensor” when prompted.
Arduino Code for DHT11:
#include <DHT.h>
#define DHTPIN 2 // Data pin connected to Arduino pin 2
#define DHTTYPE DHT11 // Use DHT22 here if you have DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("DHT11 Temperature & Humidity Monitor");
Serial.println("Waiting for first reading (2 seconds)...");
delay(2000);
}
void loop() {
// Read temperature and humidity
float humidity = dht.readHumidity();
float tempC = dht.readTemperature(); // Celsius
float tempF = dht.readTemperature(true); // Fahrenheit
// Check for failed readings
if (isnan(humidity) || isnan(tempC)) {
Serial.println("ERROR: Failed to read from DHT11!");
Serial.println("Check wiring and ensure 2+ seconds between reads.");
delay(2000);
return;
}
// Calculate heat index (feels-like temperature)
float heatIndexC = dht.computeHeatIndex(tempC, humidity, false);
Serial.print("Humidity: "); Serial.print(humidity, 1); Serial.println(" %");
Serial.print("Temperature: "); Serial.print(tempC, 1); Serial.print(" C | ");
Serial.print(tempF, 1); Serial.println(" F");
Serial.print("Heat Index: "); Serial.print(heatIndexC,1); Serial.println(" C");
Serial.println("---");
delay(2000); // DHT11 minimum interval is 2 seconds
}
DS18B20 Digital Temperature Sensor
The DS18B20 is the most accurate and versatile temperature sensor in this comparison. Manufactured by Maxim Integrated (now Analog Devices), it uses the 1-Wire protocol — a clever single-wire bus that allows multiple DS18B20 sensors to share a single Arduino digital pin, each with a unique 64-bit address. This makes it the sensor of choice for multi-point temperature monitoring systems.
Key characteristics:
- Highest accuracy: ±0.5°C across -10°C to +85°C, ±2°C across full range
- Exceptional resolution: configurable 9–12 bit (0.5°C to 0.0625°C resolution)
- Wide temperature range: -55°C to +125°C — works in freezing environments and high-heat applications
- 1-Wire protocol: multiple sensors on one pin, each with unique address
- Available in TO-92 IC package or waterproof stainless steel probe — perfect for measuring water, soil, or pipe temperature
- Parasitic power mode: can be powered by the data line alone (2-wire connection)
Limitations: Requires two libraries (OneWire + DallasTemperature), slightly more complex setup than DHT11. Cannot measure humidity. Reading takes up to 750ms at 12-bit resolution.
Wiring (DS18B20 TO-92 or module):
- VCC → Arduino 5V (or 3.3V)
- GND → Arduino GND
- DATA → Arduino Pin 4 (with 4.7kΩ pull-up resistor between DATA and VCC)
- If using a module (like the Zbotic.in DS18B20 module), the pull-up resistor is already included — no external resistor needed
Library Installation: Open Arduino IDE → Manage Libraries → search “OneWire” by Paul Stoffregen → Install. Then search “DallasTemperature” by Miles Burton → Install.
Arduino Code for DS18B20:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // DS18B20 data pin connected to Arduino pin 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin();
// Print number of sensors found on the bus
int deviceCount = sensors.getDeviceCount();
Serial.print("DS18B20 sensors found on bus: ");
Serial.println(deviceCount);
Serial.println("Starting temperature monitoring...");
Serial.println("Resolution: 12-bit (0.0625 C steps)");
}
void loop() {
// Request temperature from all sensors
sensors.requestTemperatures();
// Read temperature from first sensor (index 0)
float tempC = sensors.getTempCByIndex(0);
float tempF = sensors.getTempFByIndex(0);
if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("ERROR: DS18B20 sensor not found!");
Serial.println("Check wiring and pull-up resistor (4.7k between DATA and VCC).");
} else {
Serial.print("Temperature: ");
Serial.print(tempC, 4); // 4 decimal places to show 0.0625 resolution
Serial.print(" C | ");
Serial.print(tempF, 2);
Serial.println(" F");
}
delay(1000);
}
// To read multiple DS18B20 sensors on one pin:
// sensors.getTempCByIndex(0), getTempCByIndex(1), etc.
// All sensors share Pin 4 with a single 4.7k pull-up resistor.
Bonus: BME280 for Advanced Projects
If your project requires measuring temperature, humidity, AND barometric pressure simultaneously, the BME280 sensor by Bosch is the premium choice. It communicates via I2C or SPI, offers excellent accuracy (±1°C, ±3% RH, ±1 hPa), and is widely used in weather stations, drones, and IoT devices.
The BME280 is available at Zbotic.in as a ready-to-use module for advanced makers who need more environmental data than the DHT11 provides.
Which Sensor Should You Choose?
Here is a clear recommendation guide based on your project requirements:
| Your Requirement | Best Sensor | Reason |
|---|---|---|
| Learning Arduino basics | LM35 | Teaches analog concepts, no library |
| Indoor weather station | DHT11 or DHT22 | Measures both temperature and humidity |
| Soil or water temperature | DS18B20 (waterproof probe) | IP68 waterproof version available |
| High accuracy measurement | DS18B20 | ±0.5°C accuracy, 12-bit resolution |
| Multiple sensor points | DS18B20 | Multiple sensors on one Arduino pin |
| Cold storage / freezer monitoring | DS18B20 | Works down to -55°C; DHT11 stops at 0°C |
| IoT environmental node | BME280 | Temp + Humidity + Pressure in one I2C module |
| Lowest cost project | LM35 | Available for under ₹30 |
| Fastest response time | LM35 | Analog output updates continuously |
Summary recommendation:
- If you are just learning Arduino and want to understand analog sensors — start with the LM35. It is the simplest and cheapest.
- If you are building an indoor weather station, home automation display, or greenhouse monitor — use the DHT11 (or DHT22 for better accuracy) because humidity data adds real value.
- If you need reliable, accurate temperature readings for a product prototype, outdoor sensor node, liquid temperature monitor, or multi-point measurement system — use the DS18B20. It is worth the slightly higher cost.
- For advanced projects that need all environmental parameters — barometric altitude, humidity, and temperature together — the BME280 is the professional choice.
Frequently Asked Questions
Q: Why does my LM35 give incorrect temperature readings?
The most common cause of inaccurate LM35 readings is electrical noise on the analog input. To improve accuracy: (1) use a short wire between LM35 and Arduino, (2) add a 100nF decoupling capacitor between the LM35’s VCC pin and GND as close to the chip as possible, (3) average multiple readings (analogRead() called 10 times, sum divided by 10), and (4) switch to the Arduino’s internal 1.1V reference for better ADC resolution at room temperature.
Q: Can I use the DHT11 outdoors?
The DHT11 is not rated for outdoor use. Its temperature range of 0–50°C means it will not work in winter conditions that go below zero, and its plastic housing is not sealed against moisture or rain. For outdoor use, the DS18B20 waterproof probe is a much better choice. If you need humidity outdoors, the SHT31 or BME280 in a waterproof enclosure is the professional approach.
Q: How many DS18B20 sensors can I connect to one Arduino pin?
Theoretically, you can connect up to 127 DS18B20 sensors on a single Arduino pin using the 1-Wire bus (each has a unique 64-bit address). In practice, beyond 10–15 sensors on a long bus you may encounter timing and signal integrity issues. For very long cable runs or many sensors, use a separate 4.7kΩ pull-up per sensor branch and keep wire lengths under 100m. The DallasTemperature library handles all addressing and multi-sensor reading automatically.
Q: What is the difference between DHT11 and DHT22?
The DHT22 (also called AM2302) is the upgraded version of the DHT11. The DHT22 offers better temperature accuracy (±0.5°C vs ±2°C), wider temperature range (-40°C to +80°C vs 0°C to 50°C), better humidity range (0–100% RH vs 20–90% RH), and higher resolution (0.1°C vs 1°C). The DHT22 costs approximately 2–3x more than the DHT11. For serious projects, the DHT22 is worth the price difference. The code for both is identical in the Adafruit DHT library — just change DHT11 to DHT22.
Q: Can I use these sensors with ESP8266 or ESP32 instead of Arduino?
Yes, all three sensors work perfectly with ESP8266 and ESP32 as well. The same libraries (DHT, OneWire, DallasTemperature) are fully compatible. The ESP8266 and ESP32 operate at 3.3V logic — the LM35 should be powered from the 3.3V pin (adjust the millivolt calculation accordingly), while the DHT11 and DS18B20 work fine at both 3.3V and 5V. ESP32 is the preferred choice for IoT temperature logging projects since it has built-in Wi-Fi and Bluetooth.
Ready to Start Your Arduino Journey?
Shop all Arduino boards, sensors, and starter kits at Zbotic.in — India’s trusted electronics component store with fast shipping across India.
Add comment