Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bboyho/63d29f37a141e8d8aa556c03fdbc7292 to your computer and use it in GitHub Desktop.
Save bboyho/63d29f37a141e8d8aa556c03fdbc7292 to your computer and use it in GitHub Desktop.
/*
HTU21D Humidity Sensor Example Code
By: Nathan Seidle
SparkFun Electronics
Date: September 15th, 2013
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Uses the HTU21D library to display the current humidity and temperature
Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.
Hardware Connections (Breakoutboard to Arduino):
-VCC = 3.3V
-GND = GND
-SDA = A4 (use inline 330 ohm resistor if your board is 5V)
-SCL = A5 (use inline 330 ohm resistor if your board is 5V)
*/
#include <Wire.h>
#include "SparkFunHTU21D.h" // include HTU21D library
#include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library
#define PIN_RESET 7
#define DC_JUMPER 1
MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration
//Create an instance of the object
HTU21D myHumidity;
void setup()
{
Serial.begin(9600);
Serial.println("HTU21D Example!");
delay(100);
myHumidity.begin();
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.display(); // Display what's in the buffer (splashscreen)
delay(1000); // Delay 1000 ms
oled.clear(PAGE); // Clear the buffer.
}
void loop()
{
float humd = myHumidity.readHumidity();
float temp_C = myHumidity.readTemperature();
float temp_F = temp_C * (9.0 / 5.0) + 32.0;
oled.clear(PAGE); // Clear the display
oled.setCursor(0, 0); // Set cursor to top-left
oled.setFontType(0); // Smallest font
oled.print(" Relative"); // Print
oled.setCursor(0, 8); // Set cursor to top-left
oled.print(" Humidity"); // Print
oled.setCursor(0, 19); // Set cursor to middle-ish
oled.print(temp_F); // Print temp, assuming that it is within room temp in tens
oled.print(" degF"); // Print
oled.setCursor(0, 30); // Set cursor to middle-ish
oled.print(temp_C); // Print temp, assuming that it is within room temp in tens
oled.print(" degC"); // Print
oled.setCursor(0, 41); // Set cursor to middle-ish
oled.print(" "); // Print
oled.print(humd); // Print temp, assuming that it is within room temp in tens
oled.print(" %"); // Print
oled.setFontType(0); // Smallest font
oled.display();
Serial.print("Time:");
Serial.print(millis());
Serial.print(" Temperature:");
Serial.print(temp_C, 1);
Serial.print("C ");
Serial.print(temp_F, 1);
Serial.print("F");
Serial.print(" Humidity:");
Serial.print(humd, 1);
Serial.print("%");
Serial.println();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment