Re: Problem with Multiple I2C devices on Due
I just post this on the Arduino forum and I think definitely has to post here too since this conclude the problem I was having and how I solved it. By the way this was tested using Arduino IDE 1.6.0 released yesterday or this morning.
I just want to share that I am able to share SCL1,SDA1 pin between RTC clock using Adafruit RTClib.h library with their RGBLCDShield.
I did have to do modification as everyone mention in the other posts. What I think is lacking is for someone to actually clearly setup an entire code for someone to see.
I am a novice so when reading "just change Wire to Wire1" and go to this folder and include this and that. It is very difficult, anyway I am able to put together some instruction here with hope it may be useful for someone.
Originally the RTC clock using RTClib.h by default can only be used on SDA(20) and SCL (21). However, I want to be able to share this with SDA1 and SCL1 pin on the Due. What is the point of having i2C if each device use their own pins right?
Anyway, because the built-in SDA1 and SCL1 have no pull up resistors, these will be the one to use according to the posts I read so far. Luckily the RTC clock and RGBLCDShield already has built in pull up resistors.
I copy RTClib folder in my Arduino library folder ( I am using MAC). and copy it in the same library folder with the name RTClib1
Rename the 2 files under that RTClib1 folder so you would have these 2 names RTClib1.cpp and RTClib1.h
Edit the RTClib1.cpp using text editor and here is what it looks like for me
#define DS1307_ADDRESS 0x68
#define SECONDS_PER_DAY 86400L
Change the 3rd and insert a new 4th line to look like :
#include <Wire.h> // keep this don't delete
#include <avr/pgmspace.h> // no change on this line
#include <RTClib1.h> // this is to match the name of the library and use <> instead of ""
extern TwoWire Wire1; // insert this line is the modification to include Wire1
Now still in the RTClib1.cpp use search and replace feature for the word Wire to Wire1. Make sure you do not change the "extern TwoWire" you add above.
There is no change needed in RTClib.h other than renaming that file to RTClib1.h
Okay, now to the .ino file or the Arduino IDE code that you use that you will download. Here are what mine look like :
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define WHITE 0x7
#include "RTClib1.h" // a modified version of RTClib.h from Adafruit to force the Due to use Wire1 instead of Wire
RTC_DS1307 rtc; //this stay the same as instructed in the tutorial
the rests of the code here can be kept similar to their tutorials or whatever you want to do with it.
Notice that the only place that you want to make sure is when you include the library RTClib1, make sure
you include RCTlib1.h not RTClib.h
(by the way I am not sure why sometime they use "" and sometime <> when including libraries)
Hope this helps you. I will try to attached my code and the RTClib1.cpp that I modified. The original RTClib.h can be found from Adafruit link and this did not change other than renaming the file.
The 2 ino files are for comparison purposes. One is for Uno the other one is for the Due. Same function.
Now you just have to remember to use RTClib1.h if you want to use it on SCA1,SDL1
and use RCTlib.h if you want to use SCA0,SDL0.
Tada..... I am a happy camper. Thanks you all inspired me to keep learning and sharing.
- (1.11 KiB) Downloaded 15 times
- (1.02 KiB) Downloaded 13 times
- (6.76 KiB) Downloaded 14 times