Skip to content

Instantly share code, notes, and snippets.

@annappropriate
Created March 26, 2015 20:30
Show Gist options
  • Save annappropriate/48f6891159e89adc06a4 to your computer and use it in GitHub Desktop.
Save annappropriate/48f6891159e89adc06a4 to your computer and use it in GitHub Desktop.
#include <dht.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht11 = DHT();
int HIH4030_Pin = A5;
void setup() {
lcd.init();
lcd.backlight();
dht11.attach(A0);
Serial.begin(9600);
Serial.println("Temp,DHT11_RH,HIH4030_Volt,HIH4030_RH_calc");
}
void loop() {
dht11.update();
lcd.clear();
int t = dht11.getTemperatureInt();
int h = dht11.getHumidityInt();
lcd.print("T: ");
lcd.print(t);
lcd.print(" ");
Serial.print(t);
Serial.print(",");
lcd.print("DHT11: ");
lcd.print(h);
Serial.print(h);
Serial.print(",");
lcd.setCursor(0, 1);
int hihv = analogRead(HIH4030_Pin);
// lcd.print("HIH4030 Volt: ");
// lcd.println(hihv);
Serial.print(hihv);
Serial.print(",");
int hih4030_rh = getHih4030Humidity(t);
lcd.print("HIH4030: ");
lcd.print(hih4030_rh);
Serial.println(hih4030_rh);
delay(1000);
}
float getHih4030Humidity(float degreesCelsius){
//caculate relative humidity
float supplyVolt = 5.0;
// read the value from the sensor:
int HIH4030_Value = analogRead(HIH4030_Pin);
float voltage = HIH4030_Value/1023.; // convert to voltage value
float sensorRH = 161.3 * voltage - 25.8;
float trueRH = sensorRH / (1.0546 - 0.00216 * degreesCelsius); //temperature adjustment
return trueRH;
}
@ulidtko
Copy link

ulidtko commented Mar 27, 2015

Revised

#include <LiquidCrystal.h>
#include <dht11.h>
#include <stdlib.h>

const int DHT11_pin = A1;
const int HIH4030_Pin = A0;

LiquidCrystal lcd (A2, A3, 15, 14, 16, 10);
class dht11 dht11;

void setup() {
  lcd.begin(16, 2);

  Serial.begin(9600);
  Serial.println("time,DHT_Temp,DHT_RH,HIH_Adc,HIH_sensRH,HIH_trueRH");
}

void loop() {
  /* measurement */  
  (void) dht11.read(DHT11_pin);

  int dhtT = dht11.temperature;
  int dhtH = dht11.humidity;

  int hihV = analogRead(HIH4030_Pin);

  long time = millis();

  /* formatting & output */
  static char line1[20];
  static char line2[20];
  // LCD top line
  snprintf(line1, sizeof(line1),
    "DHT:%0.2d%%  %+5.2d\xDF\x43",
    dhtH, dhtT
    );
  // LCD bottom line
  snprintf(line2, sizeof(line2),
    "V=%d %0.2d%% [%0.2d%%]",
    hihV,
    (int)calcHIH4030Humidity(hihV/1023., 25.28),
    (int)calcHIH4030Humidity(hihV/1023., dhtT)
    );

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(line2);

  // serial log
  static char serline[120];
  snprintf(serline, sizeof(serline),
    "%ld.%03ld, %+.2d, %02d, %d, %d, %d\n", 
    time/1000,
         (time % 1000),
                dhtT,
                       dhtH,
                             hihV,
                                 (int)calcHIH4030Humidity(hihV/1023., 25.28),
                                     (int)calcHIH4030Humidity(hihV/1023., dhtT)
    );
  Serial.print(serline);

  delay(500);
}

float calcHIH4030Humidity(float voltRatio, float degreesCelsius) {
  float sensorRH = 161.3 * voltRatio - 25.8;
  float trueRH = sensorRH / (1.0546 - 0.00216 * degreesCelsius);

  return trueRH;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment