0Pricing
Arduino & IoT Academy · Lesson

Install the DHT Library

Add the library and include it in your sketch.

Install the DHT Library is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 DHT11

Create 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 DHT22

Restart 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. 🎉

Frequently asked questions

Is the “Install the DHT Library” lesson free?

Yes — the full text of “Install the DHT Library” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Install the DHT Library”?

Add the library and include it in your sketch. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Install the DHT Library” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Wire the DHT11/DHT22
  2. Install the DHT Library
  3. Read Temperature & Humidity
  4. Handle Failed Reads
← Back to Arduino & IoT Academy