ติดตั้งไลบรารี DHT
เพิ่มไลบรารีและเรียกใช้ในสเก็ตช์
ติดตั้งไลบรารี DHT เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Use a Library
Talking to a DHT by hand means tricky timing. A library hides that work so you can read climate data with one simple call. 📚
Open Library Manager
In the Arduino IDE, open the Library Manager from the Tools menu or the side panel. It is your store for ready-made code.
Search for DHT
Type DHT sensor library into the search box. The well-known one is published by Adafruit and works with both the DHT11 and DHT22.
It Has a Helper
The DHT library leans on the Adafruit Unified Sensor library. The IDE usually offers to install this dependency for you.
Click Install
Press Install and accept the prompt to add the dependency too. In a few seconds both libraries are ready to use.
Include It in Code
At the top of your sketch, pull the code in with #include. This line gives your program access to the DHT functions.
#include <DHT.h>Tell It Your Pin
Define which pin the sensor uses and which type you own. Clear names up top make the rest of the sketch easy to read.
#define DHTPIN 2
#define DHTTYPE DHT11Create the Object
Make a DHT object by passing your pin and type. This object is how you will ask for readings throughout the program.
DHT dht(DHTPIN, DHTTYPE);Start It in setup
Inside setup, call dht.begin() once to wake the sensor. After this line the library is ready to take measurements.
void setup() {
Serial.begin(9600);
dht.begin();
}Match Type to Hardware
Set DHTTYPE to DHT22 if that is your part. A mismatch here leads to wrong or failed readings, so pick the right one.
#define DHTTYPE DHT22Restart If Hidden
If the IDE does not see your new library right away, restart it. A fresh start makes the included header resolve cleanly.
Quick Check
You installed the DHT library. Which call belongs in setup() before you can read values?
Recap
You installed the DHT library and its helper, included the header, set your pin and type, made the object, and called begin. 🎉
คำถามที่พบบ่อย
บทเรียน “ติดตั้งไลบรารี DHT” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ติดตั้งไลบรารี DHT” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ติดตั้งไลบรารี DHT”
เพิ่มไลบรารีและเรียกใช้ในสเก็ตช์ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ติดตั้งไลบรารี DHT” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ต่อสาย DHT11/DHT22
- ติดตั้งไลบรารี DHT
- อ่านอุณหภูมิและความชื้น
- จัดการการอ่านค่าที่ล้มเหลว