The DHT22 sensor is an electronic component designed to measure both temperature and relative humidity in the surrounding environment. It belongs to the family of temperature and humidity sensors produced by Aosong (also known as AOSONG).
To use the DHT22 sensor with an Arduino Uno board, you'll need to connect the sensor to the board and use a library to interact with it. Here are the steps to do so:
Hardware Connection: Connect the DHT22 sensor to the Arduino Uno board:
Software Setup:
1-Install the Library:
Open the Arduino IDE.
Go to "Sketch" > "Include Library" > "Manage Libraries."
Search for "dhtlib" and install the "DHT sensor library".
Write the Code: Use the code to read data from the DHT22 sensor
Upload the Code: Connect your Arduino Uno board to your computer.
To display the temperature and humidity readings from the DHT22 sensor on SSD1306 display using an Arduino, you'll need the following components:
Arduino UNO Board
The Arduino Uno board is a microcontroller board based on the ATmega328P microcontroller. It's one of the most popular and widely used boards in the Arduino family due to its simplicity and versatility.
It is the central control unit for your project.
DHT22 Sensor
The DHT22 sensor is popular for its relative accuracy, ease of use, and affordable cost, making it a common choice for various electronic projects. However, like any sensor, it has its limitations, and considering its specifications is important for accurate measurements.
SSD1306 Display
The SSD1306 is a popular display controller used in OLED (Organic Light-Emitting Diode) screens. It's commonly employed in various electronics projects to showcase information on small screens. The SSD1306 controller is compatible with different screen sizes and is often integrated with microcontrollers like Arduino to display text, graphics, and images.
This display is used for displaying the temperature and humidity from the DHT22 sensor .
Jumper Wires:
For making temporary connections and wiring between components.
Breadboard:
A breadboard is a useful tool for creating temporary electronic circuits. It allows you to connect components without soldering.
Mounting the Arduino board along with the DHT22 sensor and SSD1306 display can be done in various ways based on the preferences for the project's permanence, aesthetics, and convenience. Here's a suggestion on how to mount them:
Attaching the SSD1306 Display :
- connect the VCC pin of the display to 5V pin of the Arduino
- connect the GND pin of the display to GND pin of the Arduino
- connect the SDA pin of the display to A4 pin of the Arduino
- connect the SCL pin of the display to A5 pin of the Arduino
Mounting the DHT22 Sensor:
- connect the (+) pin of the DHT22 Sensor to 3.3V pin of the Arduino
- connect the (-) pin of the DHT22 to GND pin of the Arduino
- connect the OUT pin of the DHT22 to pin 2 of the Arduino
To display temperature and humidity measured by the DHT22 sensor on an SSD1306 OLED display using an Arduino Uno, you'll need to integrate both the DHT sensor library and the Adafruit SSD1306 library. Here's an example code that accomplishes this task:
Ensure you've installed the necessary libraries: dhtlib and Adafruit SSD1306.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <Adafruit_SSD1306.h> #include <dht.h> #define dataPin 2 // Defines pin number to which the sensor is connected dht DHT; // Creats a DHT object //OLED define #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); //For OLED I2C if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); for(;;); } display.display(); //Display logo delay(1000); display.clearDisplay(); } void loop() { delay(1000); int readData = DHT.read22(dataPin); // DHT22/AM2302 float t = DHT.temperature; // Gets the values of the temperature float h = DHT.humidity; // Gets the values of the humidity display.clearDisplay(); oledDisplayHeader(); oledDisplay(2,5,28,h,"%"); oledDisplay(2,60,28,t,"C"); display.display(); } void oledDisplayHeader(){ display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); display.print("Humidity"); display.setCursor(60, 0); display.print("Temperature"); } void oledDisplay(int size, int x,int y, float value, String unit){ int charLen=12; int xo=x+charLen*3.2; int xunit=x+charLen*3.6; int xval = x; display.setTextSize(size); display.setTextColor(WHITE); if (unit=="%"){ display.setCursor(x, y); display.print(value,0); display.print(unit); } else { if (value>99){ xval=x; } else { xval=x+charLen; } display.setCursor(xval, y); display.print(value,0); display.drawCircle(xo, y+2, 2, WHITE); // print degree symbols ( ) display.setCursor(xunit, y); display.print(unit); } } |
This code initializes the DHT22 sensor and SSD1306 display. It then reads temperature and humidity data from the DHT22 sensor and displays it on the SSD1306 OLED display.
Upload the code to your Arduino Uno and observe the readings on the SSD1306 OLED display and Serial Monitor.
Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info