Arduino Temperature and Humidity Sensor

About: Mechatronics engineer hosting Thundertronics electronics video blog on youtube... Saiyan Prince too.

In this tutorial, I am going to explain the making of a temperature and humidity sensor using Arduino pro mini board with DHT11 (or DHT22) sensor.

Step 1: Watch the Video

It is important to see the video first before moving to the next step. The video explains everything and demonstrates how it is done. However, in this post, I will write more technical data and details.

https://www.youtube.com/watch?v=56LKl7Xd770

Step 2: Needed Parts

The parts needed for this project are:

1- Arduino pro mini board (or any Arduino).

2- DHT11 temperature and humidity sensor (or DHT22).

3- 16x2 LCD display.

4- An enclosure of your choice, preferably the same as the one used in the video.

5- 10K potentiometer.

6- Screw terminals.

7- Resistors of different values.

8- 9v battery.

while the tools needed are:

1- hand drill like a Dremil.

2- different bits for the drill, since we will use smoothing bits and cutting bits.

3- helping hands.

plus, the usual electronics tools such as multimeter and so on.

Step 3: Schematic Design

In this project, I have chosen to make a PCB for it instead of wiring it myself. So I have used EasyEDA online tool for the job which was a nice experience.

This is the project's page at easyEDA website: https://easyeda.com/Hossam_VEGETA_Moghrabi/Arduin...

The explanation of schematic is as follows:

1- I have used a 6-pin ICSP adapter to program the Arduino pro mini since it doesn't come with one on board. it is J2 at schematic.

2- R2 is 100 Ohms and it sets the brightness of the LCD. Basically, you could put more resistance than 100R if you want the LCD backlight to be dimmer. Or better yet, get a potentiometer to act as a variable series resistance.

3- JP1 is just a connector which has a nice PCB footprint. I never put an actual terminal but instead soldered the wires. Do as you like.

4- U2 is the battery connection terminals. Here, I prefer a nice screw terminals to get firm connection. You could solder the wires, but be sure to put enough solder to make the connection solid enough to withstand any shakes.

5- LCD1 is the LCD component in easyEDA. It has the basic connection to Arduino pro mini. Be sure that pins here are identical to the ones in software.

6- RV1 is a 10K potentiometer to set LCD contrast. It should be used only once and it is when you first power the LCD.

Step 4: PCB Design

After finishing the schematic design and understand what everything means, it is now time for making a PCB for it.

You should press "Convert to PCB" in EasyEDA to create the PCB in PCB editor. Then, start placing parts and doing routing as usual. I suggest never using the auto-router though.

I have used lots of vias to move from top to bottom layer since the space is so little.

Step 5: Fabricate the PCB

Now, the PCB design is finished. We checked everything and no problem found. We need to send the design files (the gerbers) to the PCB fabrication company of our choice so it can do it for us.

My company of choice is JLCPCB. They are the best for such projects and prototyping and they offer just 2$ of price for an entire 10 pieces of your design!


So, now we click (....) and choose JLCPCB. We are directed to JLCPCB website since they are partners with EasyEDA. Now fill in everything and place the order. Now just wait until PCBs arrive.

It is worth mentioning that JLCPCB not only have EasyEDA associated with them, but they also have a big components store as well! The benefit here is getting both the PCB order and components order shipped together! Yes, no need to wait for 2 packages to arrive separately, but instead they come combined in one package. I highly recommend using this.

Step 6: Assembly

We have the PCBs alone with everything now. It is time to assemble everything together.

First, we need to solder the electronics according to schematic. It is an easy task for this project.

After finishing soldering, now cut the necessary holes in the plastic enclosure then fix the PCB with other components well inside using hot glue gun.

You should now use the potentiometer to adjust the contrast of the LCD, while choosing the required resistor valve for the brightness, I have chosen 100R.

Step 7: Code

Code for this project is attached with this step, and the explanation is as follows:

// include the library code:
#include <liquidcrystal.h> #include "DHT.h" // set the DHT Pin #define DHTPIN 2

Include necessary libraries and define pin 2 of Arduino pro mini as the data pin for the sensor. Be sure to install these libraries if you don't have them.

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 7, 6, 5, 4); #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE);

Now initialize LCD library with these pins according to schematic itself. Also use DHT library and choose DHT11 as the sensor to use, so if you have DHT22 then you should change it.

Last line says we have DHT11 sensor and its data pin is at pin "DHTPIN" which is pin 2 as we defined it previously.

void setup() {
// set up the LCD's number of columns and rows: lcd.begin(16, 2); dht.begin(); lcd.setCursor(0,0); lcd.print("Temperature and"); lcd.setCursor(0,1); lcd.print("humidity sensor"); delay(3000); lcd.clear(); lcd.setCursor(0,0); lcd.print("THUNDERTRONICS"); lcd.setCursor(0,1); lcd.print("Hossam Moghrabi"); delay(3000); }

Now it is setup time! and here is what is going on:

LCD is 16 by 2 type.

Start DHT command to get values.

Print "Temperature and humidity sensor" on the 2 lines.

Delay 3 seconds.

Clear display

Print "THUNDERTRONICS" at first line then print "Hossam Moghrabi" at 2nd line.

Delay 3 seconds.

^
I've done this as a welcome screen which lasts around 6 seconds or so before values are displayed.

void loop() {
// read humidity int h = dht.readHumidity(); //read temperature in c int t = dht.readTemperature(); if (isnan(h) || isnan(t)) { lcd.print("ERROR"); return; }

Now we are inside our eternal loop which will keep repeating itself.

Store humidity readings inside "h" variable and temperature readings inside "t" variable.

Next, we have an if statement. This is basically returns an error message when there is an error. Leave it without changing it.

Now we have all the values that we need.

lcd.setCursor(0,0);
lcd.print("Temp. = "); lcd.print(t); lcd.print(" "); lcd.print((char)223); lcd.print("C"); lcd.setCursor(0,1); lcd.print("Humidity = "); lcd.print(h); lcd.print (" %"); // lcd.print("Hossam Moghrabi"); delay(2000);

Finally, we display these values on LCD display. You can change it the way you want because it is simply printing values inside "h" and "t" variables. Putting a delay of 2 seconds is kinda optional but you won't benefit much from doing it faster since the sensor itself is not that fast and even if it is, still the physical values never change that fast. So 2 seconds are very very fast for the job!

That is it!

Share

    Recommendations

    • Big and Small Contest

      Big and Small Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • First Time Author

      First Time Author

    Discussions