How to use many different sensors that require the SDA and SCL pins on my Arduino Mega 2560 Pro Mini?

Hey everyone, I hope you all are doing great! I am working on a project that overall requires a bunch of sensors. The problem I am facing right now is that many of these sensors require the SDA and SCL pins of my Arduino Mega 2560 Pro Mini, but there is only one SDA and one SCL pin. I am using two MPU6050's, but I think I can connect both sensors to the same pin just by changing some code. The problem is that I am also using two ZTZ GY-68 BMP280 Digital Barometers and one TOF400C-VL53L1X (distance sensor) which also requires to be connected to the SDA and SCL pins. I am using other sensors such as some DHT11's or voltage sensors, but those can be connected to digital or analog (depends). I'm wondering if there is a way I can read the data from the five sensors that do require the connection to SDA and SCL. If it can be done, is it easy? I am a beginner. Thank you so much for helping!!!!

  • The SDA and SCL pins are shared, the pull-up resistors might need adjusting.

  • Your devices will have an I2C address it responds to.

If you cannot change/set the addresses, things get complicated.

So basically, I can connect al the sensors to the same SDA and SCL pins?

  • Yes.
    As long as the devices have unique I2C addresses and the pull-up resistors are properly shaping the I2C signals.

  • For I2C information, red this:

The I2C protocol ( pronounced "eye squared Cee") is a "bus", and like a bus it can carry many different passengers, or devices. Also like a bus each passenger or device must have its own unique seat, or as we say address.

Each address is fixed by the manufacturer and can be found in the data sheet for that device. Often a device will have one pin, or more, to allow you to make a small variation on the address number. If it has then you can use a bit of a software trick so that many devices can appear to use the same address without interfering with each other.

Over all the limit is 126 devices although some of these are reserved and are recommended not to be used.
See this for details:-

Note that although people do call it "eye two see", it is only because they don't know how to pronounce it correctly. This is because the correct pronunciation is actually a geek joke derived from the true name IIC (intra integrated circuit) which if you take it as an equation simplifies to "I squared C".

1 Like

if you need to extend the I2C bus have a look at I2C Bus Buffer

I see that each address is fixed, does that mean all MPU6050's have the same address? So it would not be possible to connect those two together? I saw that instead of connecting power to VCC for the second sensor I can connect it to ADO (I2C Address Select), so it would be switched to another fixed address?

If devices have an address select pin, then you can set one of them to the address you want to talk to and the others to a different address. So you need a digital pin for the address select pin of each device, with the SCL/SDA pins of all devices in common.
If you also need level shifting, then a TCA9548A I2C selector might be a better solution.
Leo..

The address is fixed, right? But selected/established on the code?

I think you just saved my entire project, I will definitely give it a try!

Remember what I said about level shifting. The Mega is a 5volt-logic device.
Most (not all) MPU6050 boards are 3.3volt-logic only (but VCC powered with 5volt).
Most boards don't have built-in level shifters (a tiny chip with 6 pins).
And if they do, then the address pin is likely not 5volt tolerant.
The TCA9548A is the safe way.
Leo..

I haven't seen your device (or "board").
I've seen an example with an "AD0".

I'm using the Mega 2560 PRO MINI, ATmega2560-16AU

5volt-logic.
Use one or more I2C switchers.
Or switch to a 3.3volt-logic board (with enough pins).

The answers could still change, depending on your sensor boards.
More expensive (Adafruit) boards usually have level shifters included, others (cheaper) might not.
What you pay is usually what you get.
Leo..

how does the logic changes based on the voltage? How easy is to use the level shifters? As I said, I am a complete beginner.

If you send 3.3volt signals to a device that expects 5volt signals, then data might not be recognised.
If you send 5volt signals to a device that expects 3.3volt signals, then you might damage it.
We are unfortunately in a transition phase where classic 5volt and newer 3.3volt processors exist.

A level converter is a small board that converts between the two logic levels.
The 8-channel I2C switch that I linked to already has those level converters built-in.

Post links to the boards you actually have. There are many different ones.
Leo..

Yes.

No they don't.
Well not in the I2C mode that is, with the SPI mode then each device can have a data select line connected directly to each chip.

No.
Section 10 of the data sheet says it switches to an auxiliary bus. This is not what you said.

How many MPU6050 do you want to read?
You might need one of these:-

It is absoloutly vital that you have level shifters on the I2C output of the MEGA, otherwise you will fry your sensor.

Are you sure about that? Some boards have an AD0 pin, others have an address select solder pad on the back. And this is from the MPU6050 datasheet:

"The LSB bit of the 7 bit address is determined by the logic level on pin AD0. This allows two MPU-60X0s to be connected to the same I2C bus."

The 6050 doesn't seem to have SPI mode.

Hard to give advice when we don't know exactly what OP has or wants to do.
Leo..

I did read that, but when you get to section 10.3 the diagram seems to show something different.