Two DS3231 on I2C bus


Playing around with a project that use 2 x RTC's (DS3231) on the I2C bus.
One address is the base address, Ox57, other Ox56 (A0) bridged.

How do I write and read time from one, and later the other?

if i use
#include <RTClib.h>
#include <Wire.h>

RTC_DS3231 rtc;

in the program, it will read both the RTC's if inserted separately into the circuit.

How do I address either individually?


Does the RTC constructor have an option to specify the address?
You'd need two RTC objects, or an array of two RTC objects.

Why do you need two RTCs?

If you are playing with the following DS3231 RTC Module (Fig-1), then you have to note down the following points:


1. The larger IC is the DS3231 RTC Chip and its I2C Bus address is fixed at 0x68 (Fig-2). If you want to connect more RTC's in the Bus, you need to use I2C Bus Multiplxer.


2. The smaller IC is the EEPROM of type AT24C32. The open jumpers A0, A1, A2 are for this chip to select its I2C address. Fig-3 says that these jumpers allow to connect as many as eight EEPROMs in the I2C Bus with addresses: 0x50 to 0x57. In my module, the jumpers are at open condition; so, the I2C Bus address of my EEPROM is 0x57 (Fig-2).


Just to clarify what @GolamMostafa is saying: if you want to read/write both RTC, you must add an i2c multiplexer, because the RTC chip has one fixed address, and you can't have 2 devices on the same bus with the same address. The multiplexer isolates one or the other RTC so that only one is connected to the bus at any time.

As an alternative to the multiplexer, you can use an Arduino with 2 separate i2c buses. Most do not have more than one. But Arduino based on samd21 chip can be configured to have a second i2c bus, for example.

A third alternative, and probably the easiest for you, is to make a second i2c bus on whatever Arduino you have using an i2c software library. Like "software serial", a software i2c bus is slower and less widely compatible, but will probably be ok for your purpose. I say probably, because we don't know why you want to do this, it seems a strange idea.

As was also mentioned, the bridges on your module change the address of the eeprom chip, not the RTC chip

1 Like

Thanks all - fully understand!
Just by the way, does the DS1307 RTC Module and the DS3231 have the same address?
I know I can google it - but you guys are much faster!
Is there another Super accurate RTC with a different address?

You can't read two DS3231 on the same bus. The address that you changed is not the RTC address, it is the address of the on board EEPROM chip. The DS3231 has no address settings at all. That is why all the libraries for it, do not bother to allow you to specify the address.

Do an I2C scan of your module. You will see two devices, one is the RTC, the other is the memory chip.

Yes, the DS1307 has the same address as the DS3231. But you didn't have to ask here. Maxim has all the data sheets online.

Also unless you are using rechargeable CR2032 in the module referenced in post #4, it would behoove you to remove the diode/resistor battery charger circuit, as prolonged charging can damage a non-rechargeable battery.

Then perhaps do so, it saves the voulunteers on here doing it for you.

Do apologise!!!

Tried the humble quad bilateral switch CD4066 - to switch in one then the other, controlled by a spare data pins on the Arduino. You can switch both Clock and Data leads (if required) as there are four switches on the chip.
Works like a charm.
Only switched the data leads via the switch.
Thanks yet again!

Did you try software i2c? Would use another Arduino pin, but at least you would not need the extra chip.

PCF8563. Not sure if this matches up to ds3231 in accuracy terms, I guess roughly equivalent to ds1307. Address 0x53.

Can you explain why you need two RTCs? The DS3231 has an internal oscillator, so both would be running the clock at the same speed (except for any small tollerance difference in the oscillator frequency).

My guess is in order to detect paranormal phenomena. One RTC would be protected by distance or shielding, and the other exposed to the potential phenomena. Any change in the time discrepancy between the two RTC could be used to infer the presence of the phenomena.

(PS I have been re-watching the entire series of the X-Files recently... The Truth Is Out There!)

That would at least explain why it's top secret.

Nothing to do with paranormal....
I wish to use two RTC's, one as an accurate short interval timer (seconds, days or weeks), which is reset after the timing interval, and the other, the real time if it is required to be displayed - shift registers and seven segment LED's.
Just to answer the other question, why another chip (4066)? My programming skills are roughly 3 out of 10, whilst my traditional electronic skills, are much higher.
Maybe, my 60 plus age, will explain! Not that we don't have brilliant programmers > 60!

It completely eludes my why you'd need two separate RTC's for this. One would work just fine.
It's sounds like what everybody's expecting: you're trying to solve a shortcoming in your software by adding hardware. I'd suggest solving the actual problem instead.

1 Like

What I do after the timing period, is to reset the RTC with this code below
ss = 0;
mm = 0;
hh24 = 0;

By doing this, the REAL time is lost - if using a single RTC.
If I somehow store the real time, and write it back after the timing period, the real time will be out by the 'timing period'.

Then don't do it! Calculate the interval by using a single RTC, never resetting it, capturing the time before and after the interval and subtracting one from the other.

This thread has turned out to be a classic example of an "X-Y problem". You want to time a period, but you can't figure out how to do it (problem "Y"), so you latch on to the idea that you have to use 2x RTC. Then you find you have a problem with connecting 2x RTC and ask the forum for help with that problem (problem "X"), and 15~20 posts later we finally figure out what your real problem is.

So let's forget all that 2x RTC nonsense and focus on your real problem. Tell us about the period you want to time. How long, typically, and how precise do you need to be?

DS1307 looses/gains 10 minutes per year at the very least. Many DS1307 modules are not well designed or use less accurate crystals and loose/gain far more than that. DS3231 typically looses/gains only around a minute per year, because the crystal is built into the chip package by the chip manufacturer. So the module manufacturer can't screw it up so much, at least in terms of accuracy. They can and do still make other mistakes, of course!

1 Like

Be careful when buying the cheaper DS3231 boards, some of them use the DS3231M variant of the DS3231 chip, which does not use a crystal but a MEMS oscillator, which is not nearly as accurate. Still better than the DS1307 though.

The DS3231 boards also typically have the 24C32 battery backup up memory chip, which would be a convenient place for you to store data for your time interval measurements since it can be rewritten many more times than then internal EEPROM on the arduino.

You should get familiar with unix time when measuring time intervals or doing time comparisons, most if not all libraries used with an RTC work with unix time, and it makes the coding much simpler.