Skip to content

Instantly share code, notes, and snippets.

@embeddeduser
Last active February 22, 2020 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save embeddeduser/e7adc347397f1b3d1a9ae7e2598b4dc3 to your computer and use it in GitHub Desktop.
Save embeddeduser/e7adc347397f1b3d1a9ae7e2598b4dc3 to your computer and use it in GitHub Desktop.
Arduino Humidity Meter with DHT11 and I2C OLED display
// Written by EmbeddedUser
// Refer to blog http://www.jaimeferns.in/2016/10/how-to-build-humidity-and-temperature.html
// Youtube Video https://www.youtube.com/watch?v=g-rcEd_2I-w
// Humidity meter with DHT11, Arduino and I2c OLED display
// Now support displaying sensor error and configuring deg C or deg F
#include <DHT.h>
#include "U8glib.h"
// Display defaults to Degree F. TO use metric display,
// comment out next line to display temperature in Degree C,
//#define METRIC
#define DHTPIN 2 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
//U8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,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0
};
bool first = true;
float hum = 0.0;
float temp = 0.0;
float hIndex = 0.0;
bool dht_test(float* humPerc, float* tempF, float* heatIndex);
void setup(void)
{
Serial.begin(9600);
dht.begin();
first = true;
// assign default color value
if (u8g.getMode() == U8G_MODE_R3G3B2)
{
u8g.setColorIndex(255); // white
}
else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
{
u8g.setColorIndex(3); // max intensity
}
else if (u8g.getMode() == U8G_MODE_BW)
{
u8g.setColorIndex(1); // pixel on
}
else if (u8g.getMode() == U8G_MODE_HICOLOR)
{
u8g.setHiColorByRGB(255, 255, 255);
}
// picture loop
u8g.firstPage();
do
{
u8g.drawBitmapP(0, 0, 16, 64, logo);
}
while (u8g.nextPage());
dht_test(&hum, &temp, &hIndex);
}
void HumMeter(float* humPerc, float* temp, float* heatIndex)
{
u8g.setFont(u8g_font_fub11);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(4, 0, "Hum %");
u8g.setPrintPos(80, 0);
u8g.print(*humPerc);
#ifdef METRIC
// if metric system, display Celsius
u8g.drawStr(4, 20, "Temp C");
#else
//display Farenheit
u8g.drawStr(4, 20, "Temp F");
#endif
u8g.setPrintPos(80, 20);
u8g.print(*temp);
u8g.drawStr(4, 40, "Heat Ind");
u8g.setPrintPos(80, 40);
u8g.print(*heatIndex);
}
void loop(void)
{
bool result = dht_test(&hum, &temp, &hIndex);
if (first)
{
// skip displaying readings first time, as its stale data.
first = false;
}
else
{
if(result == false)
{
u8g.firstPage();
do
{
// sensor error
u8g.setFont(u8g_font_fub11);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(10, 30, "Sensor Error");
}
while (u8g.nextPage());
}
else
{
u8g.firstPage();
do
{
HumMeter(&hum, &temp, &hIndex);
}
while (u8g.nextPage());
}
}
}
bool dht_test(float* humPerc, float* temp, float* heatIndex)
{
// Wait a few seconds between measurements.
delay(2000);
*humPerc = 0;
*temp = 0;
*heatIndex = 0;
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Failed to read from DHT sensor!");
return false;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
*humPerc = h;
#ifdef METRIC
// metric system, load degree celsius
*temp = t;
*heatIndex = hic;
#else
*temp = f;
*heatIndex = hif;
#endif
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment