Wire not working on Due?

This had got me baffled, I want to use a real time clock in my project that uses a Due.
I cannot get it to work!
The same code works fine on the Uno and Mega, but the Due refuses.
Wire.begin() on the Due should be SDA(20), SCL(21).
so when I comment out the following....
//#ifdef AVR
Wire.begin();
//#else
//Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
//#endif
This should work on the Due...???
Any thoughts?

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  Serial.begin(9600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();
  // following line sets the RTC to the date & time this sketch was compiled
  //rtc.adjust(DateTime(__DATE__, __TIME__));

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(__DATE__, __TIME__));
    
  }
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    /*
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    */
    Serial.println();
    delay(1000);
}

Provide a wiring diagram. I'd guess that the problem is on the hardware side but that's currently just a wild guess.

Well, I found something interesting.

The Due apparently has internal pull-ups on SDA(20), SCL(21) as I can measure 3.3V on the pins. Yet when Wire.begin() is called out it (communication to my RTC module) does not work.

The Due also has a second wire connection SDA1 and SCL1 which are called with Wire1,begin(), however there are no pull-ups on these pins. I installed a couple of 10K's for use as pull-ups and now the RTC is working with Wire1.

Must be a problem with the board that Wire.begin is not working on SDA(20) and SCL(21)??
I've tried it with two different Due boards with the same results.

Regards, Bart

The Due apparently has internal pull-ups on SDA(20), SCL(21) as I can measure 3.3V on the pins. Yet when Wire.begin() is called out it (communication to my RTC module) does not work.

The Due has such pull-ups (1k5 according to the schematics) on pin 20 and 21 and it doesn't have on SDA1 and SCL1.
A 10k pullup is probably too weak if the bus is more than a few centimeters long, don't forget it goes to 3V3 and not 5V.

What do you mean by Wire.begin() is working or not working? Wire.begin() does not communicate with your RTC module, it just configures the hardware that the TWI module will be used.

I ask again for a wiring diagram.

Over a year ago Paul Soffgren found a bug in delayMicroseconds() with an oscilloscope. At that time, he thought he was the only person informally maintaining Wire.h . That discussion thread seemed to settle on a very low level delay function that was edited.

But we have a bug seems to be triggered when Arduinos are connected to about three external devices. Meanwhile, the documentation for the Wire library sounds like Wired is regarded as fine by it's authors. Here is my guess: Two people are editing the delay functions. One editor is trying to make the TWI library use existing functions and also respond correctly. The other editor might be the person who edited something mentioned in the release notes:

"Fixed delayMicrosecond() when interrupts are disabled." I suspect a well meaning fixer has re-introduced the bug that breaks the two wire connection software when the microcontroller gets busy.

I have the same problem. I am using a Due with an Adafruit Bmp 180 barometer connected to Pin20 and Pin21 and then I am connecting a second device, the Real Time Clock from an Adafruit SD Logger plugged into the Due SCL1 and SDA1 secondary bus. I will also add that I switched to a Due because I figured gobs of memory and really high clock speed ought to get me away from the aggravation of struggling with hard to measure problems attributed to "memory shortage" on an Uno.

Now the barometer is working fine and the Real Time Clock is showing the following bug.
The Real Time Clock says the unixtime.now is 2313941504 in my application program running on the Arduino Due.
Pretty much the same code running on an Arduino Mega always correctly reports the unixtime as
1398029773 which is the unixtime my Linux box says as I write this.

I have tried several RTC setup programs to see if I can isolate the problem
First, I switched to Arduino 1.5.6-r2 library. It is one of the latest.
Now the test program
libraries ->RTClib_master->softrc # Demo code shows unixtime and current time correctly.
But the other test program you used, with the IFDEF code doesn't work for me either
libraries->RTClib_master->ds1307 # Returns unixtime.now of 2313941504 and never changes.

Hi,
I also have a lot of problems with using I2C with the Due. My circuit use a few of I2C components and I load a small MultiSpeedI2CScanner (MultiSpeed I2C Scanner - 50,100,200,400 KHz. - Libraries - Arduino Forum) with which I make the tests.

With an Duemilanove I see every component which is installed. I think the Hardware is ok. With a scope I looked for the signal and the peak were about 3.8V on both wire. I changed the board and use a Due with a level shifter on Port 20/21 und als SDA1/SCL1. The signal are always with a peak of 3.8V and looked an a similar timing, but the software find any chip. I tried to reduce the voltage to 3.3V ( only the DS1307 isn't available). The Due find nothing.

There should be a problem in the I2C or TWI library, but sorry I can't fix that problem,unfortunately because I have not enough knowledge about it. Please help me to fix this problem, it's not possible to me to change over my application to an Mega of cause I need the speed and the memory of the Due.

Regards, Detlef

If you have 3.8V on the I2C lines I guess your hardware is not wired correctly. According to the datasheet this may damage your processor if it hasn't already. None of the posters has shown a wiring diagram, so I'd expect the hardware to be the problem first.

The Real Time Clock RTC from the Adafruit SD card plus clock works on the Arduino Due when you use the initialization code from the libraries->RTClib_master->ds1307 clock example program.

I will quote the initialization code in a moment. Three important things I point out to you:

First the entire IFDEF block is in the setup{} function.
Second, the IFDEF block has to execute before any other Real Time Clock code.
Third: The logic of the IFDEF block is reversed from what you might expect it to be. I mean, the Arduino Due is not an AVR computer. The code author was very clever and he also understands the compiler literal constants way better than me.

I'll spare you my sad story of spending hours trying to debug my Arduino DUE with a Real Time Clock stuck 20 years in the future. The DUE simply uses a different location for the I2C bus and the Wire library uses the different location if the IFDEF statement works. You can actually put print statements in the IFDEF code to see if the branches are working right.

void setup () {
  Serial.begin(57600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }
}

Here are the comments I use to document how the Adafruit SD Card reader and Real Time Clock are connected to the DUE
Arduino SD Card logger connected with 6 jumper wires from the 6 pin DUE Spi connector. Power, ground, and 4 signal wires.
The RTC clock uses power from SPI connector and sends data by two wires of it's own:
Connect SDA and SCL points on Logger to pins 20 and 21 on the Due board.

Hi there LeeMcK
I started out a project for logging temperatures(both to SD and to online DB), and controlling a pump, with a Uno. I soon ran into memory problems, and therefor ordered a Mega 2560. But when the mail finally arrived, I had got the Due. So while i wait for the Mega to arrive(up to 25days!!), I want to see if I can get i working on a Due.
I have got everything functioning on the Uno, but only one thing at the time, because of lack of memory.

On the Uno, I have added the EthernetShield, for logging to microSD and to online DB.
and the Adafruit Logger shield, for getting a RTC time.

I am a bit worried, to damage some of my hardware, if i just connect and try. But it seems that you have got it working.
But im a bit of a newbie. So I dont really understand the explanation, of how you connect the Due board, and the Adafruit Logger shield. I use it just the logger shield for the RTC.
Please can you help me with this?