Get to Know ESP32 #3: Temperature and Humidity Sensor Using DHT11
I’m currently snuggling in bed since Bandung is very cold lately. What’s the temperature here, anyway?
Back at it again with the sensors, but now we’re going to work with DHT11 sensor. DHT11 itself is a sensor to calculate the temperature and humidity of weather around you using capacitive humidity sensor and thermistor. It’s really inexpensive, but you can only get new data from the sensor every 1 second and it has lower accuracy compared to DHT22. It can measure from 0 to 50 degree Celcius with 2 degree accuracy, and 20–80% humidity with 5% accuracy.
Preparation
These are the equipments needed to do this project:
- 2 Male-to-Male Wire Jumpers
- 3 Male-to-Female Wire Jumpers
- 1 DHT11 Sensor
- 1 10k Ohm Resistor
- 1 Breadboard
- 1 NodeMCU ESP-32S Development Board
- 1 Laptop/PC with Arduino IDE installed and configured
- 1 USB-A to MicroUSB cable
Then, for the code, these are the libraries that need to be installed. To install it, go to Sketch>Include Library>Manage Libraries:
- DHT sensor library by Adafruit
- Adafruit Unified Sensor
Circuit
There are four pins in DHT11, and below is the pinout. Pin 1 is connected to 3.3 V, pin 2 is connected to any digital GPIO (here I’m using GPIO4), while pin 4 is connected to ground (GND).
So, I make a circuit that looked like this:
Below is the circuit that I managed to make.
Code
Now, let’s proceed with the code. Open Arduino IDE and use this code from randomnerdtutorials.
There are some insights that I learn from this code:
- #define DHTPIN 4
In this part, it is required to change the value to the GPIO you’re using. I’m using GPIO4 in this project, but as the comments in the code say, you can use pin 3, 4, 5, 12, 13, or 14, even 15 with special actions required.
- #define DHTTYPE DHT11
Since I’m using DHT11, I uncomment the DHT11 part.
- DHT dht(DHTPIN, DHTTYPE);
Create an object called dht with DHTPIN and DHTTYPE as inputs, while inputs have been specified above.
- Serial.begin(9600);
Baud rate is set to 9600.
- delay(1000);
It means that we get readings every second. It’s supposed to be 2000 in randomnerdtutorials, but since we have DHT11 — instead of DHT22, it can read from the sensor each second.
Demonstration
As usual, verify the code using the tick symbol, then connect your laptop to ESP32 using USB-A to Micro-USB cable. Upload the code, and when it says ‘Done uploading’, open Serial Monitor by pressing Ctrl+Shift+M or Tools>Serial Monitor. Make sure that the baud rate in Serial Monitor is 9600.
Since I want to confirm that my DHT11 sensor is working well, I put my hands around the sensor and breathe to the sensor and tadaaa, the temperature is increasing (and of course, the humidity too).
Next experiment, I use hand fan to make the air surrounding the sensor cooler. Guess what? It works! It doesn’t affect the humidity like my breath, though..
Evaluation
A little backstory, actually my team bought the wrong sensor, it’s BMP280! We’re supposed to buy the BME280 or BMP180, but yeah.. what we got was a mix of both. BMP or BME sensor requires soldering, so we tried to solder it, but we failed to use the sensor. Until this moment, we still haven’t figured out what made it unable to work — is it our soldering which fails, or perhaps the code didn’t match. We decided to borrow our friend’s sensor (thank you, Tasya!) for this project. And also, while trying out this sensor for the first time, my laptop broke and I had to go to BEC to fix it hahaha. It cost me Rp200.000,00 so….. I have to save more this month, i guess.
That’s it! Thank you for reading, and if you’re facing some problems with this project, make sure to drop some comments, I’ll try to help! :) See you!