Arduino Nano 33 IoT - DHT11 - Relay

This guide shows you how to use the Arduino Nano 33 IoT board to control a relay using the temperature from a DHT11 sensor.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×DHT11 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 DHT11 Sensor

If you're new to using the Relay, DHT11 Sensor, and Arduino Nano 33 IoT, please check out these tutorials:

These tutorials explain how Relay and DHT11 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 DHT11 Sensor.

Wiring Diagram

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

This image is created using Fritzing. Click to enlarge image

How System Works

  • The Arduino Nano 33 IoT gets the temperature using the DHT11 sensor.
  • When the temperature goes above a set level, the Arduino turns on the relay.
  • When the temperature goes below a set level, the Arduino turns off the relay.

This process goes on forever in a loop.

If you want to switch the relay on when the temperature goes above a set value, and off when it goes below that same value, simply set both the maximum and minimum limits to that number.

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-dht11-relay */ #include <DHT.h> #define DHT11_PIN 2 // The Arduino Nano 33 IoT pin connected to DHT11 #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 dht11(DHT11_PIN, DHT1); void setup() { Serial.begin(9600); // initialize serial dht11.begin(); // initialize the DHT sensor } void loop() { float temperature = dht11.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT11 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 above 25°C, and it stays on until the temperature falls 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 on the Libraries icon on the left side of the Arduino IDE.
  • Search for DHT and then find the DHT sensor library by Adafruit.
  • Click the Install button to add the library.
Arduino Nano 33 IoT DHT sensor library
  • You will be asked to install some extra library components.
  • Click the Install All button to install all the necessary libraries.
Arduino Nano 33 IoT Adafruit Unified sensor library
  • Copy the code for 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 hotter or colder.
  • Watch the relay to see its status.

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!