-
-
Save yarogniew/9dfe3e5e0aa06d16b57f69c3e56e4d90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************** | |
This is an example for the SHT31-D Humidity & Temp Sensor | |
Designed specifically to work with the SHT31-D sensor from Adafruit | |
----> https://www.adafruit.com/products/2857 | |
These sensors use I2C to communicate, 2 pins are required to | |
interface | |
****************************************************/ | |
#include <Arduino.h> | |
#include <Wire.h> | |
#include "Adafruit_SHT31.h" | |
#include <Adafruit_SSD1306.h> | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
Adafruit_SHT31 sht31 = Adafruit_SHT31(); | |
void setup() { | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
display.clearDisplay(); | |
display.display(); | |
while (!Serial) | |
delay(10); // will pause Zero, Leonardo, etc until serial console opens | |
display.println("SHT31 test"); | |
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr | |
display.println("Couldn't find SHT31"); | |
while (1) delay(1); | |
} | |
} | |
void loop() { | |
display.clearDisplay(); | |
float t = sht31.readTemperature(); | |
float h = sht31.readHumidity(); | |
display.setTextColor(WHITE); | |
display.setTextSize(2); | |
display.setCursor(0, 1); | |
if (! isnan(t)) { // check if 'is not a number' | |
display.println("Temp *C"); | |
display.print(" "); | |
display.println(t); | |
} else { | |
display.println("Failed to read temperature"); | |
} | |
if (! isnan(h)) { // check if 'is not a number' | |
display.println("Hum. %"); | |
display.print(" "); | |
display.println(h); | |
} else { | |
display.println("Failed to read humidity"); | |
} | |
display.display(); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment