Still unable to use two I2C devices in the same sketch

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mgiara
 
Posts: 18
Joined: Sat Aug 25, 2018 11:06 pm

Still unable to use two I2C devices in the same sketch

Post by mgiara »

I was previously trying to communicate with a MAX6675/MAX31855 for thermistor readings, and display them on a ST7735 display.

Using an ESP32-WROOM-32U but I don't think the uC is relevant. I use only and all ESP32's for the various projects I develop (approaching 100 now).

No matter what I tried, I was unable to use both peripherals at the same time.

I'm now trying to do nearly the same thing but with a SHTC3 instead of the MAX.

Here's my sketch:

Code: Select all

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>

#include "Adafruit_SHTC3.h"

#if defined(ARDUINO_FEATHER_ESP32) // Feather Huzzah32
  #define TFT_CS         14
  #define TFT_RST        15
  #define TFT_DC         32

#elif defined(ESP8266)
  #define TFT_CS         2
  #define TFT_RST        1                                            
  #define TFT_DC         3

#else
  // For the breakout board, you can use any 2 or 3 pins.
  // These pins will also work for the 1.8" TFT shield.
  #define TFT_CS         14
  #define TFT_RST        15 // Or set to -1 and connect to Arduino RESET pin
  #define TFT_DC         32
#endif


// OPTION 2 lets you interface the display using ANY TWO or THREE PINS,
// tradeoff being that performance is not as fast as hardware SPI above.
#define TFT_MOSI 21  // Data out
#define TFT_SCLK 22  // Clock out

// For ST7735-based displays, we will use this call
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

// OR for the ST7789-based displays, we will use this call
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

Adafruit_SHTC3 shtc3 = Adafruit_SHTC3();


float p = 3.1415926;

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

  while (!Serial)
    delay(10);     // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("SHTC3 test");
  if (! shtc3.begin()) {
    Serial.println("Couldn't find SHTC3");
    while (1) delay(1);
  }
  Serial.println("Found SHTC3 sensor");

  Serial.print(F("Hello! ST77xx TFT Test"));


  // OR use this initializer (uncomment) if using a 1.44" TFT:
  tft.initR(INITR_144GREENTAB); // Init ST7735R chip, green tab

  Serial.println(F("Initialized"));

  uint16_t time = millis();
  tft.fillScreen(ST77XX_BLACK);
  time = millis() - time;


  tft.fillScreen(ST77XX_BLACK);


  Serial.println("done");
  delay(1000);
}

int runningsecs = 0;

void loop() {
  //digitalWrite(14, LOW);
  //tftPrintTest();
  //digitalWrite(14, HIGH);

  sensors_event_t humidity, temp;
  
  shtc3.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  /*
  Serial.print("Temperature: "); Serial.print((temp.temperature * 1.8) + 32); Serial.println(" degrees F");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");

  float tempF = ((temp.temperature * 1.8) + 32);
  String tempfAsString = String(tempF);
  Serial.print("Converted temp: ");
  Serial.println(tempfAsString);*/
  

  //time = millis();
  //digitalWrite(14, LOW);
  tft.setCursor(0, 0);
  tft.setTextSize(4);
  tft.println(runningsecs);
  Serial.println(runningsecs);
  tft.println(String(temp.temperature));
  //tft.println(dstime);
  delay(1000);
  tft.fillScreen(ST77XX_BLACK);
  //digitalWrite(14, HIGH);
  runningsecs++;

}
The screen works properly when

Code: Select all

sensors_event_t humidity, temp;
  
  shtc3.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  /*
  Serial.print("Temperature: "); Serial.print((temp.temperature * 1.8) + 32); Serial.println(" degrees F");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");

  float tempF = ((temp.temperature * 1.8) + 32);
  String tempfAsString = String(tempF);
  Serial.print("Converted temp: ");
  Serial.println(tempfAsString);*/
is commented out.

Then when uncommented, the SHTC3 works properly and the screen does not update.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Still unable to use two I2C devices in the same sketch

Post by dastels »

The sensor is I2C, but the display is SPI. The connections are all separate? No shorts?

Dave

User avatar
mgiara
 
Posts: 18
Joined: Sat Aug 25, 2018 11:06 pm

Re: Still unable to use two I2C devices in the same sketch

Post by mgiara »

dastels wrote: Fri Dec 30, 2022 3:09 pm The sensor is I2C, but the display is SPI. The connections are all separate? No shorts?

Dave
Holy smokes. What an egregious oversight by me. Thank you Dave, let me give this another try and I will report back!

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Still unable to use two I2C devices in the same sketch

Post by dastels »

It happens.

Dave

User avatar
mgiara
 
Posts: 18
Joined: Sat Aug 25, 2018 11:06 pm

Re: Still unable to use two I2C devices in the same sketch

Post by mgiara »

dastels wrote: Fri Dec 30, 2022 3:09 pm The sensor is I2C, but the display is SPI. The connections are all separate? No shorts?

Dave
Just now realizing that the connections on my ST7735 say SCK and SDA, probably why I connected them to I2C on the ESP32.

User avatar
mgiara
 
Posts: 18
Joined: Sat Aug 25, 2018 11:06 pm

Re: Still unable to use two I2C devices in the same sketch

Post by mgiara »

It's working now. The silkscreen definitely has abbreviations for I2C and not SPI but I simply used SDA as MOSI and SCK as the clock line and it worked just fine.

Thank you for the help!

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Still unable to use two I2C devices in the same sketch

Post by dastels »

Have fun!

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”