/**************************************************************************/ /* Modified by annawb This is an Arduino library for SHT21 & HTU21D Digital Humidity & Temperature Sensor Written by enjoyneering79 https://github.com/enjoyneering/HTU21D Modified by luetzel for use with SoftwareI2CMaster https://github.com/todbot/SoftI2CMaster These sensor uses I2C to communicate, 2 pins are required to interface Connect HTU21D to pins : SDA SCL Uno, Redboard, Pro: A4 A5 Mega2560, Due: 20 21 Leonardo: 2 3 BSD license, all text above must be included in any redistribution */ /**************************************************************************/ #include "SoftI2CMaster.h" #include "HTU21D_SoftI2C.h" /* HTU21D(resolution) resolution: HTU21D_RES_RH12_TEMP14 - RH: 12Bit, measuring time 16ms. Temperature: 14Bit, measuring time 50ms HTU21D_RES_RH8_TEMP12 - RH: 8Bit, measuring time 8ms. Temperature: 12Bit, measuring time 25ms HTU21D_RES_RH10_TEMP13 - RH: 10Bit, measuring time 5ms. Temperature: 13Bit, measuring time 13ms. HTU21D_RES_RH11_TEMP11 - RH: 11Bit, measuring time 3ms. Temperature: 11Bit, measuring time 7ms. DEFAULT HTU21D(HTU21D_RES_RH12_TEMP14) */ //sda = 4; scl = 6 for ATTiny84 //(0, 2) for ATtiny85 #include "SoftI2CMaster.h" #include "HTU21D_SoftI2C.h" // constants won't change. They're used here to set pin numbers: const int fanPin = 2; // actual fan = 2, LED = 7 int sdaPin = 4; int sclPin = 6; // variables will change: volatile int humidity = 0; // variable for reading the pushbutton status SoftI2CMaster i2c = SoftI2CMaster( sclPin, sdaPin, 0 ); HTU21D_SoftI2C myHTU21D = HTU21D_SoftI2C(&i2c); void setup() { Serial.begin(9600); pinMode(fanPin, OUTPUT); } void loop() { requestEvent(); delay(300); //or works with 100 humidity = myHTU21D.readHumidity(); if (humidity > 90) { digitalWrite(fanPin, LOW); } else { digitalWrite(fanPin, HIGH); } } void requestEvent() { Serial.print(F("{\"H\":")); Serial.print(myHTU21D.readHumidity()); Serial.print(F(", \"T\":")); Serial.print(myHTU21D.readTemperature()); Serial.print(F(", \"D\":1")); Serial.println(F("}")); }