Installare la libreria DHT
Aggiunga la libreria e la includa nello sketch.
Installare la libreria DHT è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎉
Domande Frequenti
La lezione «Installare la libreria DHT» è gratuita?
Sì — il testo completo di «Installare la libreria DHT» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Cosa imparerò in «Installare la libreria DHT»?
Aggiunga la libreria e la includa nello sketch. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Arduino & IoT Academy?
Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Installare la libreria DHT»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?
Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Collegare DHT11/DHT22
- Installare la libreria DHT
- Leggere temperatura e umidità
- Gestire le letture non riuscite