r/ArduinoHelp • u/moonrose_07 • 1d ago
2 sensors on the same bus
I am working of an iot project for my diploma I have a max30102 and an mpu6050 Al says I can connect them on the same I2C bus and the code will be simpler but I am a beginner at coding and this project is already haunting my nights enough so any advice from professionals will coding actually be simpler and any tips on how the code will look like and frankly I don't trust AI nor do i want to fully rely on it for code
My max is connected the SDA and SCL of the esp but my mpu is connect to 2 and 15
Any advice on this will be very helpful
1
Upvotes
2
u/gbatx 1d ago
You can have multiple devices on a single i2c bus, they just each need unique addresses. Have you read the i2c specification docs?
1
u/InevitablyCyclic 1d ago
I2C is designed to allow lots of devices on the same two wires. The requirements are that 1) they all have unique addresses (most parts have jumper options to select a couple of options. I'd hope the AI would flag this if it was a potential issue, you can always check manually by looking at the driver code). 2) you don't put so much load on the bus it takes too long to transition (not an issue with 2 things unless you have some really long cables) and 3) you don't try to access them at the same time.
That last one may or may not be handled by the drivers/libraries, it depends how they are written but is normally best to handle by structuring your code so that it never happens. Unless you are using interrupts or DMA this is not normally going to be a problem.
Assuming you meet these requirements you can connect them both to the same pins and it will just work.
1
u/creativejoe4 1d ago
Read the datasheets. It will tell you everything you need to know. You can put multiple i2c slaves on the same bus if the datasheets say it will work/supported, they should but I have seen some pretty strange stuff in the past, so you should double check anyway. The datasheet will also have the addresses you need.
I can't give much advice on the code since I have no clue what your doing with it. But the code will be pretty easy so don't worry about that, you can find a tutorial for whatever you will need, just copy and paste, then tweak it. You could also just make a detailed document about your project, give a hardware description, add requirements and acceptance criteria and tell AI to make the code for you and to explain what it did and how it works, connect your hardware, and let the AI flash and debug the project until you are happy with it.