Hello,
My latest project is a precision thermometer using a TMP117 sensor on a Sparkfun breakout board. Other components are an Arduino Pro-Micro (3.3v 8MHz) ,a small Winstar OLED display (WEA012832FLPP), two 10K pullup resistors, and a battery supply. The KiCad schematic is as below. All devices operate at 3.3v.
The TMP117 sensor address is 0x48, and the display SSD1306 address is 0x3C.
I have a problem with the I2C communication, which I think might be an issue of bus contention. I have written the following sketch to demonstrate the problem.
/* Test sketch to demonstrate I2C problem
--------------------------------------
With only the TMP117 temperature sensor connected -- works OK
Temperature is printed in serial monitor
With only the display module connected -- works OK
Alternating "Hello", "Goodbye" are displayed.
With both connected -- the display does not work
Display is either frozen or garbled.
TMP117 sensor address is 0x48
Display SSD1306 address is 0x3C
---------------------------------------------------------------*/
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include <SparkFun_TMP117.h> // Used to send and recieve specific information from sensor
#include <SparkFun_TMP117_Registers.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
TMP117 sensor; // Initalize sensor The default address of the sensor is 0x48
bool flag = 1;
// -----------------------------------------------------------------------------
void setup() {
//==========
delay(4000);
Wire.begin();
Serial.begin(115200); // Start serial communication
Wire.setClock(400000); // Set clock speed
// // Display I2C address of display is 0x3C
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) Serial.println("SSD1306 allocation failed");
// Above message not printed altho display is disconnected??
// Check if the sensor will correctly self-identify
if (sensor.begin() == true) Serial.println("Sensor Begin");
else Serial.println("Sensor failed to setup");
display.setFont(&FreeMonoBold12pt7b);
display.setTextSize(1);
display.setTextColor(WHITE);
}
// ------------------------------------------------------------------------
void loop() {
//=========
if (sensor.dataReady() == true) {
float tempC = sensor.readTempC();
Serial.println();
Serial.print("Temperature in Celsius: ");
Serial.println(tempC);
display.clearDisplay();
display.setCursor(20, 20);
if (flag) display.print("Hello");
else display.print("Goodbye");
display.display();
flag = !flag; // Toggle
// display.clearDisplay();
// display.setCursor(0,20);
// display.print(tempC);
// display.print(" c");
// display.display();
delay(1000);
}
//delay(1000);
} // End of loop
// =============================== End of File ==========================================
With only the TMP117 temperature sensor connected it works properly:
Temperature is printed in the serial monitor.
With only the display module connected it also works properly:
Alternating "Hello", "Goodbye" are displayed on the OLED.
But with both connected the display does not work:
Temperature is printed correctly in the serial monitor,
but the display is either frozen or garbled.
Above behaviour is the same whether power from USB or from batteries, except there is of course no serial monitor with batteries.
I've searched the forum, and am aware that there are many posts about I2C, but I havn't found anything that helps. I've also had a look at all the Wire library functions, but cannot fathom how to use them to maybe fix this problem.
Obviously, in the "real" project I will use the OLED to display the temperature, not "Hello", "Goodbye". That is just for this demo.
Can you help? Thank you, Ken.
