Using I2C OLED with I2C Accelerometer

Hi,
I have an ESP32 talking to an SC7A20 accelerometer via I2C, the same HW will succesfully talk to an SSD1306 OLED display.
The problem starts when I try to combine the two together in one sketch as neither the display or the accelerometer will work.
I changed the OLED instance from
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/5, /* data=*/4, /* reset=*/U8X8_PIN_NONE);
to
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(U8X8_PIN_NONE)
but this made no difference.
I'm assuming there is some type of conflict on the I2C bus when both are used on the I2C bus at the same time, is there a way of getting them to work together?
Here are the parts of the latest code I tried that doesn't work

#include <Wire.h>
#include <Arduino.h>
#include <U8x8lib.h>
//U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/5, /* data=*/4, /* reset=*/U8X8_PIN_NONE);  // OLEDs without Reset of the Display
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(U8X8_PIN_NONE);
#include "SparkFun_LIS2DH12.h"  //Click here to get the library: http://librarymanager/All#SparkFun_LIS2DH12
#define LIS2DH12_ID = 0x11;
SPARKFUN_LIS2DH12 accel;      //Create instance

void setup() {
  Wire.begin(4, 5);
  u8x8.begin();
  //u8x8.setBusClock(1);
  u8x8.setFlipMode(0);
  u8x8.setFont(u8x8_font_8x13B_1x2_f);
  //u8x8.setFont(u8x8_font_profont29_2x3_f);
  // initialize serial communication with computer:
  Serial.begin(115200);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB
  }

  Serial.println("SparkFun Accel Example");

  if (accel.begin() == false) {
    //Serial.println(accel.LIS2DH12_ID);
    Serial.println("Accelerometer not detected. Are you sure you did a Wire1.begin()? Freezing...");
    while (1)
      ;
  }

That is unlikely. Please post any error messages encountered.

When both are connected, run the I2C address scanner program, and verify that the correct bus addresses are detected.

Is there some confusion between the two different Wire buses (Wire1 and Wire libraries)?

That is unlikely. Please post any error messages encountered.

I get "Accelerometer not detected. Are you sure you did a Wire1.begin()? Freezing..." due to it not finding the accelerometer and stopping?

When both are connected, run the I2C address scanner program, and verify that the correct bus addresses are detected.

Ran and I get 0x19 and 0x3C

Is there some confusion between the two different Wire buses (Wire1 and Wire libraries)?

I don't think so, that's just what the text says, I only have one "Wire"

From the error message, the Sparkfun library seems to assume that the sensor is connected to Wire1. If so, that is a problem. Check the library code, and/or consider using a different library.

From the error message, the Sparkfun library seems to assume that the sensor is connected to Wire1. If so, that is a problem. Check the library code, and/or consider using a different library.

I understand why you say that but it works fine without the OLED code added.

I have no experience with that sensor or that Sparkfun library, but in principle, you should have no problem accessing those two devices on one bus.

The Sparkfun library is the primary suspect.

First question to solve is does it use SW I2C or HW I2I?
When using the same pins for both, then both should be of same kind, I expect.

If you look at the library header file you will see it only contains one variant of begin:

bool begin(uint8_t address = ACCEL_DEFAULT_ADR, TwoWire &wirePort = Wire); //Begin comm with accel at given I2C address

You use it without passing any parameters:

`if (accel.begin() == false) {`

That compiles okay because the declaration of begin in the header file contains default values which will be used if you don't specify any parameters.
I've not looked in detail at what begin does, but I didn't spot anything to make me think there would be a conflict.

That's strange. In post #8 you can see that the default value used by begin is Wire, not Wire1. I wonder why the error message assumes the user didn't initialise Wire1

The Sparkfun library is configured for two different I2C addresses, depending on an address input setting. I don't see how either of these could match up with the 0x19 address reported by the I2C scanner program.

/** I2C Device Address 8 bit format if SA0=0 -> 31 if SA0=1 -> 33 **/
#define LIS2DH12_I2C_ADD_L   0x31U
#define LIS2DH12_I2C_ADD_H   0x33U

The accelerator has two possible I2C addresses 0x19 is the default and 0x18 is the optional address pin is tied high

Initailly the sparkfun library fails to talk to the SC7A20 due to the device ID being different to the sparkfun device, it took me a while to find that problem and fix it.

The Sparkfun library you are using apparently does not detect the accelerometer.

Since you modified the library code, but forget to post it, you have some more detective work to do. Have fun!

I think it's the usual 7bit address vs 8bit address issue.
This is a scope trace I took ages ago when I first tried using I2C with Arduino. The line of text from #1 Start to #11 STOP is my annotation, not Picoscope's decode.


For the default 7 bit address of 0x19, if we multiply it by 2 we get 0x32, then presumably for some reason they assume it's a read and add 1 to get 0x33.

I don't think so.

I'm a bit confused by your reply. I can't spot my misunderstanding.
The datasheet for the LIS2DH12 contains this table:

By device ID I didn't mean the I2C address, there is a device ID in a register, the sparkfun device ID is 0x33 but the SC7A20 reports 0x11 as its ID,which is checked during the accel.begin function.
I added the define for the SC7A20 in the sketch to overide the library definition.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.