Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created May 24, 2018 22:03
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 ShawnHymel/761995a0974865212eb34fcd0bc13a9f to your computer and use it in GitHub Desktop.
Save ShawnHymel/761995a0974865212eb34fcd0bc13a9f to your computer and use it in GitHub Desktop.
Differential I2C Demo
/*
* 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