Sie sind auf Seite 1von 11

Arduino Thermometer and Hygrometer with OLED

Display
1 1

By Muhamed Aqib

In this project, we are going to make an Arduino OLED thermometer and hygrometer. We will read the temperature and
humidity from the DHT22 sensor and then we will show the data on the OLED screen.

OLED stands for organic light emitting diode and they are available in many different sizes. The size we are going to use is
128X64 (1.3). The OLED works with the Arduino through SPI as well as I2C communication but in our project, we are
going to use the SPI communication.

Required Components
The components required for this project are as follows

Arduino Uno
Monochrome OLED 128X64

converted by Web2PDFConvert.com
DHT22 Temperature and Humidity Sensor
Connecting wires
Breadboard

Circuit Diagram
First of all, we will connect the OLED with the Arduino. The OLED can be connected to the Arduino in I2C as well as SPI.
The connections for connecting the OLED in the I2C way are easier but SPI communication is faster than the I2C. So, we
are going to connect the OLED with the Arduino using SPI. Make the connections of the OLED with the Arduino as follows:

Connect the CS pin on the OLED to pin 10 on the Arduino


Connect the DC pin on the OLED to pin 9 on the Arduino
Connect the RST pin on the OLED to pin 8 on the Arduino
Connect the D1 or CLK pin on the OLED to pin 11 on the Arduino
Connect the D0 or DIN pin on the OLED to pin 13 on the Arduino

We have connected the OLED to pins 13, 11, 10, 9, and 8 because these pins are for SPI communication. Next, connect
the DHT22 with the Arduino. The connections for DHT22 sensor with Arduino are as follows:
Connect the VCC on DHT22 to the 5V pin on the Arduino
Connect the GND on the DHT22 to the GND on the Arduino
Connect the data pin of the DHT22 to pin 7 on the Arduino

Arduino Code
converted by Web2PDFConvert.com
#include
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT sensor(DHTPIN, DHTTYPE);

U8GLIB_SH1106_128X64 oled (13, 11, 10, 9, 8);

void setup()
{
sensor.begin();
oled.firstPage();
do
{
oled.setFont(u8g_font_fur14); //setting the font size
//Printing the data on the OLED
oled.drawStr(20, 15, "Welcome");
oled.drawStr(40, 40, "To");
oled.drawStr(5, 60, "DIYHACKING");
} while( oled.nextPage() );
delay(5000);
}

void loop()
{
float h = sensor.readHumidity(); //Reading the humidity value
float t = sensor.readTemperature(); //Reading the temperature value
float fah = sensor.readTemperature(true); //Reading the temperature in Fahrenheit
if (isnan(h) || isnan(t) || isnan(fah)) { //Checking if we are receiving the values or not
Serial.println("Failed to read from DHT sensor!");
return;
}
float heat_index = sensor.computeHeatIndex(fah, h); //Calculating the heat index in Fahrenheit
float heat_indexC = sensor.convertFtoC(heat_index); //Calculating the heat index in Celsius

oled.firstPage();
do
{
oled.setFont(u8g_font_fub11); //setting the font size
//Printing the data on the OLED
oled.drawStr(0, 15, "Temp: ");
oled.drawStr(0, 40, "Hum: ");
oled.drawStr(0, 60, "Hi: ");
oled.setPrintPos(72, 15); //setting the dimensions to print the temperature
oled.print(t, 0);
oled.println("C");
oled.setPrintPos(72, 40); //setting the dimensions to print the humidity
oled.print(h, 0);
oled.println("%");
oled.setPrintPos(72, 60); //setting the dimensions to print the heat index
oled.print(heat_indexC, 0);
oled.println("%");
}
while( oled.nextPage() );
delay(2000);
}

Code Explanation

converted by Web2PDFConvert.com
First, we include the library for the DHT22 sensor and for the OLED. The U8glib library is for the OLED and it makes the
code very easy. We will show the data on the OLED by using the functions of U8glib library.

#include
#include "DHT.h"

Next, we defined the pin where we have connected the DHT22 sensor data pin and then defined the type of DHT sensor.
There are some other types of DHT sensors available in the market. After that, we initialized the pins where we have
connected the OLED.

#define DHTPIN 7
#define DHTTYPE DHT22
DHT sensor(DHTPIN, DHTTYPE);
U8GLIB_SH1106_128X64 oled(13, 11, 10, 9, 8);

In the setup function, we give the command to start receiving the values from the DHT22 sensor. Then we set the font
and printed Welcome to DIYHACKING on the OLED for 5 seconds. You can change the font size if you dont like that. You
can find different font sizes here.

sensor.begin();
oled.firstPage();
do
{
oled.setFont(u8g_font_fur14); //setting the font size
//Printing the data on the OLED
oled.drawStr(20, 15, "Welcome");
oled.drawStr(40, 40, "To");
oled.drawStr(5, 60, "DIYHACKING");
} while( oled.nextPage() );
delay(5000);
}

In the loop function, we read the values of the humidity and the temperature from the DHT22 sensor and then we
calculated the heat index using the temperature and the humidity.

float h = sensor.readHumidity(); //Reading the humidity value


float t = sensor.readTemperature(); //Reading the temperature value
float fah = sensor.readTemperature(true); //Reading the temperature in Fahrenheit
if (isnan(h) || isnan(t) || isnan(fah)) { //Checking if we are receiving the values or not
Serial.println("Failed to read from DHT sensor!");
return;
float heat_index = sensor.computeHeatIndex(fah, h); //Calculating the heat index in Fahrenheit
float heat_indexC = sensor.convertFtoC(heat_index); //Calculating the heat index in Celsius

Lastly, we again set the font size and printed the temperature, humidity, and heat index on the OLED. You can change the
font size by following the link which is discussed above and also you can set the data at different dimensions.

converted by Web2PDFConvert.com
oled.firstPage();
do
{
oled.setFont(u8g_font_fub11); //setting the font size
//Printing the data on the OLED
oled.drawStr(0, 15, "Temp: ");
oled.drawStr(0, 40, "Hum: ");
oled.drawStr(0, 60, "Hi: ");
oled.setPrintPos(72, 15); //setting the dimensions to print the temperature
oled.print(t, 0);
oled.println("C");
oled.setPrintPos(72, 40); //setting the dimensions to print the humidity
oled.print(h, 0);
oled.println("%");
oled.setPrintPos(72, 60); //setting the dimensions to print the heat index
oled.print(heat_indexC, 0);
oled.println("%");
}
while( oled.nextPage() );
delay(2000);
}

Tags: Arduino, Arduino Uno, DHT22, Humidity Sensor, I2C, OLED, SPI, Temperature Sensor

R E C O M M E N D E D P O S T S

converted by Web2PDFConvert.com
D I Y D A Q W I T H A N A R D U I N O : C A L I B R A T I N G

converted by Web2PDFConvert.com
A R D U I N O O L E D T E M P E R A T U R E D I S P L A Y W I T H

converted by Web2PDFConvert.com
D I Y D A Q : M A K E A N A R D U I N O D A T A A C Q U I S

converted by Web2PDFConvert.com
D I Y E X P L O R E R P R O B E F O R R O C K E T S W I T H D H

C O M M E N T S

Prompt July 19, 2017 Reply

I am going to make it.


Is it possible to use it in the sauna?

L E A V E A C O M M E N T

converted by Web2PDFConvert.com
LEAVE YOUR COMMENT

Name (Required)

Email (Required)

Math Captcha
5+3=

P O S T C O M M E N T

F O L L O W U S

R E C E N T P O S T S
How to Use Telegram Instant Messaging on Raspberry Pi

DIY Motion-Activated Sink: How to Make an IR-Controlled Tap

Cleaning Things Up: Noisy Signals and Proper Practice

Do-It-Yourself Insect Detector with an Electret Microphone and PIC18F1220

Connecting Raspberry Pi IoT Devices with MQTT

C A T E G O R I E S

3D Designing
Advanced
Analog
Arduino projects
Beginners
Blog

converted by Web2PDFConvert.com
Collaboration tools
Cyber Security
Drones
eBooks
Electronics hacks
ESP8266
Home Automation
Intel Edison
Intermediate
Raspberry pi projects
Resources
Robotics
Sensors
VR and AR

converted by Web2PDFConvert.com

Das könnte Ihnen auch gefallen