2

I've just obtained a DS3231 real time clock (RTC), and added it to my RPi 3b. It seems to work as far as keeping time, but I wanted more.

I'd like to be able to set alarms to trigger the INT/SQW pin on the chip (while retaining the basic timekeeping function I currently have), but I've been unable to find any way to do that with the toolset in RPi. I've installed python-smbus and i2c-tools as part of the RTC configuration. Have I overlooked something? - how should I go about reading & writing the DS3231's internal registers to set up the alarm function on my RPi 3b?

11
  • I played with DS3231 some three years ago. I found the drivers around then had too limited, (and only minimal functions like many other drivers). So I DIYed my own python library. Fast forward 3 years, now I am playing with Pico and plan to port my old package such as MCP23017, ADS1115 etc to Pico. In case you and other newbie readers are interested to know me the hobbyist developer learned how to do the job, I am writing an answer below. Comments and counter suggestions welcome. Cheers. – tlfong01 Apr 7 at 1:43
  • I remember 3 years ago the DT3231 libraries then were written in C/C++, me python guy found it hard to append more functions. But if you can now find any python package and wish to add more functions, my little DIY functions and design notes might help you modify any existing python or C/C++ packages. – tlfong01 Apr 7 at 1:58
  • 2
    @tlfong01: I appreciate the effort you put into your answer, but it doesn't actually address my question... with no offense intended, it strikes me as a stream of consciousness rather than an answer. My research suggests all of the code written to configure the ds3231 is written for the Arduino - which is baffling to me. However, I've found one resource that seems useful, and may help you in formulating a useful answer. – Seamus Apr 8 at 5:35
  • 1
    After reading through the resource I mentioned above, I think I understand more clearly what needs to be done in terms of setting the registers in the ds3231, but I remain at a loss in terms of how to do this from the RPi. – Seamus Apr 8 at 5:40
  • 1
    OK, so a work in progress - I'll leave you to it. However, I'm trying to move forward on this myself. I don't think now that I need an extraordinarily lengthy answer. It could be as simple as install pkg-X, write code in Python. import smbus, read register, write register, etc. Nothing I've tried so far is working. – Seamus Apr 8 at 7:18
1

Question

How to write a Rpi4B/Pico Thonny Python program to talk to the DS3231 RTC (Real Time Clock) to do things such as the following:

  1. Detect interrupts,

  2. Trigger on chip square wave signals.


Short Answer

  1. I have searched my 3 year old posts (over 50) in rpi.org.forum to refresh my memory on how I messed around with DS3231, listening to its interrupts, and program/trigger the on chip 32kHz square signals etc.

  2. My old posts are messily scattered in a couple of forums and intermingled with other I2C devices such as MCP23017 IOX and ADS1115 ADC. Porting to Pico is a good opportunity for me to tidy them up.

  3. The OP would like to be able to set alarms to trigger the INT/SQW pin, so I am jumping start to this part. I would first paste the design parameters in Appendix A below, then show a python function on how to program this pin. This double function pin is a bit tricky to handle, you might need to choose SQW mode, program the square wave frequencies, then go back to enable INT, and when INT comes, you need to switch to SQW mode to trigger it. In short, it is a bit tedious and easy to go wrong, so I gave up and tried other workarounds, ... :)

  4. DS3231 has two alarms, Alarm 1 and Alarm 2, as summarized in Appendix B. I am going to show how to set Alarm 1 for day, hour, min, sec.

  5. There are a couple of DS3231N moudles around in the market. For prototyping and evaluation, I would recommend to buy the most expensive, deluxe version, with a lot of pins to play with, address pins for multiple RTCs, and daisy chain I2C pins for easy extending signals. See Appendix C for more details


ds3231 RTC 1


  1. Now I have written a little python program to detect the addresses of the devices on the two I2C buses of Rpi Pico. DS3231 and 24C32 EEPROM occupy addresses 0x68 and 0x57.

ds3231 addresses

/ to continue, ...


Long Answer (TL;DR)

/ to continue, ...


References

(1) Search query: DS3231 tlfong01 Record - found 54 matches

(2) Python Program to Ping DS3231 and MCP23017

(3) DS3231 Python Programming Code

(4) DS3231 python Write Register Example

(5) DS3231 Old Posts Reference Links

(6) DS3231 i2cdetect record

(7) DS3231 driver vs dit programming

(8) DS3231 read register functions

(9) DS3231 Set alarm etc

(10) DS3231 python programming design notes

(11) DS3231 python programming design notes

(12) DS3231 python programming design notes

(13) DS3232 Square Wave Setup

(14) DS3231SN Realtime Clock with AT24C32 EEPROM Module - AliExpress US$1.28

(15) Using a $1 DS3231 Real-time Clock Module with Arduino (and which components to remove) - Underwater Arduino Data Loggers

(16) MicroPython class I2C

(17) MicroPython Class RTC

(18) Build Programmable Time-based Switches Using a Real Time Clock (Arduino) - Raymond Genovese, All About Circuits, 2016apr07


/ to continue, ...


Appendices

Appendix A - DS3231 INT/SQ pin programming design notes

int/sq pin


Appendix B - DS3231 Alarm programming design notes

alarm 1


Appendix C - Real Time Clock Module

(14) DS3231SN Realtime Clock with AT24C32 EEPROM Module - AliExpress US$1.28

rtc module


Appendix D - Schematic and Pullups to remove to reduce capacitance to < 400pF

Notes

  1. I2C bus uasually has a capacitance limit of 400pF, exceeding which might cause I/O transmission errors.

  2. R4B I2C pins already have strong pullups of 1k8, so it is a good idea to remove the DS3231 and other devices' on board pullups of usually 4k7, sometimes 10k

  3. It is also a good idea of keeping the I2C wiring short, less than 30cm if possible.

  4. It is also a good idea, especially during prototyping or troubleshooting, to use low frequencies for I2C, say 100kHz or even down to 50kHz, so that resulting impedance is less than 400pF.


ds3231 schematic


Appendix E - Summary of MicroPython I2C I2C and RTC class and methods used to control DS3231

MicroPython class I2C

1. Example usage:

from machine import I2C i2c = I2C(freq=400000)
i2c.scan()
i2c.writeto(42, b'123') i2c.readfrom(42, 4)

2. Constructors

class machine.I2C(id, *, scl, sda, freq=400000)

3. General Methods

I2C.init(scl, sda, *, freq=400000) I2C.scan()

4. Standard bus operations

I2C.readfrom(addr, nbytes, stop=True, /) Read nbytes from the slave specified by addr. If stop is true then a STOP condition is generated at the end of the transfer. Returns a bytes object with the data read.

I2C.readfrom_into(addr, buf, stop=True, /) Read into buf from the slave specified by addr. The number of bytes read will be the length of buf. If stop is true then a STOP condition is generated at the end of the transfer. The method returns None.

I2C.writeto(addr, buf, stop=True, /) Write the bytes from buf to the slave specified by addr. If a NACK is received following the write of a byte from buf then the remaining bytes are not sent.

I2C.writevto(addr, vector, stop=True, /) Write the bytes contained in vector to the slave specified by addr. vector should be a tuple or list of objects with the buffer protocol. The addr is sent once and then the bytes from each object in vector are written out sequentially. The objects in vector may be zero bytes in length in which case they don’t contribute to the output.


MicroPython Class RTC

class machine.RTC(id=0, ...) Create an RTC object. See init for parameters of initialization

RTC.init(datetime) Initialise the RTC. Datetime is a tuple of the form: (year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

RTC.now() Get get the current datetime tuple.


Appendix F - Old Rpi python program for DS3231

Update 2021apr19hkt1229

The program list uploaded here yesterday is too long and hit the SE's 30k words limit. The listing was ported from Chinese Windows 10 and edited to fit RpiSE format but stil has wrong formatting, funny charaters here is there. So I put the completed lising as a PenZu lab log, with this link:

DS3231 Old 2012 Penziu Listing

Notes:

  1. The program was written in pure functional programming style, with no global variables and no side effect. So it is easy to copy any DS3231 functions with their independent higher level variables, and paste as a module/package. In other words, never mind the pretty print, user menu, mux functions.

/ to continue, ...


12
  • 1
    FWIW, this is the module I need to program. – Seamus Apr 8 at 7:21
  • Ah, your DS3231 RTC module is a "mini" version. It is small and therefore good if you want to save space. For prototyping, my module is better for the following reasons: (1) with address jumpers A0, A1, A2 to set 8 device address, so to use more than a DS3231 on the same bus. (2) With an EEPROM to store data. (3) Generously spaced components to easily modify circuits. eg. remove pullup resistors, modify battery charging options. See Ref 15 for more details. – tlfong01 Apr 8 at 14:47
  • You recommended tutorial by All About Circuits is as good as mine. So I have included in to my reference list (Ref #18). Cheers. – tlfong01 Apr 9 at 13:44
  • 1
    You still have / to continue, ... remarks in several places, but since it's been over a week since your last edit, I assume you're finished. I don't understand how you mis-interpreted my question. It's not a long, detailed question - but you even summarized it incorrectly. With no disrespect intended, this answer is not helpful. I'd like to delete my question. I don't want to impact your efforts, so perhaps you could post your own question, and then copy & paste your answer there? – Seamus 2 days ago
  • Ah I did hesitate to update with this info. seeedstudio.com/…. I now have 6 or more Pico in hand, and 10 more are on their way from RS. I also have perhaps 10 Pico breakout boards in hand, mostly by perorder,. as soon as the morning I read the Google alert. I do have 3 Pico and 6 breadout boards from Seeed. RTS is not urgent for me, so I might just order them after I see the reviews. I also have more than 10 breakout boards from Piromori. Perhaps they have already been making RTC boards for Pico. – tlfong01 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.