I am having trouble with a project I am working on. I have a DS1307 real time clock that is connected to pin 21 and 20, scl and sda respectively, on the Arduino Mega 2560. I bought an ADXL345 from sparkfun electronics and I wrote a program to read data from it. That program works fine using I2C. When I put the code for the ADXL345 into my other program with RTC I cannot get the correct data from the ADXL345.
How do you use multiple devices using I2C? I thought I could just connect both to the same sda and scl and I would be able to read and write.
Side Note: The addresses for the sensors are different. The ADXL345 address is 0x53 and the DS1307 address is 0x68.
You should be able to use more than one I2C device. You just use the correct address when refering to each device.
If you run into a problem, there may be an issue with the total amount of resistors "pulling up" the signal lines, which
your devices have to overcome to pull the lines "down" to send data.
Or there may be a problem in your program. If you had two devices working in separate sketches, and they didn't work when
you put those sketches together, maybe you did it wrong. You can post your code and we will look at it.
I would post the code but I am not sure my adviser for the design team would want me!
Can I only use one device at a time? If the RTC is started in the setup and continues to run through out the program can I still use the other sensor? Im pretty sure with SPI you have to select and deselect your slave and can only communicate to one at a time. My understanding of I2C is you could communicate with several devices all at once?
As for how the devices are wired, the RTC has resistors connected to it but the ADXL345 does not. I just have a wire connected to the pins on the arduino to the pin on the chip. Does the resistor affect it that much?
Every I2C slave device keeps reading the bus, until its address is transmitted. That is the moment the slave acknowledges the I2C session. So you can have many slave devices.
Also using the libraries should not get in each other way.
But if you have large arrays, combining too many things, that could result in ram overflow.
Did you connect the RTC DS1307 to the 5V ?
And the ADXL345 to the 3.3V ?
And the DS1307 breakout board has pull-up resistors to 5V ?
That is a big problem. The pull-up resistors pull the I2C-bus to 5V and that could damage the ADXL345, since that chip can not deal with 5V.
A quick and dirty solution is to remove the pull-up resistors to 5V, add add 4k7 pull-up resistors to 3.3V.
A good soluation is to use a I2C level shifter.
Hey. Sorry for taking a few days to get back to you!
I do have the RTC connected to 5V and the ADXL to 3.3V.
When I test the ADXL by itself I have not been using pull-up resistors. The RTC has two pull-up resistors. They are 2.2k each. If I replace those with 4.7k resistors will that do the trick? Im new to I2C so my apologies if I am not explaining myself well.