YENFU CHEN
Published

Temp/Humidity LCD Box

Arduino + LCD + DHT11 sensor + Lego = Awesome temp/humidity display

IntermediateFull instructions provided5 hours7,803
Temp/Humidity LCD Box

Things used in this project

Story

Read more

Schematics

Arduino + LCD + DHT11 Sensor

Code

Temp/Humidity Sensor with LCD

C/C++
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht11.h>

LiquidCrystal_I2C __flagI2CLCD( 0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
dht11 __flagDHT11;
unsigned long __lastDHT11Time = 0;

int __getDHT11Temp(int dataPin) {
  if((millis() - __lastDHT11Time <= 150) && __lastDHT11Time > 0)
    return __flagDHT11.temperature;
  if(__flagDHT11.read(dataPin) == DHTLIB_OK) {
    __lastDHT11Time = millis();
    return __flagDHT11.temperature;
  } else
    return -1;
}

String __rightPaddingStr(String content, int width) {
  int len = content.length();
  for(int i = 0;i < (width - len);i++)
    content += " ";
  return content;
}

int __getDHT11Humi(int dataPin) {
  if((millis() - __lastDHT11Time <= 150) && __lastDHT11Time > 0)
    return __flagDHT11.humidity;
  if(__flagDHT11.read(dataPin) == DHTLIB_OK) {
    __lastDHT11Time = millis();
    return __flagDHT11.humidity;
  } else
    return -1;
}


void setup() {
  __flagI2CLCD.begin(16,2);
}

void loop() {
  __flagI2CLCD.setCursor(0, 0);
  __flagI2CLCD.print(__rightPaddingStr(String((String(__getDHT11Temp(2)) + String(char(223)) + String(u8"C"))), 16));
  __flagI2CLCD.setCursor(0, 1);
  __flagI2CLCD.print(__rightPaddingStr(String((String(__getDHT11Humi(2)) + String(u8"%"))), 16));
  delay(2000);

}

Credits

YENFU CHEN

YENFU CHEN

2 projects • 0 followers
A creative technologist, an instructor of digital communication and also an interactive product designer

Comments