Sunday, October 23, 2016

How to build Humidity and temperature meter using DHT11 and OLED Display

Arduino Light Meter with photo resistor and OLED display

https://www.youtube.com/watch?v=g-rcEd_2I-w
This video show how to build a humidity meter using a DHT11 humidity sensor, I2C OLED display and Arduino Uno.

 A 128X64 I2C Oled ssd1306 display is used to display the humidity, temperature and heat Index.

I used an Arduino UNO 3.3V clone which uses Atmega328P. DHT11 humidity sensors are cheap and easily available on Ebay, Bangood, Sparkfun etc.

DHT22 humidity sensor can be used in place of DHT11 for more accuracy.

Library used U8Glib: Installed directly from the Library Manager in Arduino IDE.

Connect the humidity sensor pin 2 to D2 pin of Arduino. Also connect an 10K resistor between D2 and 3.3V pin of Arduino. The OLED display VCC is connected to 3.3V of Arduino. GND to GND of Arduino. SDA of OLED to SDA or Arduino, SCL of OLED to SCL of Arduino.

Humidity Meter code: Download/copy in the box below and save with .ino extension.
https://gist.github.com/embeddeduser/e7adc347397f1b3d1a9ae7e2598b4dc3
// 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
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
const uint8_t logo[] PROGMEM =
{
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, 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, 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, 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, 0x0, 0x0, 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0, 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, 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,
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;
}

Arduino Light Meter with photo resistor and OLED display

Arduino Light Meter with photo resistor and OLED display


This video show how to make an Arduino Light Meter using an Photoresistor or Light depended resistor(LDR). A 128X64 i2c Oled ssd1306 display is used to display the light intensity in percentage numeral and bar. I used an Arduino UNO 3.3V clone which uses Atmega328P.
Photo resistors are cheap and easily available on Ebay, Bangood, Sparkfun etc. Library used U8Glib: Installed directly from the Library Manager in Arduino IDE. Connect the photoresistor or LDR between 3.3V and A0 analog input pin of Arduino. Also connect an 12K or 10K resistor between A0 and ground pin of Arduino. The OLED display VCC is connected to 3.3V of Arduino. Gnd to Gnd of Arduino. SDA of OLED to SDA or Arduino, SCL of OLED to SCL of Arduino.



Light Meter code: Arduino Code download/copy the code in the box below to a file and save with .ino extension.
https://gist.github.com/embeddeduser/feab7144aceeabb6a9aa4e5862b92c75

// Light meter
// 12K resistor connected between A0 pin and GND
// Photoresistor connected between A0 and VCC 3.3V
#include "U8glib.h"
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
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
int sensorValue = 0;
unsigned int curOutputValue = 0;
unsigned int lastOutputValue;
void setup(void)
{
Serial.begin(9600);
lastOutputValue = ~curOutputValue;
// 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);
}
}
void LightMeter(uint16_t percentLight)
{
u8g.setFont(u8g_font_fub17);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(4, 0, "Light %");
u8g.drawStr(4, 20, "Meter");
u8g.drawFrame(4, 40, 122, 24);
u8g.drawBox(8, 44, (114 * percentLight / 100), 16);
//
if (percentLight > 99)
{
u8g.setPrintPos(90, 15);
}
else
{
u8g.setPrintPos(100, 15);
}
u8g.print(percentLight);
}
void loop(void)
{
sensorValue = analogRead(A0);
Serial.print("input :");
Serial.println(sensorValue);
// map input values between 0 t0 100%
curOutputValue = map(sensorValue, 0, 690, 0, 100);
if (curOutputValue > 100)
{
curOutputValue = 100;
}
if (lastOutputValue != curOutputValue)
{
lastOutputValue = curOutputValue;
u8g.firstPage();
do
{
LightMeter(curOutputValue);
}
while (u8g.nextPage());
}
}
view raw Lightmeter.ino hosted with ❤ by GitHub



Follow me on Twitter : https://twitter.com/embedded_user
 
Previous Videos: 
How to connect 128X64 i2c Oled display to Arduino:
Nokia 5110 graphical lcd Demo using Arduino
Controlling RGB LEDS with Arduino and TLC5940

Subscribe to my channel
https://www.youtube.com/channel/UCqJym4fsMaT1CZ2i6GxWURQ

Saturday, October 22, 2016

Arduino BMP180 Barometric Pressure & Temp Sensor with I2C OLED Display

Arduino BMP180 Barometric Pressure & Temp Sensor with I2C OLED Display

This video show how to build BMP180 Barometric Pressure & Temp Sensor with I2C OLED Display and Arduino Uno. A 128X64 i2c Oled ssd1306 display is used to display the barometeric Pressure, temperature and altitude. I used an Arduino UNO 3.3V clone. BMP180 sensors are cheap and easily available on ebay, bangood, sparkfun etc.
Library used U8Glib: Installed directly from the Library Manager in Arduino IDE. Select version 1.18.0. For Some reason Version 1.19.0 update gave crc error. The OLED display VCC is connected to 3.3V of Arduino. Gnd to Gnd of Arduino. SDA of OLED to SDA or Arduino, SCL of OLED to SCL of Arduino.




Arduino Code: Copy to a file and save with .ino extension.

// BMP180 Barometric Pressure& Temp Sensor //
// SSD1306_128X64 I2C OLED
// BMP180 Sensor I2C interface
// OLED SDA to BMP SDA to Arduino UNO SDA
// OLED SCL to BMP SCL to Arduino UNO SCL
// OLED VCC to BMP VCC to Arduino UNO 3.3V
// OLED GND to BMP GND to Arduino UNO GND
// Video https://www.youtube.com/channel/UCqJym4fsMaT1CZ2i6GxWURQ
// More information at http://www.jaimeferns.in/2016/10/arduino-bmp180-barometric-pressure-temp.html
// BMP sensor code Modified by embeddedUser from Adafruit example
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include "U8glib.h"
// based on adafruit example
/***************************************************
This is an example for the BMP085 Barometric Pressure & Temp Sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
// Connect GND to Ground
// Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
// Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here
Adafruit_BMP085 bmp;
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
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
float pressure = 0.0;
float tempC = 0.0;
float altitude = 0.0;
void BmpSensorRead(float* pressure, float* tempC, float* altitude);
void DisplayPresTemp(float* pressure, float* tempC, float* altitude);
void setup(void)
{
Serial.begin(9600);
// 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);
}
for (int a = 0; a < 30; a++)
{
u8g.firstPage();
do
{
u8g.setFont(u8g_font_fub11);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(4, a, "BMP180 Sensor");
}
while (u8g.nextPage());
}
delay(3000);
if (!bmp.begin())
{
u8g.firstPage();
do
{
u8g.setFont(u8g_font_fub11);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(4, 0, "BMP085 Sensor");
u8g.drawStr(4, 20, " ERROR!");
}
while (u8g.nextPage());
Serial.println("BMP085 sensor, ERROR!");
while (1) {}
}
}
void loop(void)
{
BmpSensorRead(&pressure, &tempC, &altitude);
DisplayPresTemp(&pressure, &tempC, &altitude);
delay(1000);
}
void DisplayPresTemp(float* pressure, float* tempC, float* altitude)
{
u8g.firstPage();
do
{
u8g.setFont(u8g_font_fub11);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
u8g.drawStr(2, 0, "Pressure");
u8g.setPrintPos(75, 0);
u8g.print(*pressure);
u8g.drawStr(4, 20, "Temp C");
u8g.setPrintPos(75, 20);
u8g.print(*tempC);
u8g.drawStr(4, 40, "Altitude");
u8g.setPrintPos(75, 40);
u8g.print(*altitude);
}
while (u8g.nextPage());
}
void BmpSensorRead(float* pressure, float* tempC, float* altitude)
{
*tempC = bmp.readTemperature();
Serial.print("Temperature = ");
Serial.print(*tempC);
Serial.println(" *C");
*pressure = bmp.readPressure() / 100.0;
Serial.print("Pressure = ");
Serial.print(*pressure / 100.0);
Serial.println(" hPa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
*altitude = bmp.readAltitude();
Serial.print("Altitude = ");
Serial.print(*altitude);
Serial.println(" meters");
}


Follow me on Twitter : https://twitter.com/embedded_user
Other Videos:
Subscribe to my channel