Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By mtaivalmaa
#76529 I am trying to use the built in olde to display data from a I2C temp sensor.

I am using Arduino IDE

If I create a sketch for just displaying data that works fine. If I create a sketch to read the temp sensor that works fine.

I can not figure out how to have the display which is I2C and the temp sensor also I2C on the same buss.. I2C scan sketches only show the display. I had to move the temp sensor to new pins and then use the wire.h begin() with the new pins to get it to even work on the esp.

If I try to have them both work in the same sketch the display doesn't work.

Why won't they both work on the same I2C buss / pins.

Code: Select all#include <SPI.h>
#include <Wire.h>
#include "SparkFunMPL3115A2.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 16
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

//Create an instance of the object
MPL3115A2 myPressure;

void setup()   {
 
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.clearDisplay();

}

void loop() {
  // put your main code here, to run repeatedly:
 
  Wire.begin(12,13);        // Join i2c bus
  myPressure.begin(); // Get sensor online
  myPressure.setModetemp(); //
  myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
  myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
 
 float temp = myPressure.readTempF();
 
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 display.clearDisplay();
 display.setTextSize(4);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 Serial.println(round(temp));
 display.print(round(temp));
 display.display();
 delay(125);

}