News:

Let's find out together what makes a PIC Tick!

Main Menu

DS3231 RTC Problems

Started by Colin G3YHV, Jun 07, 2021, 04:48 PM

Previous topic - Next topic

Colin G3YHV

Hi - I am using  ds3231 Precision (!) real time clock in a club project.
We have found a number of these keep poor time loosing a few seconds a day.
I am using the include file ds1307 Which Les wrote, and it seems to work with these chips.
I am only setting the hours/mins and seconds and ignoring the date etc etc.
Does anybody know if this works without setting all the variables  ?
Its possible the Chinese have flogged a load of fakes or duds ....
Colin G3YHV

top204

#1
With all the RTC devices I have used over the years, the time slippage is because of the 32.768KHz crystal, or, mainly, due to the two capacitors used with it.

What I do to find the correct capacitors is setup a program that transmits the current time every second, and receive it with a serial terminal that displays the time before the time data received, so you can see the time slippage over a few hours using the PC's more accurate clock. It is important to transmit the milliseconds part of the RTC's output as well, so you can see small slippages occuring over a shorter period of time.

Then change the 2 capacitors either side of the crystal and perform the test again. In most of the RTC devices I have used, 7pF capacitors normally do the trick, but it depends on what capacitance the crystal has internally as well. The capacitance of the crystal will be in its datasheet.

It takes time, but once known, the capacitors can be used all the time with the particular crystal. Also make sure the PCB has a good grounding around the crystal and capacitors and no data lines are beside them. If using a solderless breadboard, the time will never be accurate over a few days because of the capacitance of the breadboard itself.

If the RTC has a square wave output pin, this normally takes its 1KHz frequency from the crystal beind divided, so a frequency meter may be able to show how accurate the crystal is operating, but this type of test is not always accurate, It all depends on how the square wave is created within the device, and how "off" the crysal is, to show discrepancies once the 32KHz is divided down to 1KHz.

Colin G3YHV

Hi Les  -  the DS3231 is a rather different beast.
The Crystal is inside the actual chip and it has temperature compensation built in
It measures the temperature and select a internal  tuning capacitor every 64 seconds to trim the clock.
So it should be very accurate  -  but my chips are not!
The chip also has the temperature  available to the user  -  but only 3 deg c accuracy.
Colin
 

Gamboa

#3
Colin,

The DS3231 is a precision RTC with internal xtal and VCXO. I used it several years ago and had no accuracy problem. This chip is expensive. So if you bought it at a much lower price than Maxim sells it, it may be counterfeit. Check the supply chain.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DIM FechayHora[7]          AS BYTE     'Array que guarda la fecha y hora ss:mm:hh ds DD/MM/AA

  SYMBOL CONTROL_LECTURA_DS   %11010001  'byte de control del reloj ds3231 para leer
  SYMBOL CONTROL_ESCRITURA_DS %11010000  'byte de control del reloj DS3231 para escribir
  SYMBOL DIRECCION_DS %00000000          'byte que apunta al registro cero del reloj DS

'*************************************************************************
'** SUBRUTINA: escribir_ds3231
'** escribe en el reloj de tiempo real DS3231 la fecha y hora para actualizarlo
'**
'** Parametros de entrada: control_escritura_ds, direccion_ds, fechayhora
'** Parametros de salida:
'*************************************************************************

Escribir_DS3231:
 
 BUSOUT CONTROL_ESCRITURA_DS,DIRECCION_DS,[STR FechayHora]   
 DELAYMS 6 'Tiempo de grabación de la informacion en EEPROM
 RETURN
   
'** FIN SUBRUTINA: escribir_ds3231




'*************************************************************************
'** SUBRUTINA: Leer_DS3231
'** lee la hora del reloj en tiempo real
'**
'** Parametros de entrada: control_escritura_ds, CONTROL_LECTURA_DS
'** Parametros de salida: FechayHora
'*************************************************************************

Leer_DS3231:
    'Posiciono el puntero de lectura del reloj en el registro número cero
    'para leer desde esa posición en adelante siete registros  de la hora y fecha
    'reg0-> ss  segundos 00 a 59
    'reg1-> mm  minutos  00 a 59
    'reg2-> hh  horas    00 a 23 (o 0 a 12 AM PM)
    'reg3-> ds  día de la semana 1 a 7 (1 domingo, 2 lunes ...)
    'reg4-> DD  fecha día 1 a 31
    'reg5-> MM  mes de 1 a 12
    'reg6-> AA  año de 00 a 99
    'La posicion de los datos en la matriz es:
    'fechayhora[ss,mm,hh,ds,DD,MM,AA]
   
   
    BSTART                    ' envío Start
    BUSOUT CONTROL_ESCRITURA_DS ' Envío direccion del esclavo
    BUSOUT 0                    ' Envío direccion del primer registro
    BSTOP                       'envio Stop
'    'leo siete registros y los cargo en la matriz de hora y fecha
    BUSIN CONTROL_LECTURA_DS, [STR FechayHora]

  RETURN
   
'** FIN SUBRUTINA: Leer_DS3231
>>>>>>>>>>>>>>>>>>>>>>>
Regards
Gamboa
Long live for you

trastikata

If the crystal is built-in, there should be a register to adjust for crystal aging and mechanical stress due to soldering stress

In a RTC project sometime ago there was space on the board and I used a GPS module for synchronization and correction whenever the signal was available.

Colin G3YHV

Hi All. Thanks for your advice.
The device is being used in an amateur radio WSPR transmitter.
Our previous version did use GPS for the clock but the RTC would be a simpler
solution to our time keeping problem.
I see the device has an aging register that trims the crystal but the data sheet says
it should not be needed. I assume this register defaults to zero correction.
But surely this registers setting would be lost without the battery backup

Colin

top204

#6
A lot of the RTC devices have built in EEPROM for some registers, and EEPROM for the user to use.

I haven't looked at your device's datasheet, but make sure a tuning register is not being written too and check if a bit has to be set or cleared to enable its use. I got caught out with a Microchip RTC device, where a register's bit had to be set before it would use the battery backup, which is rather silly because all the other types I've used, auto detect the battery in place.

Stephen Moss

Is the DS3231 being powered by the battery for any significant amount of time, i.e. while the transmitter is switched off as opposed to the battery being a temporary backup for 20 minutes during a power outage?
If so and you have access to an an oscilloscope or DVM that can measure frequency to at least 50KHz I would add a pull up resistor to the 32KHz output pin and monitor what frequency it is running at both when mains powered and battery powered. Because it is possible that if the internal power management circuitry cannot maintain a constant XTAL voltage as the battery discharges that there will be a corresponding change to XTAL frequency which is responsible for the time slippage.

Colin G3YHV

Hi  -  I have been doing more tests.
The modules have a new battery but are running on the 3.3v pic supply and still loose time.
I have measured the 32.768 kHz square wave output and get the following results on five modules
32.694
32.714
32.750
32.698
32.684    I am sure these are reject chips being flogged by China -  you get what you pay for! :o
I will experiment with  adjusting the ageing register and see if I can trim them up far enough.
Colin

ranox

Colin,
I use a number of these devices, normally powered, so back-up battery seldom used.
Their time is WITHIN few Seconds after months :).
I have not trimmed them at all, but do read/log their internal temperature.
I purchased complete I2C modules (RTC, AT24C32 EEROM, with CR2032 battery etc) from Ebay at about AUD1.40 (in 2015)
- they list (https://www.ebay.com.au/itm/164809019189?hash=item265f620f35:g:bQUAAOSwobpgc~wp) today at AUD4
- they work well for me, including 32kHz & square wave outputs
Alan.  ;)

Giuseppe

Colin G3YHV where did you buy them? and how much did they cost you per module?

Colin G3YHV

These modules came from various ebay/ali express dealers.
I have tested 6 modules and the clock frequencies are way off.
32694,32714,32750,32698,32684 -  I only found one  that was spot on at 32768.
The best one I have is about 500 ppm low in frequency!
SO it seems these boards are failures being flogged of cheap  =  and no use at all !
Colin G3YHV

Giuseppe

You have to be lucky to find a good supplier. For example with the Hc12 I found that there are counterfeit cards around at low cost

RGV250

Hi,
I buy modules etc from eBay, Amazon and Ali Express etc, I use those to test the code and to see if it works as I want. If I decide to use it properly I then get it from Farnell or RS.
I did get caught out with Chinese ZIF sockets (branded 3M), I had unreliability issues and doubted my code. When I ditched the sockets it all worked OK.

Regards,
Bob

charliecoutas

Giuseppe

Were do you get your "good" HC-12's from? I love these and have several ticking away around the house. It will be good to know who is a reliable supplier and who is not.

Charlie

Giuseppe

Ebay seller is cayin35 i have ordered multiple times from him and he always sent me original hc12

charliecoutas


joesaliba

Quote from: Giuseppe on Jun 11, 2021, 07:06 AMEbay seller is cayin35 i have ordered multiple times from him and he always sent me original hc12

Yes, that is a very good seller.

shantanu@india

Colin,
I had used DS3231 based modules in bulk before and always found the timekeeping inaccurate.
The performance improved dramatically when I accessed the RTC on need-to-know basis. Suppose you need to timestamp an event. Access the RTC when the event occurs... avoid reading the RTC in tight loops unnecessarily.
Quite likely these are not original devices from Dallas and the I2C communication overhead consumes some 32.768kHz clock cycles illegally.
Regards
Shantanu

superpro

Quote from: Gamboa on Jun 07, 2021, 05:26 PM>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DIM FechayHora[7]          AS BYTE     'Array que guarda la fecha y hora ss:mm:hh ds DD/MM/AA

  SYMBOL CONTROL_LECTURA_DS   %11010001  'byte de control del reloj ds3231 para leer
  SYMBOL CONTROL_ESCRITURA_DS %11010000  'byte de control del reloj DS3231 para escribir
  SYMBOL DIRECCION_DS %00000000          'byte que apunta al registro cero del reloj DS

'*************************************************************************
'** SUBRUTINA: escribir_ds3231
'** escribe en el reloj de tiempo real DS3231 la fecha y hora para actualizarlo
'**
'** Parametros de entrada: control_escritura_ds, direccion_ds, fechayhora
'** Parametros de salida:
'*************************************************************************

Escribir_DS3231:
 
 BUSOUT CONTROL_ESCRITURA_DS,DIRECCION_DS,[STR FechayHora]   
 DELAYMS 6 'Tiempo de grabación de la informacion en EEPROM
 RETURN
   
'** FIN SUBRUTINA: escribir_ds3231




'*************************************************************************
'** SUBRUTINA: Leer_DS3231
'** lee la hora del reloj en tiempo real
'**
'** Parametros de entrada: control_escritura_ds, CONTROL_LECTURA_DS
'** Parametros de salida: FechayHora
'*************************************************************************

Leer_DS3231:
    'Posiciono el puntero de lectura del reloj en el registro número cero
    'para leer desde esa posición en adelante siete registros  de la hora y fecha
    'reg0-> ss  segundos 00 a 59
    'reg1-> mm  minutos  00 a 59
    'reg2-> hh  horas    00 a 23 (o 0 a 12 AM PM)
    'reg3-> ds  día de la semana 1 a 7 (1 domingo, 2 lunes ...)
    'reg4-> DD  fecha día 1 a 31
    'reg5-> MM  mes de 1 a 12
    'reg6-> AA  año de 00 a 99
    'La posicion de los datos en la matriz es:
    'fechayhora[ss,mm,hh,ds,DD,MM,AA]
   
   
    BSTART                    ' envío Start
    BUSOUT CONTROL_ESCRITURA_DS ' Envío direccion del esclavo
    BUSOUT 0                    ' Envío direccion del primer registro
    BSTOP                       'envio Stop
'    'leo siete registros y los cargo en la matriz de hora y fecha
    BUSIN CONTROL_LECTURA_DS, [STR FechayHora]

  RETURN
   
'** FIN SUBRUTINA: Leer_DS3231
>>>>>>>>>>>>>>>>>>>>>>>

you have the complete program for the microcontroller, which one do you use?