funny problem with softI2C

Hello,

I am using a DS1307 via softI2C (due to using a home brew UNO board on a Mega) based on:

All is working well as long as I open the serial console.

If I power on the DS doesn't initialize.
If I upload code the DS does't initialize.
If I start the serial console to see my debugging messages all is fine. (also without the debug lines)

I know the RTC isn't working since I coded some errors for the RTC and SDcard to blink a LED.

What is the serial console doing that it can change the working of the code/board?

RTC parts of my code are:

#include <avr/pgmspace.h>
#include <I2cMaster.h>

// define clock and softI2C...
#define SDA_PIN 58
#define SCL_PIN 59
SoftI2cMaster rtc(SDA_PIN, SCL_PIN);
#define DS1307ADDR 0XD0 //i2c 8-bit address for DS1307. low bit is read/write

int LED = 16; // output pin for LED

// Read the DS1307
uint8_t readDS1307(uint8_t address, uint8_t *buf, uint8_t count) {
  if (!rtc.start(DS1307ADDR | I2C_WRITE)) return false; // issue a start condition, send device address and write direction bit
  if (!rtc.write(address)) return false; // send the DS1307 address
  if (!rtc.restart(DS1307ADDR | I2C_READ)) return false; // issue a repeated start condition, send device address and read direction bit
  for (uint8_t i = 0; i < count; i++) { // read data from the DS1307
    buf[i] = rtc.read(i == (count-1)); // send Ack until last byte then send Ack
  }
  rtc.stop(); // issue a stop condition
  return true;
}

void blinkRTC(int count)
{
  for(int i = 0; i < count; i++)
  {
    digitalWrite(LED, HIGH);
    delay(50);
    digitalWrite(LED, LOW);
    delay(50);
  }
}

void setup()
{
  
  // setup USB-serial output for external use
  Serial.begin(115200);

  pinMode(LED, OUTPUT);

  // check Real Time Clock
  uint8_t r[8];
  if (!readDS1307(0, r, 8)) {
    Serial.println("RTC error...");
    blinkRTC(600); // blink steady for a minute
    return;
  }

Does the softI2C use the same timer as Serial? - might be the root cause...

Hello Rob,

I did look in the I2Cmaster lib but the only thing related to clock is:

uint32_t const F_TWI = 400000L;

No way to tell what is used.
On the other hand, in the arduino code the serial port is always setup so what changes on the arduino the moment I press the "console" button in the IDE making the code work?

Tried to run the board with external power with same result.
code is only working when the serial connection is "serial reset" by uploading sketch or opening serial console from IDE.
With USB attached/powered and doing a "cat /dev/ttyACM0" no go so it seems a "serial reset" is making things work as it should.

Does the softI2C use the same timer as Serial?

The hardware serial interface doesn't use a timer.

Have you tried inserting a delay in your code before you read the DS1307? The DS1307 doesn't activate it's I2C interface if power supply hasn't stabilized. Insert a delay(5000) before you try to read anything from the DS1307.

I did try a 500ms delay without luck, will try longer one when I get back at home, thanks for helping!

Inserted a 5sec delay to stabilize power supply and indeed the DS start working as it should, sharp thinking Pylon, Thanks!

The story continues, when powering the Mega with USB cable or 12V on the DC connector the DS1307 works fine (with the 5sec. delay) I use the regulated 5V output of the Mega to power the DS1307.

On my addon board besides the clock (and some other stuff) I placed a DC-DC regulator that converts up to 24V to 5V, the 5V goes in the VIN of the Mega board.
When using this power supply the DS1307 does not function, however all other hardware (including the Mega) does.
Even setting the delay to stabilize power to 10sec. does not solve this, am I missing something crucial like power drop inside the Mega when feeding with 5V?

The device is used on a car battery (up to 14.5V) so I am afraid to put that in the DC connector of the Mega that is why I choose my own power supply.

On my addon board besides the clock (and some other stuff) I placed a DC-DC regulator that converts up to 24V to 5V, the 5V goes in the VIN of the Mega board.

This is wrong. A regulated 5V goes directly to the 5V pin. If you supply 5V to Vin you get a Vcc of max. about 4V which is not enough for the DS1307 to work correctly. Vin is about the same as if you would supply by the power connector.

That is indeed wrong, to be more precise that is very very stupid of me, I didn't read the docs and presumed that a 5V. feed like USB would be enough.
Will try with a 9V DC-DC and are pretty sure that will do better...
Thanks for pointing that out to me!

I would use the DC-DC converter with 5V and feed that 5V to the 5V pin of the Arduino. With the 9V DC-DC converter feeding the onboard 5V linear regulator wastes too much energy.

If I am correct feeding the 5V pin is not advised, apart from that if I feed the 5V to the arduino's pin I have to rewire my extension board, the easiest is putting another DC-DC converter instead with an output voltage of 7V. or an LM7807. Since the car battery delivers up to 14V a DC-DC is better but I am not sure if they excist in a 7V version, a LM78xx probably has some serious heat dissipation converting 14 to 7V.

The positive part: it's a simple thinking error and nothing serious!

If I am correct feeding the 5V pin is not advised, apart from that if I feed the 5V to the arduino's pin I have to rewire my extension board, the easiest is putting another DC-DC converter instead with an output voltage of 7V. or an LM7807. Since the car battery delivers up to 14V a DC-DC is better but I am not sure if they excist in a 7V version, a LM78xx probably has some serious heat dissipation converting 14 to 7V.

I don't see a reason why you shouldn't feed a regulated 5V to the 5V pin. You loose the reverse voltage protection and the overcurrent fuse (USB) but you win efficiency if you use a DC-DC converter instead of the linear regulators onboard. If you use just linear voltage regulators to get 5V from a 14V source you loose 65% of the energy and you're heating the environment. I don't know how much current your project consumes but my suggestion is clear: use a DC-DC converter to get 5V from your source and connect it directly to the 5V pin.

You can buy small modules on EBAY for around $2 that are designed for automotive use that will supply a stable regulated 5v output

Look for DC-DC buck converter

such as

http://www.ebay.com/itm/3pcs-LM2596-DC-DC-Buck-Converter-Step-Down-Module-Power-Supply-Output-1-23V-30V-/370808283934?pt=LH_DefaultDomain_0&hash=item5655e5631e

Craig

I have a DC-DC convertor (in a handy LM78xx package) Craig but it was connected to the VIN...