Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Fredyy
#76090 Hey,

've been struggeling allday to get I²C and SPI working on the same time on my Wemos D1 Mini Pro, i want to use a BME280 + the Wemos D1 Mini 1,4' TFT Shield.

If i comment one or the other out, the sensor or the display work fine. But as soon as both are active there must be some kind of races condition, sometimes the display works but the sensor only returns NaN or the display freezes and the sensor returns valid data.
Sometimes the display works a few seconds, while the Sensor return garbage and then the suddenly switch, e.g display freezes and sensor returns valid data.

When the display works it seems like i2c aka Wire.h crashes and cant even i do a i2c scan, when i tried it while debugging and get errors on every address.

I hope i've just overlooked something really basic and didn't run in a library/hardware limitation.

Thank you for your help and enjoy the rest of the weekend,
Fredyy

Code: Select all#include <BME280I2C.h>
#include <Wire.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>

#define TFT_CS     D4
#define TFT_RST    -1  // you can also connect this to the Arduino reset
#define TFT_DC     D3

#define I2C_SDL    D1
#define I2C_SDA    D2

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);
BME280I2C bme;

void setup(void) {

  Serial.begin(9600); 
  while(!Serial) {} // Wait

  Wire.setClock(400000L);
  Wire.begin(I2C_SDL, I2C_SDA);

  while(!bme.begin()){
    Serial.println("Could not find BME280 sensor!");
    delay(250);
  }

  switch(bme.chipModel())
  {
    case BME280::ChipModel_BME280:
      Serial.println("Found BME280 sensor! Success.");
      break;
    case BME280::ChipModel_BMP280:
      Serial.println("Found BMP280 sensor! No Humidity available.");
      break;
    default:
      Serial.println("Found UNKNOWN sensor! Error!");
  }

  tft.initR(INITR_144GREENTAB);
  tft.setTextWrap(false); // Allow text to run off right edge
  tft.fillScreen(ST7735_BLACK);

}

void loop(void) {
  tft.fillScreen(ST7735_BLACK);
  tft.setCursor(0, 0);
  tft.setTextColor(ST7735_BLUE);
  tft.setTextSize(2);
  tft.println("Hello World");
  tft.setRotation(tft.getRotation()+1);
  delay(1000);

  printBME280Data(&Serial);
  delay(3000);
}

void printBME280Data(Stream* client) {
   float temp(NAN), hum(NAN), pres(NAN);

   BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
   BME280::PresUnit presUnit(BME280::PresUnit_Pa);

   bme.read(pres, temp, hum, tempUnit, presUnit);

   client->print("Temp: ");
   client->print(temp);
   client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
   client->print("\t\tHumidity: ");
   client->print(hum);
   client->print("% RH");
   client->print("\t\tPressure: ");
   client->print(pres);
   client->println(" Pa");

}