← Back to Course
Temperature Sensors - DHT22, DS18B20, NTC
🌡️ Temperature Sensors Complete Guide
Monitor air temperature in greenhouses, crop storage, and soil temperature for planting decisions.
📊 Sensor Comparison:
| Sensor | Measures | Accuracy | Cost | Best For |
|---|---|---|---|---|
| DHT22 | Temp + Humidity | ±0.5°C | $5 | Air temp/humidity |
| DS18B20 | Temperature only | ±0.5°C | $4 | Soil temperature (waterproof) |
| NTC Thermistor | Temperature only | ±1-2°C | $1 | Budget projects |
🌡️ DHT22: Air Temperature and Humidity
#include "DHT.h"
#define DHTPIN 16
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (!isnan(temp) && !isnan(hum)) {
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println("%");
if (temp > 35) {
Serial.println("⚠️ HIGH TEMPERATURE - Open vents!");
}
if (hum > 80) {
Serial.println("⚠️ HIGH HUMIDITY - Risk of fungus!");
}
}
delay(2000);
}
🌍 DS18B20: Waterproof Soil Temperature
#include#include #define ONE_WIRE_BUS 17 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { Serial.begin(115200); sensors.begin(); } void loop() { sensors.requestTemperatures(); float soilTemp = sensors.getTempCByIndex(0); Serial.print("Soil Temperature: "); Serial.print(soilTemp); Serial.println("°C"); if (soilTemp < 10) { Serial.println("⚠️ SOIL TOO COLD - Delay planting!"); } delay(60000); }
💰 NTC Thermistor: Budget Option ($1)
#define THERMISTOR_PIN 34
const float BETA = 3950;
const float SERIES_RES = 10000;
void loop() {
int raw = analogRead(THERMISTOR_PIN);
float voltage = raw * (3.3 / 4095.0);
float resistance = (SERIES_RES * voltage) / (3.3 - voltage);
float tempK = 1 / ((1 / 298.15) + (log(resistance / SERIES_RES) / BETA));
float tempC = tempK - 273.15;
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("°C");
delay(2000);
}
💡 Key Takeaways:
- Apply these concepts directly to your farm or project.
- Take notes on important details for the quiz.
- Use the button below to track your progress.
×