DS1307 using SoftI2CMaster Issue


For Some Reasons, I had to use SoftI2CMaster Library to make a software I2C to connect Arduino MEGA with RTC Module.

After a lot of Tests, it works fine but the problem is little weird.

The Test Code writes data in a certain location then read it again and then prints it in the serial monitor, But I found that numbers that have its hex representation not included A to F (10 TO 16) works fine, But if it has A To F, it gets wrong data.

For example: when I tried to save (0x23), I get the right data in the Serial Monitor which is (35), But when I tried (0x1A), I get wrong Data.

And that always happen whenever I have (A to F) in the data number.

// Simple sketch to read out one register of an I2C device
#define SDA_PIN 1 // = A4
#define SCL_PIN 0 // = A5
#define I2C_PULLUP 1
#include <SoftI2CMaster.h>

#define I2C_7BITADDR 0x68 // DS1307
#define MEMLOC 0x0A
#define SECLOC 0x00
#define MINLOC 0x01
#define HOURLOC 0x02
#define DAYLOC 0x03
#define DATELOC 0x04
#define MONTHLOC 0x05
#define YEARLOC 0x06

void setup(void) {
    if (!i2c_init()) // Initialize everything and check for bus lockup
        Serial.println("I2C init failed");

void loop(void){
    if (!i2c_start((I2C_7BITADDR<<1)|I2C_WRITE)) { // start transfer
        Serial.println("I2C device busy");
    while(!i2c_write(SECLOC)); // send memory address
    if (!i2c_start_wait((I2C_7BITADDR<<1)|I2C_WRITE)) {
    Serial.println("I2C device busy");
    byte val = i2c_read(true); // read one byte and send NAK to terminate
    i2c_stop(); // send stop condition

For Some Reasons, I had to use SoftI2CMaster Library to make a software I2C to connect Arduino MEGA with RTC Module.

#define SDA_PIN 1 // = A4

#define SCL_PIN 0 // = A5

It would appear that the "Some Reason" is your failure to read the Mega's data sheet. If you ditch all that softI2CMaster stuff, use the standard code, and connect the RTC to pins 20/21, which is the I2C bus in a Mega, you will likely find that you can use the RTC just like everybody else does.

You are right, I am using the pins that are already assigned to the hardware I2C, but this is not the point, I am using a hardwired RTC to the Standard pins while testing, but I am intending to use other pins, that what makes me use the SoftI2CMaster library.

I don't want to go in details that are not related to the topic so I typed "For Some Reason".

The Problem is Solved, and I would like to share it with any1 faces the same problem.

  1. RTC Registers are limited to specific numbers, for ex. Seconds register is limited to 59 you cant save a number that's more than that.

  2. From the DS1307 datasheet, Data saved in the registers should be in BCD Format, so you have to convert the number you want to BCD while setting the clock and when reading you have to convert the reading(in BCD Foramt) to decimal format.

OK. So in other words, items addressed in the RTC libraries that are available?

RTC Libraries available uses Hardware I2C, I found RTC Library that works with Software I2C but it didnt work for me, So I implement my own library (that uses Software I2C Library).


I'm also struggling to get rtc readings with software i2c, and really can't afford using the adc pins for i2c communication. Did you manage to get the time readings? Can you share it please?
Thank you.