Multiple I2C Connections (Accelerometer and RTC) to Arduino Nano

Hi All,

I'm currently trying to make a data logger that logs timestamp, acc_x, acc_y, acc_z to an SD card.

I am using:

I started by logging the acceleration data to the SD card and had no problems. Everything worked as expected.

However, when I connect the RTC to my Arduino, the accelerometer starts outputting all zeros.

When I run the following code to confirm my devices don't share an I2C address

#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial); // Wait for Serial to be ready
  Serial.println("\nI2C Scanner");
}

void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for (address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      nDevices++;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }

  delay(5000); // Wait 5 seconds before scanning again
}

I get the following output

Scanning...
I2C device found at address 0x57  !
I2C device found at address 0x68  !
done

When I physically remove the RTC, I am left with

Scanning...
I2C device found at address 0x68  !
done

But interestingly, even if I remove only the accelerometer, I get both addresses which I was very surprised to see.

Any help/guidance is appreciated

upload a schematic showing your wiring and power supplies?
could be the nano cannot supply sufficient power for all the devices

If I recall correctly, both the DS3231 and MPU6050 use I2C address 0x68. The 0x57 is from the EEPROM on the RTC module. You'll need to change the address on the MPU6050.

2 Likes

Thanks @van_der_decken -- I think this is on the right track!

I googled how to do this and found this Stack Overflow Forum that led me to connect the AD0 and VCC pins of the MPU6050.

Rerunning my sample RTC code

#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  while (!Serial); // Wait for serial port to be available

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  // Uncomment the following line to set the date and time
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  // Or set it manually like this:
  rtc.adjust(DateTime(2024, 7, 4, 12, 0, 0)); // Year, Month, Day, Hour, Minute, Second
}

void loop() {
  DateTime now = rtc.now();
  
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(5000); // Wait for 5 seconds
}

I get Couldn't find RTC

How should I modify it to work with the new address?

Edit: I just realized the power light on my RTC is now super dim as well

Oh oh.

Is your Nano a 5V board? And did you connect the AD0 pin to the Vcc pin on the breakout board? And is your Vcc pin hooked up to 5V on your Nano? If so, your non 5V tolerant MPU6050 (I checked the datasheet) may be history.

You were likely getting away with the high value internal pullups enabled by the Wire library on the SDA and SCL pins, and may have even gotten away with the 4.7K pullups on the RTC module but a direct connection to 5V on one of the MPU6050s inputs was likely more than it could take.

And if you had a CR3032 battery installed on the RTC module, did you defang the charging circuit beforehand?

Welcome to the forum.

You have a combination of 3.3V and 5V modules.

Arduino Nano is a 5V board.

The SD card module has a level shifter. That is good. It translates the 5V signals from the Nano to 3.3V signal for the SD card.

The MPU-6050 is a 3.3V chip. You may not connect its AD0 pin to 5V. The MPU-6050 chip might be damaged. That module has a counterfeit MPU-6050 which is not reliable. That module has a voltage converter and there is no level shifter for the I2C bus.
Solution: Buy a good module from Adafruit that works with a 5V Nano board.

The DS3231 chip itself can run at 3.3V and 5V, but that module is not reliable. If you put a battery in it (rechargeable or not), then it gets overcharged at 5V. The DS3231 on that module is a counterfeit or a factory reject.
Solution: remove the diode and use a normal battery. Or buy a DS3231 module from Adafruit.

While I was writing this, van_der_decken already wrote the same.

Ah I see thanks @Koepel @van_der_decken

Can you guys let me know how this plan sounds?

I'll use the same Arduino Nano I currently am on but swap out the accelerometer and RTC for these units.

  • DS321
  • MMA8451. (I tried to find a breakout board that also uses the MPU-6050 chip but couldn't find one. If you happen to know of one, please let me know :slight_smile:

Once I have these, I'll use a breadboard to set up my prototype with the following connections:

Accelerometer to Arduino Nano
3V to 5V (VIN to 5V is the proper connection per @Koepel)
SDA to A4
SCL to A5
GND to GND

I'm unsure what the VIN, I1, and I2 pins do on the accelerometer. Is it safe to assume I can leave them without any extra connections?

RTC to Arduino Nano
VIN to 5V
SDA to A4
SCL to A5
GND to GND

If this works, great.

If not, and I have reason to suspect the I2C address of the 2 units happens to be the same, how should I proceed?

Thanks again for your guys' help

With your board and your modules, you have a 5V Arduino board with a 5V I2C bus.

The Adafruit DS3231 is just the DS3231 chip which works at 3.3V and 5V. Apply 5V from the Arduino to VIN (as you wrote). Everything is okay.

The MMA8451 Adafruit module has a voltage regulator and I2C level shifters on the board.
Apply 5V from the Arduino board to VIN ("VIN" stands for Voltage INput).
:exclamation: If you apply 5V to the 3Vo pin, then the sensor is damaged :exclamation: Don't use the 3Vo pin.
The I2C address of the MMA8451 is 0x1D or 0x1C. There is no conflict with others.
The "I1" and "I2" are interrupt outputs from the sensor. Don't use them, because those pins are directly connected to a 3.3V sensor.

If you add more things, such as a OLED to the I2C bus, then all the trouble might start again. Only Adafruit makes OLED modules that are compatible with a 5V system.
If you have enough of all this 5V and 3.3V business and buy a Raspberry Pi Pico and make a 3.3V system, then the voltage regulators and the I2C level shifters on the Adafruit modules makes the signals of the I2C bus weaker.

1 Like

Thanks so much @Koepel for a great explanation

How did you find/know the I2C addresses are unique for these two chips? I definitely want to be able to figure it out for myself in the future.

Go to the Adafruit product: https://www.adafruit.com/product/2019
Click there on the link to the tutorial: https://learn.adafruit.com/adafruit-mma8451-accelerometer-breakout/
Then read which library to install in the Arduino IDE.
The Library Manager of the Arduino IDE has a link to the Github repository.

Just kidding :rofl: use Google to search for: github adafruit mma8451
Look in the .h file: https://github.com/adafruit/Adafruit_MMA8451_Library/blob/master/Adafruit_MMA8451.h#L25

The real source for the I2C address is the datasheet of the MMA8451.
This is the manufacturer's page of the MMA8451: https://www.nxp.com/products/sensors/accelerometers/2g-4g-8g-low-g-14-bit-digital-accelerometer:MMA8451Q
In the datasheet is this:


But some manufacturer's show the I2C address as 8-bits, and some as shifted 7-bits.

Some of us happen to know that the MPU-6050 has 0x68 and 0x69 and the DS3231 has 0x68. If you don't know, then you have to check that for every module. The easiest way is to ask us :wink:

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