I am using SDA1, SCL1 for my DS3232 with RTClib on Due. It works on a Mega2560 on SDA, SCL. I ran I2c_test with success after I inserted #define Wire Wire1 However with ds3231.ino this define doesnot work. Looking at RTClib.cpp it looks like I can pass a parameter to Wire.begin(). I have tried sda1, scl1 and Wire1. They didnot work. Thanks for the help.
The unmodified RTCLib DS3231 sample code running on Arduino Due defaults to the Wire I2C connection on pins 20 SDA and 21 SCL. To enable the same sketch to connect to a device connected to the Wire1 I2C connection on pins 70 SDA1 and 71 SCL1, I changed line 333 of RTClib.h to reference &Wire1 rather than &Wire. Successful test output from the Wire1 connection is shown.
OP @Marlin Turbett pointed out that adding
#define Wire Wire1 to RTClib.h also works. I agree. The modification of line 333 of RTClib.h which I suggested originally is I think the minimal change to get the desired behaviour. But using substitution to achieve the same change also works. A global change across the whole library may be a bigger hammer than might be desirable.
#define substitution works in single compilation units, so the #define needs to be in the same compilation unit as the text to be overwritten. In this case in the library, not the sketch.
The technique of using #define directives to overwrite which Wire port is referenced in RTClib is used selectively by the library depending on arduino architecture (see RTClib.cpp lines 46-73, below).
#include "RTClib.h" #if defined(__AVR__) && !defined(TWCR) && defined(USICR) #include <TinyWireM.h> #define Wire TinyWireM #else #include <Wire.h> #endif #ifdef __AVR__ #include <avr/pgmspace.h> #elif defined(ESP8266) #include <pgmspace.h> #elif defined(ARDUINO_ARCH_SAMD) // nothing special needed #elif defined(ARDUINO_SAM_DUE) #define PROGMEM #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) #define Wire Wire1 #endif #if (ARDUINO >= 100) #define _I2C_WRITE write ///< Modern I2C write #define _I2C_READ read ///< Modern I2C read #else #include <WProgram.h> #define _I2C_WRITE send ///< Legacy I2C write #define _I2C_READ receive ///< legacy I2C read #endif
I don't know why the library code at lines 60 to 64 (for ARDUINO_SAM_DUE) was not effective, and I hope to do some more testing. Maybe others here can shed some light?
Update 2: Noting the link from @Edgar Bonet in the comments to the new example sketch
customWire_DS3231onSAMD21.ino in RTClib 1.14.1 which specifically caters for multiple i2c ports. Nice one.