ESP32

Display DHT21 Temperature Sensor Data on 16×2 I2C LCD and SSD1306 Oled display using ESP32

Introduction:

The DHT21 temperature and humidity sensor is a popular choice among hobbyists and DIY enthusiasts for measuring environmental conditions. In this article, we will explore how to display the data from the DHT21 temperature sensor on the serial monitor and serial plotter, as well as on 16×2 i2c lcd and SSD1306 oled display using ESP32. To learn how to interface a 16×2 I2C LCD with Arduino, visit my previous article.

A photograph showing the interface between a 16x2 I2C LCD and an SSD1306 OLED display module with an ESP32 microcontroller.

By the end of this tutorial, you will have a clear understanding of how to read data from the DHT21 sensor and how to display it on various output devices, enabling you to create your own temperature and humidity monitoring system. So let’s get started




Amazon links:

ESP32 board

DHT22 temperature sensor

Breadboard

Jumper wires

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

About ESP32 Board:

The ESP32 is a powerful and versatile microcontroller board designed for Internet of Things (IoT) projects. It was developed by Espressif Systems, a Chinese company that specializes in Wi-Fi and Bluetooth technologies. The ESP32 features a dual-core Tensilica processor, clocked at up to 240MHz, and comes with 520KB of SRAM and 4MB of flash memory. It also has built-in Wi-Fi and Bluetooth capabilities, which makes it an ideal choice for building connected devices that require wireless connectivity.

An image illustrating the DHT21 temperature and humidity sensor connected to an ESP32 board with labeled pin descriptions.

One of the key features of the ESP32 is its high number of input/output (I/O) pins. It has a total of 38 pins, which can be used for a wide range of tasks. These pins are divided into different categories, each with a specific function. The main categories of pins on the ESP32 board are:

  • Power pins: These pins are used to provide power to the board. The ESP32 can be powered using either the USB port or an external power supply. The power pins include 3.3V, 5V, and GND.
  • Analog input pins: The ESP32 has 18 analog input pins, which can be used to read analog signals from sensors or other devices. These pins are labeled A0 to A17.
  • Digital input/output pins: The ESP32 has 22 digital I/O pins, which can be used for both input and output. These pins are labeled D0 to D19, and also include pins for Serial Peripheral Interface (SPI), Inter-Integrated Circuit (I2C), and Universal Asynchronous Receiver-Transmitter (UART) communication.
  • Other pins: The ESP32 also has a number of other pins that are used for specific purposes. These include pins for the built-in LED, the boot button, and the flash memory.

In addition to these pins, the ESP32 also has a number of other features that make it a powerful platform for building IoT projects. These include:

  • Wi-Fi and Bluetooth connectivity: The ESP32 has built-in Wi-Fi and Bluetooth capabilities, which allows it to connect to other devices and the Internet.
  • Low power consumption: The ESP32 is designed to be energy efficient, which makes it ideal for battery-powered devices.
  • Support for multiple programming languages: The ESP32 can be programmed using a variety of programming languages, including C, C++, and Python.
  • Support for a wide range of development environments: The ESP32 can be programmed using the Arduino IDE, MicroPython, or other popular development environments.



About DHT21 Temperature Sensor:

A close-up photograph of a DHT21 temperature and humidity sensor connected to an ESP32 microcontroller board

The DHT21 is a temperature and humidity sensor that is widely used in IoT projects and home automation systems. It is a digital sensor that uses a capacitive humidity sensor and a thermistor to measure the surrounding air temperature and humidity. The DHT21 is also known as the AM2302 and is compatible with a variety of microcontrollers, including Arduino, ESP32, and Raspberry Pi.

The DHT21 temperature sensor is a digital sensor that is capable of measuring both temperature and humidity. It has a temperature measurement range of -40°C to 80°C with an accuracy of ±0.5°C. The humidity measurement range is from 0% to 100% with an accuracy of ±2%. The sensor operates at a voltage of 3.3V to 5V DC and has a current consumption of less than 1mA during measurement.

The DHT21 sensor uses a capacitive humidity sensor and a thermistor to measure humidity and temperature, respectively. The sensor has a response time of 2 seconds for humidity and 5 seconds for temperature measurements. The sensor has a digital output and communicates using a single-wire protocol.

To use the DHT21, it is first necessary to connect it to the microcontroller. The sensor has four pins: VCC, GND, Data, and a notched pin that indicates the orientation of the sensor. The VCC and GND pins are connected to the power supply, while the Data pin is connected to a digital input pin on the microcontroller.

Once the sensor is connected, the microcontroller can communicate with the sensor using a digital protocol known as one-wire communication. This involves sending a signal to the sensor to request a measurement, and then receiving a response from the sensor containing the temperature and humidity readings. The DHT21 uses a checksum to verify the accuracy of the data, which helps to ensure that the readings are reliable.

One of the main applications of the DHT21 is in home automation and climate control systems. The sensor can be used to monitor the temperature and humidity in a room, and then adjust the heating or cooling system accordingly. This can help to improve energy efficiency and reduce costs, while also providing a more comfortable environment.

The DHT21 can also be used in agriculture and horticulture applications, where it can be used to monitor the temperature and humidity in a greenhouse or grow room. This can help to optimize plant growth and improve yields, while also reducing the risk of disease or pest infestation.

In addition to these applications, the DHT21 can also be used in weather monitoring systems, industrial control systems, and a variety of other applications. Its low cost, ease of use, and high accuracy make it a popular choice for a wide range of projects.



DHT21 Temperature Sensor feature

The DHT21 temperature sensor, also known as the AM2301, is a digital temperature and humidity sensor that is similar to the DHT22 sensor. Like the DHT22, it uses a capacitive humidity sensor and a thermistor to measure temperature and humidity levels. However, there are a few key differences between the two sensors:

  • Accuracy: The DHT21 has a slightly lower accuracy compared to the DHT22, with a temperature accuracy of ±0.5°C and a humidity accuracy of ±3% RH.
  • Range: The DHT21 has a slightly smaller measurement range compared to the DHT22, with a temperature range of -40°C to 80°C and a humidity range of 0-100% RH.
  • Response time: The DHT21 has a slightly slower response time compared to the DHT22, with a response time of 2 seconds for humidity and 5 seconds for temperature.
  • Size: The DHT21 is slightly smaller in size compared to the DHT22, with dimensions of 15.1mm x 12.7mm x 5.6mm.

Despite these differences, the DHT21 remains a popular and reliable sensor for measuring temperature and humidity levels in a wide range of applications, including home automation, weather monitoring, and industrial control systems. Its lower cost and smaller size make it a more cost-effective and space-saving alternative to the DHT22 in certain applications.

DHT21 temperature sensor technical specifications

The DHT21 temperature sensor, also known as the AM2301, is a digital temperature and humidity sensor with the following technical specifications:

  • Measurement range: The DHT21 has a temperature measurement range of -40°C to 80°C and a humidity measurement range of 0-100% RH.
  • Accuracy: The DHT21 has a temperature accuracy of ±0.5°C and a humidity accuracy of ±3% RH.
  • Resolution: The DHT21 has a temperature resolution of 0.1°C and a humidity resolution of 0.1% RH.
  • Response time: The DHT21 has a response time of 2 seconds for humidity and 5 seconds for temperature.
  • Supply voltage: The DHT21 operates on a supply voltage of 3-5.5V DC.
  • Current consumption: The DHT21 has a current consumption of 1-1.5mA during active operation and 100uA during standby mode.
  • Communication protocol: The DHT21 communicates using a single-wire digital interface, with data transmitted as a series of bits.
  • Physical dimensions: The DHT21 has a compact size with dimensions of 15.1mm x 12.7mm x 5.6mm.

The DHT21 sensor is a reliable and cost-effective option for measuring temperature and humidity levels in a wide range of applications, including home automation, weather monitoring, and industrial control systems. Its accurate and reliable measurements, combined with its compact size and low power consumption, make it a popular choice for a variety of projects.



Applications of DHT21 Temperature Sensor:

The DHT21 temperature sensor has a wide range of applications in various industries. Some of these applications include:

  • Weather monitoring: The DHT21 temperature sensor is used in weather monitoring systems to measure temperature and humidity levels. This is useful for predicting weather patterns and ensuring safety in extreme weather conditions.
  • HVAC systems: The DHT21 temperature sensor is used in heating, ventilation, and air conditioning (HVAC) systems to measure temperature and humidity levels in homes, buildings, and other structures.
  • Agriculture: The DHT21 temperature sensor is used in agriculture for monitoring temperature and humidity levels in greenhouses and other agricultural settings. This helps in controlling the growth of plants and improving crop yield.
  • Industrial automation: The DHT21 temperature sensor is used in industrial automation systems for monitoring temperature and humidity levels in manufacturing and processing plants. This helps in maintaining quality control and ensuring product safety.

Advantages of Using the DHT21 Temperature Sensor

The DHT21 Temperature Sensor offers several advantages that make it a preferred choice for temperature sensing:

  • Accurate Readings: With its high level of accuracy, the sensor provides precise temperature and humidity readings, ensuring reliable data for critical applications.
  • Easy Integration: The sensor’s digital output and compatibility with popular microcontrollers, such as Arduino boards, make it easy to integrate into projects without complex wiring or additional components.
  • Cost-Effective: The DHT21 Temperature Sensor is an affordable option compared to other high-precision temperature sensors available in the market, making it suitable for budget-conscious projects.
  • Low Power Consumption: Its low power requirements make it ideal for battery-operated devices, extending the battery life and reducing overall power consumption.
  • Versatility: The sensor’s wide measurement range and compatibility with different platforms make it versatile for a wide range of applications, from home automation to industrial monitoring.

Comparing the DHT21 Temperature Sensor with Other Temperature Sensors

The market offers various temperature sensors with different specifications and features. Let’s compare the DHT21 Temperature Sensor with a few popular alternatives:

Sensor DHT21 Temperature Sensor DHT11 Temperature Sensor DS18B20 Digital Temperature Sensor
Measurement Range -40°C to 80°C 0°C to 50°C -55°C to 125°C
Humidity Range 0% to 100% 20% to 90% Not Applicable
Accuracy ±0.5°C ±2°C ±0.5°C
Digital Output Yes Yes Yes
Communication Single-Wire Interface Single-Wire Interface One-Wire Interface
Power Consumption Low Low Low

While the DHT21 Temperature Sensor offers a wider temperature range, higher accuracy, and a broader humidity range compared to the DHT11, it may not match the DS18B20’s extended temperature range. However, the DS18B20 operates on a different communication protocol. The choice of sensor depends on the specific requirements of your project and the range of temperatures and humidity levels you need to monitor.




DHT21 Temperature Sensor vs. DHT11: Which One Should You Choose?

The DHT21 Temperature Sensor and DHT11 are popular choices for temperature and humidity sensing applications. Here’s a comparison to help you make an informed decision:

  • Temperature Range: The DHT21 Temperature Sensor offers a wider temperature range (-40°C to 80°C) compared to the DHT11 (0°C to 50°C). If your application requires measurements beyond the DHT11’s range, the DHT21 would be a better choice.
  • Humidity Range: The DHT21 Temperature Sensor has a broader humidity range (0% to 100%) than the DHT11 (20% to 90%). If your project involves high humidity environments, the DHT21 is more suitable.
  • Accuracy: The DHT21 Temperature Sensor provides higher accuracy (±0.5°C) compared to the DHT11 (±2°C). If precise temperature readings are crucial for your application, the DHT21 is the preferred option.
  • Price: The DHT11 is generally more affordable than the DHT21. If cost is a significant factor, the DHT11 may be a suitable choice for budget-conscious projects.
  • Application Requirements: Consider the specific requirements of your application. If you need a temperature sensor for a wide range of temperatures and humidity levels with higher accuracy, the DHT21 is the recommended option. However, if your project operates within the DHT11’s temperature and humidity range and cost is a concern, the DHT11 can fulfill basic temperature sensing needs.

Ultimately, the choice between the DHT21 and DHT11 depends on your project’s requirements, budget, and the desired level of accuracy and temperature range.

Required Library installation:

DHT.h Library installation

To install the DHT21 library in the Arduino IDE for ESP32, you can follow these steps:

Open the Arduino IDE and navigate to “Sketch” > “Include Library” > “Manage Libraries”

A screenshot showcasing the installation of the DHT21 temperature and humidity library through the library manager.

In the “Library Manager” window, search for “DHT sensor library” in the search bar Look for the library named “DHT sensor library by Adafruit” and click on it to select it.

A screenshot displaying the installation process of the DHT21 temperature and humidity library for the ESP32 microcontroller.

Click the ‘Install’ button to begin the installation process, but in my case, I have already installed it. That’s why it’s not showing me the ‘Install’ button. Please wait for the installation to complete.

Once the installation is complete, you can close the “Library Manager” window.



Adafruit_SSD1306.h Library installation

The Adafruit_SSD1306.h library is a display library for Arduino and compatible microcontrollers that provides an interface for driving OLED displays based on the SSD1306 controller chip. Here are the steps to install the Adafruit_SSD1306.h library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

In the Library Manager, search for “Adafruit SSD1306” in the search bar. The library should appear in the search results.

A screenshot showcasing the installation process of the SSD1306 OLED display library for integrating the DHT21 temperature and humidity sensor with an ESP32 microcontroller.

Click on the Adafruit SSD1306 library and then click the “Install” button to install the library in my case I installed it already that why it display the update button.

After installation is complete, close the Library Manager and return to the Arduino IDE.

To use the library in your code, you will need to include the Adafruit_SSD1306.h library at the top of your sketch.

Adafruit_GFX.h Library installation

The Adafruit_GFX.h library is a graphics library for Arduino and compatible microcontrollers that provides a set of common graphics operations, such as drawing lines, rectangles, and circles. Here are the steps to install the Adafruit_GFX.h library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.

In the Library Manager, search for “Adafruit GFX” in the search bar. The library should appear in the search results.

A screenshot demonstrating the installation process of the Adafruit SSD1306 OLED and Adafruit GFX libraries for integrating the DHT21 temperature and humidity sensor with an ESP32 microcontroller.

Click on the Adafruit GFX by adafruit library and then click the “Install” button to install the library but in my case I installed that why it display the update button.

After installation is complete, close the Library Manager and return to the Arduino IDE.

Adafruit busIO Library installation

The Adafruit BusIO library is a communication library for Arduino and compatible microcontrollers that provides a set of common communication protocols, such as I2C, SPI, and UART. Here are the steps to install the Adafruit BusIO library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

In the Library Manager, search for “Adafruit BusIO” in the search bar. The library should appear in the search results.

A screenshot illustrating the installation process of the Adafruit BusIO library for integrating the DHT21 temperature and humidity sensor with an ESP32 microcontroller.

Click on the Adafruit Bus library and then click the “Install” button to install the library but in my case I installed it already that why it display the update button.

After installation is complete, close the Library Manager and return to the Arduino IDE.



16×2 i2c library installation

To use a 16×2 I2C LCD display with your ESP32, you’ll need to install a library that supports the display. Here are the steps to install the LiquidCrystal_I2C library in the Arduino IDE for ESP32:

Open the Arduino IDE and click on “Sketch” in the menu bar, then navigate to “Include Library” and select “Manage Libraries.”

In the Library Manager, search for “LiquidCrystal_I2C” in the search bar. The library should appear in the search results.

A screenshot showcasing the installation process of the 16x2 I2C LCD library for integrating the DHT21 temperature and humidity sensor with an ESP32 microcontroller.

Click on the LiquidCrystal_I2C by frank de brabander library and then click the “Install” button to install the library.

After installation is complete, close the Library Manager and return to the Arduino IDE.

To use the library in your code, you will need to include the LiquidCrystal_I2C library at the top of your sketch.

Display DHT21 Temperature Sensor Data on Esp32 Serial Monitor:

The DHT21 sensor is a digital sensor that can measure temperature and humidity with a high degree of accuracy. In this example, we will learn how to display DHT21 temperature sensor data on the ESP32 serial monitor.

Required Components:

ESP32 Development Board

DHT21 Temperature Sensor

Breadboard

Jumper Wires



Circuit diagram:

Before we start coding, let’s connect the DHT21 sensor to the ESP32 development board. Connect the VCC pin of the DHT21 sensor to the 3.3V pin of the ESP32, the GND pin to the GND pin of the ESP32, and the DATA pin to gpio 5 of the ESP32.

A circuit diagram showing the connection between a DHT21 sensor, an ESP32 microcontroller, and the display of data on the serial monitor and serial plotter.

displaying Data on Serial Monitor Program:

Programming explanation:

This is a simple Arduino sketch that reads temperature and humidity data from a DHT21 sensor and displays it on the serial monitor.

This line includes the DHT library which provides functions for working with DHT temperature and humidity sensors.

This line defines the esp32 gpio to which the DHT21 sensor is connected. In this case, it is connected to gpio 5.

This line defines the type of DHT sensor being used. In this case, it is the DHT21 sensor.

These lines declare two float variables to hold the humidity and temperature values.

This line creates a DHT object with the pin and type defined earlier.

This is the setup function that runs once when the esp32 board is powered on. It initializes the serial communication at a baud rate of 9600 and initializes the DHT sensor using the begin() function.

This is the main loop function that runs continuously after the setup function. It reads the humidity and temperature data from the sensor using the readHumidity() and readTemperature() functions respectively. It then displays the values on the serial monitor using the Serial.print() function. The delay(2000) function adds a delay of 2 seconds between each reading.




Practical demonstration:

A screenshot capturing the display of DHT21 temperature and humidity data on the serial monitor using an ESP32 microcontroller.

Display DHT21 Temperature Sensor Data on Esp32 Serial Plotter:

When combined with the ESP32 board and its programming environment, the DHT21 sensor can be used to create a powerful and versatile temperature and humidity monitoring system. By using the ESP32’s built-in Serial Plotter tool, it is possible to visualize and compare data from multiple DHT21 sensors in real-time.

With the ESP32 and DHT21 sensor, it is possible to create a system that can monitor temperature and humidity levels in different areas of a building or system, making it ideal for industrial or agricultural applications. The ability to plot data from multiple sensors on a single graph allows for easy comparison of temperature and humidity levels across different areas, making it easy to identify patterns and trends.

There are several advantages to displaying DHT21 temperature sensor data on the ESP32 Serial Plotter:

  • Real-time monitoring: The ESP32 Serial Plotter displays the data in real-time as it is received over the serial port. This allows for immediate monitoring of temperature and humidity levels, which is particularly useful for applications that require continuous monitoring of environmental conditions.
  • Multiple sensor visualization: The ESP32 Serial Plotter can display data from multiple sensors or sources on a single graph, making it easy to compare and analyze different variables. This can be useful for monitoring multiple environmental conditions at the same time, such as temperature, humidity, and air quality.
  • Graphical representation: The graphical representation of the data on the Serial Plotter makes it easier to visualize trends and patterns in the data, compared to raw numerical data. This can help in identifying patterns that may not be immediately obvious from looking at raw data.
  • Customizable settings: The ESP32 Serial Plotter allows for customization of various settings such as the graph scale, colors, and data range. This makes it possible to adjust the display to fit the specific needs of the user.
  • Cost-effective solution: The ESP32 board is a relatively low-cost solution for monitoring environmental conditions compared to other specialized monitoring equipment. By displaying the data on the ESP32 Serial Plotter, the user can have a cost-effective solution for monitoring temperature and humidity levels in real-time.

Overall, the combination of the DHT21 sensor and ESP32 board with Serial Plotter provides a powerful and flexible platform for temperature and humidity monitoring, with the potential for a wide range of applications.



DHT21 Temperature Sensor with ESP32 Circuit Diagram:

I used the same circuit diagram for displaying the DHT21 temperature sensor values on esp32 serial plotter the connection is already explained in the above example.

A circuit diagram showing the connection between a DHT21 sensor, an ESP32 microcontroller, and the display of data on the serial monitor and serial plotter.

Programming:

Programming explanation:

This is an Arduino sketch that reads temperature and humidity data from a DHT21 sensor and plot it on the esp32 serial plotter.

This line includes the DHT library which provides functions for working with DHT temperature and humidity sensors.

These lines define the esp32 gpio to which the DHT21 sensor is connected in this case it is connected with gpio 5 and the type of DHT sensor being used, respectively.

These lines declare two float variables to hold the humidity and temperature values.

This line creates a DHT object with the pin and type defined earlier.

This is the setup function that runs once when the esp32 is powered on. It initializes the serial communication at a baud rate of 9600 and initializes the DHT sensor using the begin() function.

This is the main loop function that runs continuously after the setup function. It reads the humidity and temperature data from the sensor using the readHumidity() and readTemperature() functions respectively. It then plot the values on the esp32 serial plotter using the Serial.print() and Serial.println() functions. The delay(500) function adds a delay of 500 milliseconds between each reading.



Practical demonstration:

A screenshot displaying temperature and humidity data from a DHT21 sensor plotted on the serial plotter of an ESP32 microcontroller

Display DHT21 Temperature Sensor Data on 16×2 I2C lcd using esp32:

In this example, we will learn how to display the temperature and humidity data from a DHT21 sensor on a 16×2 I2C LCD using an ESP32 microcontroller.

Circuit diagram:

A circuit diagram illustrating the connection between an ESP32 microcontroller, a DHT21 sensor, and a 16x2 I2C LCD display for displaying the DHT21 data.

Firstly, the 16×2 I2C LCD is connected to the ESP32 using the I2C protocol. The I2C protocol uses two wires: SDA (serial data) and SCL (serial clock). These wires are used to transmit data between the ESP32 and the LCD display. In this circuit, SDA is connected to GPIO 21 on the ESP32, and SCL is connected to GPIO 22.

Next, the DHT22 temperature and humidity sensor is connected to the ESP32 using a digital pin. In this circuit, the data pin of the DHT21 is connected to GPIO 5 on the ESP32.

Finally, the power and ground connections are made. The VCC pin of the DHT21 is connected with esp32 3.3v and the 16×2 I2C LCD is connected to the 5v pin of the ESP32, and the GND pins are connected to the GND pin of the ESP32.



16×2 i2c lcd Program:

Programming explanation:

This section of code includes the necessary libraries for the DHT sensor, I2C communication, and the LiquidCrystal_I2C library for controlling the LCD display. The DHTPIN and DHTTYPE are defined, with the former being the GPIO pin the DHT sensor is connected to and the latter indicating the type of DHT sensor used. The LiquidCrystal_I2C object is defined and initialized with the address of the LCD display (0x27) and its dimensions (16×2). Two float variables, temperature and humidity, are initialized to 0.0. Finally, a DHT object is created with the DHTPIN and DHTTYPE.

This section of code is the main loop of the program. The readTemperature() and readHumidity() functions of the DHT object are called to read the temperature and humidity values from the sensor, respectively. The lcd.clear() function is called to clear the display, and the lcd.setCursor() function is used to set the cursor position to the top left corner of the display. The temperature value is printed to the display using the lcd.print() function, along with the text “Temp: ” and the units “C”. The cursor is then moved to the second row of the display, and the humidity value is printed in a similar manner. The program then waits for one second using the delay() function before repeating the loop.

Overall, this code reads the temperature and humidity values from a DHT21 sensor connected to GPIO pin 5 of an ESP32 microcontroller, and displays those values on a 16×2 I2C LCD display.




Practical demonstration:

A photograph showcasing the display of DHT21 temperature and humidity data on a 16x2 I2C LCD using an ESP32 microcontroller.

Display DHT21 Temperature Sensor Data on SSD1306 Oled display using esp32:

Displaying temperature sensor data on an OLED display is a fun and easy way to monitor temperature changes in real-time. In this tutorial, we will show you how to use an ESP32 microcontroller to read data from a DHT21 temperature sensor and display it on a 128×64 SSD1306 OLED display.

Materials

To complete this project, you will need the following materials:

  • ESP32 Development Board
  • DHT21 Temperature and Humidity Sensor
  • SSD1306 OLED Display (128×64 pixels)
  • Breadboard
  • Jumper Wires

SSD1306 Oled display Circuit diagram:

A circuit diagram illustrating the connection between an ESP32 microcontroller, a DHT21 temperature sensor, and an SSD1306 OLED display for displaying the DHT21 temperature data.

Firstly, the SSD1306 OLED Display  is connected to the ESP32 using the I2C protocol. The I2C protocol uses two wires: SDA (serial data) and SCL (serial clock). These wires are used to transmit data between the ESP32 and the SSD1306 OLED display. In this circuit, SDA is connected to GPIO 21 on the ESP32, and SCL is connected to GPIO 22.

Next, the DHT22 temperature and humidity sensor is connected to the ESP32 using a digital pin. In this circuit, the data pin of the DHT21 is connected to GPIO 5 on the ESP32.

Finally, the power and ground connections are made. The VCC pin of the DHT21 and the SSD1306 OLED display are connected to the 3.3v pin of the ESP32, and the GND pins are connected to the GND pin of the ESP32.



display DHT21 Temperature Sensor data on Oled Program:

Programming explanation:

This is the code for displaying the temperature and humidity data on the SSD1306 OLED display using an ESP32 microcontroller and a DHT21 temperature sensor.

These libraries provide the necessary functions and constants for reading data from the DHT21 sensor and displaying data on the OLED display.

The code defines several constants, including DHTPIN (the GPIO pin that the DHT21 sensor is connected to), DHTTYPE (the type of DHT sensor being used, in this case DHT21), SCREEN_WIDTH and SCREEN_HEIGHT (the dimensions of the OLED display), and OLED_RESET (the reset pin for the OLED display, which is set to -1 because it is not used in this code).

The code creates two objects, one for the DHT sensor and one for the OLED display. The DHT object is created with the DHTPIN and DHTTYPE constants as parameters, and the display object is created with the SCREEN_WIDTH, SCREEN_HEIGHT, &Wire (the I2C bus object), and OLED_RESET constants as parameters.

The setup function is called once when the ESP32 is powered on or reset. In this function, the serial communication is initiated at a baud rate of 9600. The DHT sensor is initialized using the dht.begin() function. The OLED display is also initialized using the display.begin() function with the SSD1306_SWITCHCAPVCC parameter (which sets the voltage for the display) and the 0x3C address (which is the default address for the SSD1306 display). A delay of 2 seconds is added to give the display time to start up. The display is cleared using the display.clearDisplay() function, and the text color is set to white using the display.setTextColor() function.

The loop function is called repeatedly after setup() is executed. In this function, the humidity and temperature readings from the DHT21 sensor are obtained using the dht.readHumidity() and dht.readTemperature() functions, respectively. The display is cleared using the display.clearDisplay() function, and the text size is set to 2 using the display.setTextSize() function. The cursor is set to position (0,10) using the display.setCursor() function, and the temperature reading is displayed using the display.print() function. The degree symbol (°) is printed using the character code 247, which is cast as a char using (char)247. The Celsius symbol (C) is also printed. The cursor is then moved to position (0,30) using the display.setCursor() function, and the humidity reading is displayed using the display.print() function. The display is updated using the display.display() function, and a delay of 1 second is added using the delay() function.

Overall, this code reads temperature and humidity data from a DHT21 sensor and displays it on an SSD1306 OLED display using an ESP32 microcontroller.



Practical demonstration:

A photograph showcasing the display of DHT21 temperature and humidity data on an SSD1306 OLED display module using an ESP32 microcontroller.

Frequently Asked Questions (FAQs)

FAQ 1: What is the operating voltage of the DHT21 Temperature Sensor?

The DHT21 Temperature Sensor operates at a voltage between 3.3V and 5V, making it compatible with most microcontrollers and development boards.

FAQ 2: Can the DHT21 Temperature Sensor measure humidity as well?

Yes, the DHT21 Temperature Sensor can accurately measure both temperature and humidity. It incorporates a humidity-sensitive capacitive element alongside the thermistor for precise humidity readings.

FAQ 3: Does the DHT21 Temperature Sensor require a pull-up resistor?

No, the DHT21 Temperature Sensor has an internal pull-up resistor, eliminating the need for an external one. This simplifies the wiring and integration process.

FAQ 4: Can the DHT21 Temperature Sensor be used outdoors?

While the DHT21 Temperature Sensor can be used outdoors, it is not weatherproof by default. If exposed to direct sunlight or extreme weather conditions, it is recommended to use a waterproof enclosure or provide additional protection.



Watch Video Tutorial:

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button