# 5. 溫溼度感測器 ## 準備材料 1. Arduino Uno板 2. DHT11或DHT22溫濕度感測器 3. 多條杜邦線 ## 教學步驟 首先將Arduino板連接到電腦上,打開Arduino IDE。 在IDE中選擇「Tools」>「Manage Library」>輸入「DHT sensor library」點擊Install。 ![](https://i.imgur.com/908h88X.png) 在IDE中選擇「檔案」>「範例」>「DHT sensor library」>「DHTtester」。 ![](https://i.imgur.com/Dv384rf.png) Tinkercad版本 ``` /* This code records the temperature through testing the mV put out by the sensor. It records in both Celcius and Fahrenheit. It can only detect from -40 degrees C to 125 degrees C or -40 degrees F to 257 degrees F The Humidity is simulated by a potentiometer by being mapped into percentages */ const int analogIn = A0; int humiditysensorOutput = 0; // Defining Variables int RawValue= 0; double Voltage = 0; double tempC = 0; double tempF = 0; void setup(){ Serial.begin(9600); pinMode(A1, INPUT); } void loop(){ RawValue = analogRead(analogIn); Voltage = (RawValue / 1023.0) * 5000; // 5000 to get millivots. tempC = (Voltage-500) * 0.1; // 500 is the offset tempF = (tempC * 1.8) + 32; // convert to F Serial.print("Raw Value = " ); Serial.print(RawValue); Serial.print("\t milli volts = "); Serial.print(Voltage,0); // Serial.print("\t Temperature in C = "); Serial.print(tempC,1); Serial.print("\t Temperature in F = "); Serial.println(tempF,1); humiditysensorOutput = analogRead(A1); Serial.print("Humidity: "); // Printing out Humidity Percentage Serial.print(map(humiditysensorOutput, 0, 1023, 10, 70)); Serial.println("%"); delay(1000); //iterate every 5 seconds } ``` 在Arduino IDE中輸入以下code: ``` // Example testing sketch for various DHT humidity/temperature sensors // Written by ladyada, public domain // REQUIRES the following Arduino libraries: // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor #include "DHT.h" #define DHTPIN 5 // Digital pin connected to the DHT sensor // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- // Pin 15 can work but DHT must be disconnected during program upload. // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 // to 3.3V instead of 5V! // Connect pin 2 of the sensor to whatever your DHTPIN is // Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins) // Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins) // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor // Initialize DHT sensor. // Note that older versions of this library took an optional third parameter to // tweak the timings for faster processors. This parameter is no longer needed // as the current DHT reading algorithm adjusts itself to work on faster procs. DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println(F("DHTxx test!")); dht.begin(); } void loop() { // Wait a few seconds between measurements. delay(2000); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(f); Serial.print(F("°F Heat index: ")); Serial.print(hic); Serial.print(F("°C ")); Serial.print(hif); Serial.println(F("°F")); } ``` 將DHT溫溼度感測器的VCC接腳連接到Arduino板上的5V接腳,將GND接腳連接到GND接腳,將DHT感測器的S接腳連接到Arduino板上的Digital 5接腳。 ![](https://i.imgur.com/Hg8ezAU.png) 接下來上傳程式碼並執行 在IDE中選擇「Tools」>「Serial Monitor」,您將能夠看到從DHT感測器讀取到的溫度和濕度數據。