DS3231 precision RTC driver

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

DS3231 precision RTC driver

Post by pythoncoder » Wed Jan 06, 2016 10:21 am

Modules with a DS3231 offer a battery backed RTC with +-1 minute per year accuracy over 0-40°C and better than +-2 mins/yr over -40°C to +85°C. They are available at very low cost and offer a means of calibrating the Pyboard's RTC. This driver (adapted from the WiPy driver from @scudderfish) enables the calibration factor to be established. The DS3231 may be set from the Pyboard's RTC and vice-versa. The DS3231 can also be independently read.

URL https://github.com/peterhinch/micropython-samples.git
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: DS3231 precision RTC driver

Post by JimTal001 » Wed Jan 20, 2016 3:11 pm

This could be very helpful for my low power data loggers.
Questions:
1. Did you need to remove the pull-up resistors on the SDA and SCL pins of the DS3231?
2. Can you recommend a supplier of the DS3231?
3. Can I still use a mosfet to cut power to the 3.3V DS3231 I2C chip to conserve power when I don't need it?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS3231 precision RTC driver

Post by pythoncoder » Thu Jan 21, 2016 6:37 am

I bought mine from eBay http://www.ebay.co.uk/itm/281852153583? ... EBIDX%3AIT.

The module has a backup battery so it continues to run without power: switching 3V3 will work fine. My application is as an economical way to calibrate the Pyboard's RTC, which it does very effectively. However in environments subject to temperature fluctuations it is a better RTC than a calibrated Pyboard RTC because of its temperature compensation.

Regarding the pullups there may be an issue if you switch off the 3V3 supply: there is a possible current path from 3V3 through the Pyboad's pullups to the I2C data lines and from there via the board's pullups to the RTC's 3V3 supply and thence to ground. If this is an issue a circuit along the lines of my effort with an analog switch will fix it. But first I'd suggest measuring it: link the RTC's I2C and ground lines only to the Pyboard, put the Pyboard into standby and measure the supply current.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: DS3231 precision RTC driver

Post by dhylands » Thu Jan 21, 2016 7:22 am

You can use p-channel MOSFETS to turn off your pullup resistors.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: DS3231 precision RTC driver

Post by JimTal001 » Thu Jan 21, 2016 2:59 pm

Sorry but I'm not quite clear on the RTC pull-ups.

Is it necessary to remove the RTC pullup resistors for basic operation?
Here is a description of the DS3231 with a Raspberry pi and shows the pull-up removal:
http://www.raspberrypi-spy.co.uk/2015/0 ... pberry-pi/

If power is removed during pyboard standby (with RTC pullups removed), there maybe an issue which can be fixed using Peter's analog switch circuit show here:
https://raw.githubusercontent.com/peter ... _schem.jpg

Dave, are you saying that there is another way (easier) using p-channel MOSFETS to turn off RTC pullup resistors?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS3231 precision RTC driver

Post by pythoncoder » Thu Jan 21, 2016 5:02 pm

There is no need to remove the pullups in normal operation. The pullups on the Pyboard are 4.7K so only require 700uA of drive current from the Pyboard. Assuming the ones on the RTC are similar, we're still only talking about 1.4mA: the Pyboard will drive both with ease. I'm not sure why the Raspberry Pi guy removed them: I can't see a good reason. Removing them won't necessarily fix the issue of current being drawn in standby: a powered-down chip can still draw current via its inputs. There is no option other than to effectively disconnect the I2C bus from the device.

@dhylands Re using P-channel MOSFETs to disconnect the Pyboard's pullups when the Pyboard goes into standby. I assume you envisage the source connected to the Pyboard output, the drain to the DS3231 and the gate driven low to turn the transistor on. Will it work when the Pyboard drives its output low? In this case gate and the source will be at 0V so surely the device will be off. The 74HC4066 avoids this by virtue of a pair of complementary series MOSFETs - which was the reason I employed it.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: DS3231 precision RTC driver

Post by dhylands » Thu Jan 21, 2016 6:39 pm

Normally i2c pullups have a much lower value (1.2K) if they're going to provide the spec'd rise times (I wasn't necessarily thinking about the pyboard specifically).

On other reference style boards I've worked on there have been MOSFETs controlled via GPIO, so that the MCU could reduce power requirements (i.e. turn off the pullups when not actually using the I2C bus), or remove the pullups so that the pin could be used as say a GPIO.

As far as connection goes, the source goes to the pullup-voltage (3.3v), the drain goes to the pullup resistor, and the gate goes to an MCU pin. I think what you described is how voltage translation works. So I'm think more like this:
http://www.electronics-tutorials.ws/tra ... gif?81223b (where RL is the pullup resistor and insted of going to 0v it goes to the SDA/SCL line). You can control multiple pullups this way using a single MOSFET.

When the GPIO is low, the MOSFET is on, and the pullup is enabled. When the GPIO is high, the MOSFET would be off. There would need to be a pullup/pulldown on the MOSFET gate to control the powerup condition, but that pullup/pulldown can be a really large value (100K to 1M).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS3231 precision RTC driver

Post by pythoncoder » Fri Jan 22, 2016 8:13 am

@dhylands Right, I'm with you. This is a mod to the Pyboard while my solution works with a standard unit. I had a discussion with @Damien on making exactly this change when he was defining Pyboard V1.1. I also suggested making the MOSFET drain available as a switched 3V3 supply. I think he decided that too few would make use of the feature, and that the problem could be addressed with external switching. Another sticking point was re-purposing a GPIO pin to control the switch. We considered options such as a link which could be cut by the user to enable the feature but in the end he decided against.

For anyone able to wire up a DIL and a MOSFET the external solution does the job.
Peter Hinch
Index to my micropython libraries.

User avatar
wminarik
Posts: 7
Joined: Thu Oct 27, 2016 12:03 am
Location: Montréal, Canada

Re: DS3231 precision RTC driver

Post by wminarik » Wed Nov 02, 2016 3:09 pm

Some feedback on @pythoncoders RTC routines -they are excellent.

I compared the calibrated pyboard v1.0 internal RTC (with battery backup) to three versions of DS3231 based boards; a ChronoDot 2.1 and two $3 ebay versions. All have the extended range DS3231SN chip.

The DS3231.calibrate(minutes=10) results in a correction of -154 ticks to the pyboard RTC (the first set of points in the graph below). Tweaking this to -157 results in the second set of points). The small offset from the calibrate() estimate may be due to the temperature change between a running processor and one sitting around disconnected.

After an additional two weeks, there is an accumulated ~ 1.6ppm difference between the pyboard RTC and the ChronoDot's (that's about 1.3 seconds over 14 days). For a location with a regulated temperature (a house with the heat on), the calibrated pyboard RTC is quite accurate.

There is some variation in the DS3231 boards, but even the odd-board out (labelled DS3231_unit2) is only 0.5 ppm different from the other two (well within the advertised accuracy).

[attachment=0]DS3231_Comparison.png[/attachment]
Attachments
DS3231_Comparison.png
DS3231_Comparison.png (51.22 KiB) Viewed 13551 times

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DS3231 precision RTC driver

Post by pythoncoder » Thu Nov 03, 2016 6:01 am

Interesting! The DS3231 is a quick and inexpensive way to calibrate it for those of us lacking laboratory standard kit. I'm impressed by the accuracy of the Pyboard's calibrated RTC, at least under indoor conditions. I have an application which has been running since early this year, also calibrated against a DS3231 in 10 minutes. I'd estimate the drift at about a minute per year which accords with your results and also suggests it's pretty stable over long periods.
Peter Hinch
Index to my micropython libraries.

Post Reply