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

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×DHT22 Temperature and Humidity Sensor
1×Relay
1×12V Power Adapter
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Optionally, 12V Fan
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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

The wiring diagram between Arduino Nano and 33 IoT dht22 sensor relay

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

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-dht22-relay */ #include <DHT.h> #define DHT22_PIN 2 // The Arduino Nano 33 IoT pin connected to DHT22 #define RELAY_PIN 13 // The Arduino Nano 33 IoT pin connected to relay #define TEMP_UPPER_THRESHOLD 30 // upper temperature threshold #define TEMP_LOWER_THRESHOLD 15 // lower temperature threshold DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); // initialize serial dht22.begin(); // initialize the DHT sensor } void loop() { float temperature = dht22.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT22 sensor!"); } else { if (temperature > TEMP_UPPER_THRESHOLD) { Serial.println("Turn the relay on"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (temperature < TEMP_LOWER_THRESHOLD) { Serial.println("Turn the relay off"); digitalWrite(RELAY_PIN, LOW); // turn off } } // wait a 1 seconds between readings delay(1000); }

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.
Arduino Nano 33 IoT DHT sensor library
  • You may be asked to install some extra libraries.
  • Click the Install All button to install all required libraries.
Arduino Nano 33 IoT Adafruit Unified sensor library
  • 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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!