Created
May 24, 2018 22:03
-
-
Save ShawnHymel/761995a0974865212eb34fcd0bc13a9f to your computer and use it in GitHub Desktop.
Differential I2C Demo
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
/* | |
* Differential I2C Demo | |
* Author: Shawn Hymel (SparkFun Electronics) | |
* Date: May 24, 2018 | |
* | |
* Connect Qwiic OLED and Qwiic Environmental sensor to Arduino. | |
* Demo will read and display temperature and humidity data. Try | |
* adding 2x Differential I2C boards in between the Arduino and | |
* Environmental sensor to place the sensor at some distance up to | |
* 100 feet from the Arduino. | |
* | |
* This sketch was written by SparkFun Electronics, with lots of | |
* help from the Arduino community. This code is completely free | |
* for any use. | |
*/ | |
#include <Wire.h> | |
#include <SFE_MicroOLED.h> // https://github.com/sparkfun/SparkFun_Micro_OLED_Arduino_Library | |
#include <SparkFunBME280.h> // https://github.com/sparkfun/SparkFun_BME280_Arduino_Library | |
// Pins | |
const int PIN_RESET = 9; | |
const int DC_JUMPER = 1; | |
// Globals | |
MicroOLED oled(PIN_RESET, DC_JUMPER); | |
BME280 env_sensor; | |
void setup() { | |
// Initialize OLED | |
delay(100); | |
oled.begin(); | |
oled.clear(ALL); | |
oled.display(); | |
// Initialize BME280 | |
if ( env_sensor.beginI2C() == false ) { | |
oledPrint("I2C Error", 0); | |
while(1); | |
} | |
// Say hi | |
oledPrint("sup", 1); | |
delay(2000); | |
} | |
void loop() { | |
// Get temperature and humidity | |
float temp = env_sensor.readTempC(); | |
float humd = env_sensor.readFloatHumidity(); | |
// Clear OLED | |
oled.clear(PAGE); | |
oled.setFontType(1); | |
oled.setCursor(0, 0); | |
// Print text | |
oled.print(temp, 1); | |
oled.println(" C"); | |
oled.print(humd, 0); | |
oled.println("%"); | |
// Update OLED | |
oled.display(); | |
// Wait for refresh | |
delay(500); | |
} | |
void oledPrint(String text, int font) { | |
oled.clear(PAGE); | |
oled.setFontType(font); | |
oled.setCursor(0, 0); | |
oled.print(text); | |
oled.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment