doughadfield
Posts: 41
Joined: Fri Dec 21, 2012 5:27 pm

Install DS3231 Real Time Clock - Latest Info

Mon Sep 26, 2016 10:29 pm

Hi All, I just received a couple of these very cheap, neat DS3231 based real time clock modules from China (ebay specials) and have spent a good while getting them configured with the latest (mid-2016) Jessie based Raspbian. The current info on the internet is mostly out of date, so by piecing together nuggets from here and there, I've established the minimum amount of work necessary to configure latest Raspbian (lite) to get the RTC module working. It's very simple :-)

Once you have a nice shiny clean raspbian (or raspbian lite) image running (I used raspbian lite exclusively), and you've updated it to latest everything (sudo apt-get update; sudo apt-get upgrade; sudo reboot), you're ready to start.

There are only two edits you need to do:
1. put the below line into the /boot/config.txt file: (edit it with your favourite editor and type the line in - or copy and paste it from here :-) )

dtoverlay=i2c-rtc,ds3231

2. edit the /lib/udev/hwclock-set file (sudo nano /lib/udev/hwclock-set) and "comment out" the following lines ("comment out" means put a # at the beginning of each of the lines, so they become comments and are ignored by the system)

if [ -e /run/systemd/system ] ; then
exit 0
fi

so they become:

#if [ -e /run/systemd/system ] ; then
# exit 0
#fi


...and that's it - that's all you need to do. Shut down your system, connect the rtc module, then power up and test with the command:

sudo hwclock -r

this will read the time directly from the rtc module. If a sensible time string comes back, then you're cooking! Fully test by powering off the pi, disconnecting network cable (if you're running wireless, you may need to turn your router off for a bit), plug a screen into the hdmi output of the pi, wait 10 mins, then power it up. Once it's up, using a locally connected keyboard, log in and type "date" - if the time is correct and not 10 mins slow (or however long you had the pi powered down for) then the system has read the time correctly from the rtc module!!

Some posts have recommended de-installing the fake-hwclock package, or the ntp package - there's no need to do that - just do the above.

Before settling on the above steps, I tried lots of things from several web posts, including removing packages, downloading stuff, modifying the /etc/init.d/hwclock.sh file etc. I saw some of the rtc corruption that others have seen. But in the end (after re-flashing a new Jessie lite image several times ;-) ) the above was all that was needed. I've done this on a couple of different pis now, and the above seems to work consistently. So, all good.

enjoy

Doug

doughadfield
Posts: 41
Joined: Fri Dec 21, 2012 5:27 pm

Re: Install DS3231 Real Time Clock - Latest Info

Mon Sep 26, 2016 10:38 pm

Following on from the above, here's a picture of the modules I used (the connector simply plugs onto the first 5 pins of the pi) - here are some useful hwclock commands to test and play. They all need to be run as the root user, so precede with "sudo" if you're not running as root:

read time directly from rtc module
hwclock -r

update rtc module time from system time (system time should be regularly updated by ntp from the internet if your pi is networked):
hwclock -w

update system time from the rtc module (this should happen on startup):
hwclock -s

and the most fun of all - monitor the "drift" between your system clock and the rtc module:
hwclock -c

the above command will not return to the prompt, it will run continuously, printing the clock drift every 10 seconds, until you interrupt it (with Cntl-C).
Attachments
s-l225.jpg
picture of DS3231 module I used
s-l225.jpg (8.08 KiB) Viewed 47835 times

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Install DS3231 Real Time Clock - Latest Info

Mon Sep 26, 2016 11:57 pm

Thanks for that picture. Modules like that didn't exist when I bought a DS3231 a number of months ago.
The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Tue Sep 27, 2016 1:17 am

Jednorozec wrote:Thanks for that picture. Modules like that didn't exist when I bought a DS3231 a number of months ago.
Really? I've been using that sort of DS3231 module for a couple of years. I get them for less than $3 each.

doughadfield
Posts: 41
Joined: Fri Dec 21, 2012 5:27 pm

Re: Install DS3231 Real Time Clock - Latest Info

Tue Sep 27, 2016 7:32 am

I'm presuming that the shape and construction of the module would be irrelevant to it's operation, if it's using the same chip (DS3231). So the above instructions should work regardless of the origin of the device itself. Let us know if your experience is different (with a picture if possible) as it would be good to know if certain modules labelled DS3231 behave differently. It may be that there are clone chips or mis-labelled modules out there...

Doug

UK_Ham
Posts: 41
Joined: Fri Nov 18, 2016 3:00 pm
Location: Surrey

Re: Install DS3231 Real Time Clock - Latest Info

Sat Nov 26, 2016 10:39 am

Sorry to be a pain but you said you installed it on the first five pins, as a total newbie and having got one of those modules please can you confirm the pins used, I want to use it on a Pi that is run offline.
Name is Mike, Radio Ham and Weather Nut.
Total Beginner with Rpi Zero

User avatar
DougieLawson
Posts: 36173
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Install DS3231 Real Time Clock - Latest Info

Sat Nov 26, 2016 10:45 am

Pin#1 3V3
Pin#3 SDA
Pin#5 SCL
Pin#7 GPIO4
Pin#9 GND

https://pinout.xyz

You don't need pin#7 and that loses access to the default 1-wire pin but the other four are the four wires needed for 3V3 I²C.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

UK_Ham
Posts: 41
Joined: Fri Nov 18, 2016 3:00 pm
Location: Surrey

Re: Install DS3231 Real Time Clock - Latest Info

Sat Nov 26, 2016 10:49 am

Thank you for the prompt reply. Thought that was the case but wanted to make sure first.
Name is Mike, Radio Ham and Weather Nut.
Total Beginner with Rpi Zero

alphanumeric
Posts: 2077
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Install DS3231 Real Time Clock - Latest Info

Sat Nov 26, 2016 4:03 pm

I bought one of these from Adafruit, https://www.adafruit.com/products/3013 then set it up following this, https://learn.adafruit.com/adding-a-rea ... spberry-pi Pretty simple guide and working fine so far. I'm running the full Raspbian Pixel though. Mine is soldered and wired up on a Proto Hat ,https://www.adafruit.com/products/2310 I used a tall stacking header instead of the default one. I have my sense hat plugged in on top of that. I needed access to all the GPIO pins for my sense hat. It's basically an Astro Pi, minus the camera board.

tonygrimwood
Posts: 1
Joined: Wed Feb 22, 2017 8:24 pm

Re: Install DS3231 Real Time Clock - Latest Info

Wed Feb 22, 2017 8:30 pm

I've installed a DS3231 RTC into a Pi and I have it working nicely, and what I'd like to be able to do is have the RTC automatically updated by NTP every, say, 15 minutes. Should I use a cron task with hwclock -w or is there a better way to do this?

Thanks,

Tony

W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 2:47 am

I've recently come across an interesting "gotcha" with the otherwise very nice modules. That yellow rimmed object on the bottom isn't a battery. It's supercapacitor. This means that if you have a stock of spare RTC modules or you have a module on a Pi which is used sporadically, you are rather likely to find that the RTC module doesn't work, at least until the cap recharges. I have yet to see any data on how long it takes to recharge the cap or how long the charge on it will keep the RTC running if no other power is available.

So...does anyone know of a "Pi ready" DS3231 RTC module that takes a replaceable battery? (Note all the qualifications...the Adafruit DS3231 module has a replaceable battery, but it comes with male header pins and the pinout doesn't match the Pi.)

alphanumeric
Posts: 2077
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 10:57 am

Adafruit have a new one that just plugs into the GPIO header, https://www.adafruit.com/products/3386 PCF8523

I have several Adafruit RTC breakout boards in use. One DS1307 is plugged into a solderless bread board. And wired to my Pi via jumpers and a GPIO breakout board. Adafruit calls them cobblers. That Pi is dedicated to bread boarding circuits so I don't mind the RTC being hoked up with jumpers.

The other DS3231 is soldered to a Proto Hat. The proto hat is plugged into the GPIO header of my Pi and has a sense hat on top of it. An Astro Pi I guess. https://www.adafruit.com/products/2310 I used a stacking header instead of the one that comes with it.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 11:46 am

W. H. Heydt wrote:I've recently come across an interesting "gotcha" with the otherwise very nice modules. That yellow rimmed object on the bottom isn't a battery. It's supercapacitor. This means that if you have a stock of spare RTC modules or you have a module on a Pi which is used sporadically, you are rather likely to find that the RTC module doesn't work, at least until the cap recharges. I have yet to see any data on how long it takes to recharge the cap or how long the charge on it will keep the RTC running if no other power is available.
The DS3231 is designed to work safely with Lithium primary cells - as such it is UL certified *not* to charge. External circuitry is required for that, and these modules do not have that. See https://www.maximintegrated.com/en/app- ... vp/id/3816 https://datasheets.maximintegrated.com/en/ds/DS3231.pdf

The cell on mine has one terminal embossed with a "+", leading me to think it isn't a symmetric supercap.

alphanumeric
Posts: 2077
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 1:01 pm

My Adafruit DS1307 and DS3231 both use CR1220 coin cells. They should be good for many years of service.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 1:22 pm

Just checked through my little pile of these (bought for $1 each in a lot of 5 :shock: ) I can see different parts of the text depending on how the cell landed when they welded the connector on. Looks like the battery is a Lithium CR1025.

alphanumeric
Posts: 2077
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 1:28 pm

The batteries on mine are in holders and swappable/replaceable. The RTC breakout boards I have don't come with a battery and I have to remember to order one separately.

W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 2:50 pm

alphanumeric wrote:Adafruit have a new one that just plugs into the GPIO header, https://www.adafruit.com/products/3386 PCF8523
From the Adafruit description of the PCF8523...
The PCF8523 is simple and inexpensive but not a high precision device. It may lose or gain a second or two per day. For a high-precision, temperature compensated alternative, please check out the DS3231 precision RTC.
I have several Adafruit RTC breakout boards in use. One DS1307 is plugged into a solderless bread board. And wired to my Pi via jumpers and a GPIO breakout board. Adafruit calls them cobblers. That Pi is dedicated to bread boarding circuits so I don't mind the RTC being hoked up with jumpers.

The other DS3231 is soldered to a Proto Hat. The proto hat is plugged into the GPIO header of my Pi and has a sense hat on top of it. An Astro Pi I guess. https://www.adafruit.com/products/2310 I used a stacking header instead of the one that comes with it.
Both solutions I'm trying to avoid as most of my Pis with RTCs get moved around quite a bit. One, for instance, is my "travel alarm".

alphanumeric
Posts: 2077
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 3:14 pm


W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 4:18 pm

alphanumeric wrote:Ok, how about this one? https://thepihut.com/collections/raspbe ... rtc-pizero
1. DS1307, rather that the much more accurate DS3231

2. Not a "just plug it in and use it". Indeed...note that it is using a 40-pin MALE header, so not compatible with anything other than a Pi Zero on which one has installed a female header, and the marked connections on the end of the board that start with 5V then ground aren't compatible with a direct connection to any Pi.

Sorry to nitpick, but I did list what I consider requirements for what I want and this completely misses two of them just at first glance.

So shrink it down to just the size needed to hold everything, switch to a DS3231, give it a female header, and put the pins in an order that matches a Pi and I'd buy at least 3 or 4 of them even at the price this one is offered at.

alphanumeric
Posts: 2077
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 4:27 pm

Look at the pictures more closely, its a stacking header, male on one side female on the other. Or just use a normal female header, assuming the header isn't already soldered in. In which case just clip the excess off with wire cutters. What's the big deal with being a second off a day? And won't it just sync to the correct time when you get an Internet connection? Not meaning to be sarcastic or anything, just honestly wondering?

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 4:57 pm

@WHH If your Pi is left hooked up to the 5V adapter after doing a sudo poweroff, the RTC will still be getting 3V3 on it VCC and the backup battery won't be used. Have you experienced one of these module's backup cell fail?

Lastly, it's safe to leave new ones unused in a drawer. Those clever folk at Maxim designed them so that the backup cell isn't employed until after the DS3231's first I2C communication.

W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 5:45 pm

alphanumeric wrote:Look at the pictures more closely, its a stacking header, male on one side female on the other. Or just use a normal female header, assuming the header isn't already soldered in. In which case just clip the excess off with wire cutters. What's the big deal with being a second off a day? And won't it just sync to the correct time when you get an Internet connection? Not meaning to be sarcastic or anything, just honestly wondering?
For the Pis I want the RTCs for, they may stay unpowered for 6 months or more at a time, and when powered up, they won't have a net connection. A few seconds off, I'd probably agree with you, but we're talking possibly 3 minutes or more and I'd prefer to be closer than that. If there were to be a net connection, I wouldn't bother with an RTC at all.

W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 5:57 pm

gregeric wrote:@WHH If your Pi is left hooked up to the 5V adapter after doing a sudo poweroff, the RTC will still be getting 3V3 on it VCC and the backup battery won't be used. Have you experienced one of these module's backup cell fail?
Define "fail". I've had previously unused units fail to work properly (set one up, set the time, unplug to check, wait a few minutes, add power...and time is at unpowered default). So I suppose you could call that a "failure". Once connected to a Pi with power, set and left running, it will keep time just fine.
Lastly, it's safe to leave new ones unused in a drawer. Those clever folk at Maxim designed them so that the backup cell isn't employed until after the DS3231's first I2C communication.
See above.

What I'm sort of vaguely toying with is the idea of creating a board with a parallel series of 5 pin connectors and a 3.3v supply to plug unused and intermittently used modules onto. I could set the time in the modules and store them with power until I need to go somewhere that I need an RTC, at which point I'd grab one and put it on the Pi that needs it. All of this is why I'd prefer units with replaceable batteries even though I think the idea of using a supercap to maintain the clock is exceedingly clever. I just wish I could find a statement somewhere about discharge and recharge times. I.e. How long can I leave a unit with no external power and expect to have the time correct (to within its accuracy limits) when powered up again, and how long will it take to recharge the cap to the point that the discharge time is valid again. The recharge time *ought* to be fairly short. Minutes? Hours? Can't pull too much power off the 3v3 rail. Discharge time? Months?

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 6:05 pm

Are you still thinking that yellow thing is a supercap? It cannot be - those things require a charging circuit, and there plain isn't one on the module, and certainly not one inside the DS3231. It's a CR1025 lithium primary (no-rechargable) cell, the best for long backup time according to the app note I linked up above. If you want longer backup cell life, fit a bigger cell.

W. H. Heydt
Posts: 10904
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Install DS3231 Real Time Clock - Latest Info

Thu Feb 23, 2017 6:51 pm

gregeric wrote:Are you still thinking that yellow thing is a supercap? It cannot be - those things require a charging circuit, and there plain isn't one on the module, and certainly not one inside the DS3231. It's a CR1025 lithium primary (no-rechargable) cell, the best for long backup time according to the app note I linked up above. If you want longer backup cell life, fit a bigger cell.
https://thepihut.com/products/mini-rtc- ... spberry-pi
The money quote:
This is a Mini Super Capacitor Real-Time Clock module, designed for the Raspberry Pi. It can be used with an Arduino though!
This accessory plugs on top of your Pi's GPIO pins and contains a clock chip and a super-cap that allows your Raspberry Pi to remember the time!
Aside from not being able to see the underside of the module, it looks identical to the Chinese versions. So, yes, so far as I know, they're using supercaps. I agree that there is no charging circuit. This would be expected with a supercap. All the cap would need is a current limiting resistor. It's rechargeable batteries that need charging circuits.

As soon as Pi Hut has bare Pi Zeros back in, I'll get one of their modules (it's cheap enough) and get a look underneath.

Return to “General discussion”