Skip to content

Instantly share code, notes, and snippets.

@xoan
Last active December 27, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoan/7250892 to your computer and use it in GitHub Desktop.
Save xoan/7250892 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <LiquidCrystal.h>
#define LM35DATA 5 // Analog
#define HS1101DATA 47 // TCNT5 (the only timer in Mega?)
#define HS1101POWER 7
#define DEBUG
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
int count_transitions(int ms)
{
// Counter 5
cbi(TCCR5A, WGM11);
cbi(TCCR5A, WGM10);
cbi(TCCR5B, WGM12); // WGM12::WGM10 000 - Normal mode
sbi(TCCR5B, CS12); // CS12::CS10 111 - External clock, count on rising edge.
sbi(TCCR5B, CS11);
sbi(TCCR5B, CS10);
TCNT5 = 0x0000; // note that TCNT5 is 16-bits
delay(ms);
// not sure if should turn off the counter
return(TCNT5);
}
float calc_dp(float t, float rh)
{
const float a = 17.27, b = 237.7;
float x, dp;
x = (a * t) / (b + t) + log(rh / 100.0);
dp = (b * x) / (a - x);
return(dp);
}
void setup()
{
Serial.begin(9600);
pinMode(HS1101POWER, OUTPUT);
lcd.begin(16, 2);
lcd.print("Temp: --");
lcd.setCursor (0, 1);
lcd.print("% RH: --");
}
void loop()
{
int rh_count;
float t, rh_raw, rh, dp;
// Power up the 555 / HS1101, and take a measurement. Power it down again afterwards.
digitalWrite(HS1101POWER, HIGH);
delay(500); rh_count = count_transitions(1000);
delay(500); t = (5 * analogRead(LM35DATA) * 100) / 1024.0;
digitalWrite(HS1101POWER, LOW);
rh_raw = 557.7 - 0.0759 * rh_count;
rh = (1.0 + 0.001 * (t - 25.00)) * rh_raw;
dp = calc_dp(t, rh);
#ifdef DEBUG
Serial.print("Temp: "); Serial.println(t);
Serial.print("% RH: "); Serial.print(rh);
Serial.print(" (w/o correction: "); Serial.print(rh_raw); Serial.println(")");
Serial.print("D.P.: "); Serial.println(dp);
Serial.println("--");
#endif
lcd.setCursor(6, 0); lcd.print(round(t)); lcd.print(char(223)); lcd.print("C");
lcd.setCursor(6, 1); lcd.print(round(rh));
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment