Arduino Nano 33 IoT - DHT22 - Relay
In this lesson, you'll learn how to use the Arduino Nano 33 IoT to operate a relay using the temperature measured by the DHT22 sensor.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Relay and DHT22 Sensor
If you're new to using the Relay, DHT22 Sensor, and Arduino Nano 33 IoT, please check out these tutorials:
These tutorials explain how Relay and DHT22 Sensor work, their pinouts, how to connect them to the Arduino Nano 33 IoT, and how to program Arduino Nano 33 IoT to work with the Relay and DHT22 Sensor.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
How System Works
- The Arduino Nano 33 IoT checks the temperature using the DHT22 sensor.
- If the temperature goes above a set high limit, the Arduino Nano 33 IoT switches the relay on.
- If the temperature goes below a set low limit, the Arduino Nano 33 IoT switches the relay off.
This process repeats forever in a loop.
If you want the relay to turn on when the temperature goes above a certain value and turn off when it goes below that same value, just set both the upper and lower limits to that value.
Arduino Nano 33 IoT Code
In the code above, the Arduino Nano 33 IoT turns on the relay when the temperature goes over 25°C, and it stays on until the temperature drops below 20°C.
Detailed Instructions
If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:
- Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
- Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
- Click the Libraries icon on the left side of the Arduino program.
- Search for DHT and then choose the DHT sensor library made by Adafruit.
- Click the Install button to add the library.

- You may be asked to install some extra libraries.
- Click the Install All button to install all required libraries.

- Copy the code that matches your sensor and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT.
- Change the temperature around the sensor by making it either hotter or colder.
- Check the relay to see its current state.