Arduino Nano Temperature and Humidity With DHT11 and an LCD

by Sjoerd Hekking in Circuits > Arduino

1119 Views, 6 Favorites, 0 Comments

Arduino Nano Temperature and Humidity With DHT11 and an LCD

IMG_20210221_143720.jpg

Introduction:

With many guides online about attaching an LCD to an Arduino and guides about attaching a DHT11 sensor to an Arduino I have not found a single guide also doing this for the Arduino Nano and a 9v battery to make it small, portable, and easy to place wherever you want, for example in a terrarium or an ant nest. This tutorial could also be really great for learning some basics about all the components that will be used in this build. To get started you should get yourself the following things:

Physical Item:

  • Arduino Nano (ATmega328P) + (pre-soldered pins)
  • LCD1602 16x2 I2C (PCF8574 adapter board
  • DHT11 Temperature and humidity sensor
  • Jumper cables F/F * 8
  • Breakoutpins
  • Mechanical Switch

Software Needed:

  • Arduino IDE (how to install libraries will be explained in the next steps)
  • Library: LiquidCrystal I2C (by Frank de Brabander)
  • Library: Adafruit Unified Sensor (by Adafruit)
  • Library: DHT Sensor library (by Adafruit)
  • The file attached in the following steps

Tools needed:

  • Soldering iron (you can skip this but you need this to build a 3rd common GND)
  • Computer/Laptop
  • Micro USB to USB (connect the Arduino to your computer)
  • Basic knowledge of programming and circuit diagrams (or none)

Making the Arduino

IMG_20210221_143131.jpg
IMG_20210221_141737.jpg
arduino-nano-pinout.png
unnamed.jpg
DHT11–Temperature-Sensor-Pinout.jpg

In the picture, you can see most of the main components of this build. But we will go over them shortly!

Arduino Nano (ATmega328P) + (pre-soldered pins):

Why did I choose the nano? Low power consumption and cheap costs! That's about it! This component will also be the brains of everything and connect the LCD and the DHT11. You might be wondering how to power both components, luckily the nano has a 3.3v output and a 5v output (perfect!). The nano will be connected to the computer via a cable, the Arduino IDE must be set to, Board: "Arduino Nano", Processor: "ATmega328P (or the one you choose) and Port: "The one you connected to". My advice will also be to buy the pre-soldered header pin version, this costs you maybe a few cents extra, but it requires almost no soldering anymore!

LCD1602 16x2 I2C (PCF8574 adapter board):

Any LCD screen that you are familiar with would work since screens are screens, but for this tutorial, we will be using a 16x2 one! Also, be sure to get the I2C variant, since it reduces the jumper cables needed from 20~ to 8~, which is first of all cheaper, and secondly easier to manage, and thirdly less of a nightmare to program. Get yourself a fancy colored one or like me the basic green one. Via the Nano, we will "write" things to display!

DHT11 Temperature and humidity sensor:

To be fair, the DHT11 is a little less accurate and has bigger offsets, but it's 5x as cheap as the DHT22! At the time of writing this, it cost me 2.5$ for 6 units, and 1x DHT22 costs 2.5$, and for future purposes, it's perfect to use the DHT11. This thing will send data to the Nano, the nano will process it and you can easily write it to the LCD

Jumper cables F/F * 8:

Feel free to use anything you like, but Female to Female jumper cables are more than perfect, they make the build "big" and add lots of not-needed wire, but you can cut them or buy smaller ones. Basically, without these, you can't connect the sensor to the LCD

Breakoutpins:

Just a loose rack of them, since we need to build common ground, the Nano has 2, but we need 3! So solder the end of 4 of them together, you can easily use something else, but this was what I had laying around.

Mechanical Switch:

Completely optional, but will drastically extend battery life, a simple switch could be placed in between the VIN line and the Positive line of the battery. I won't be using it in the diagram, but this should be really straightforward! (I hope).

9V battery:

To make it portable and have more than 10 hours of screen time, use this! Hence why I suggested installing a switch, since you won't be able to monitor it always, so only turn it on when you are looking at it!

Diagram:

I know it's not a real circuit diagram and it looks rather amateur-like, but it serves its purpose and is easy to follow (in my opinion). Anyhow I will write down the connection for written purposes.

  1. DHT11 VCC -> Nano 3.3v
  2. DHT11 DATA -> Nano D2
  3. DHT11 GND -> common ground
  4. LCD VCC -> Nano 5v
  5. LCD SDA -> Nano A4
  6. LCD SCL -> Nano A5
  7. LCD GND -> common ground
  8. Commonground -> Nano GND (choose the one next to D2)
  9. 9V positive -> Nano VIN
  10. 9V negative -> GND (next to VIN)

Programming the Arduino

Schermopname (112).png

The next part will be programming, assuming you have correctly done step one you can now connect your nano via the cable to the pc/laptop with the Arduino IDE. You don't and mustn't use the 9V battery during this step!! The laptop/pc will power everything just fine!

Open the IDE and install the libraries seen in the screenshot!

Next, it's time to start programming:

You can either download the program and simply open it via the IDE, I excluded all comments since it keeps the script smaller and we will go over them here.

// Maybe by the wizard Tigris

// Include the sensor library and define the DHT type (11 and 22 exist)
#include <Adafruit_Sensor.h>
#define DHTTYPE DHT11


// Include the DHT library and LCD library and the wire one.
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


// Define the pin we connect the data too
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE, 6);


// Define the LCD type and size and address
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);


// Define where to draw power from
#define powerpin 5


// begin the setup for the program


void setup(){
  Serial.begin(9600);
  
  pinMode(powerpin, OUTPUT);

  lcd.init();
  lcd.backlight();
  dht.begin();
}


// This will be looped and makes the data overwrite so it changes realtime
void loop(){
	

  // The floats define the readings and the delay is needed
  // Without a delay it might show NaN sometimes!
  float temp = dht.readTemperature();
  delay(3000);
  float humi = dht.readHumidity();


  // Format the output, I choose to make it small and easy to read
  // You can remove this! it's only needed for console, to test if it works
  // Sometimes the LCD screens don't function, so at least you know console works
  Serial.print("Temperature = ");
  Serial.println(temp);
  Serial.print("Humidity = ");
  Serial.println(humi);


  // After each loop clear the screen to write new data
  lcd.clear();


  // The cursors define in which line to write, and what!
  lcd.setCursor(1,0);
  lcd.print("Temp = ");
  lcd.print(temp);

  lcd.setCursor(1,1);
  lcd.print("Humi = ");
  lcd.print(humi);


 // Minimum delay of 2 seconds is needed, but 3 seconds will suffice, since
 // Temp and Humi don't change that rapidly anyway ^^
  delay(3000);
}

Now verify it and upload it to the nano!

Downloads

Done and Enjoy

IMG_20210220_203050.jpg

If everything is correctly attached and uploaded you can switch to the 9V battery or keep using the laptop. As seen in the picture it should send the data to the nano which displays it on the LCD!

I hope you enjoyed it! If there are questions please ask them! I am not an expert but I might be able to solve the problem!