Reading DS3231 RTC in Node Red

Is there a simple way to input time to display on the dashboard from the DS3231 RTC module? Using the I2C IN node, I can see and read what looks like seconds and minutes data using the 00 and 01 commands in those nodes. The SECONDS update to a new number every second, but it isn't decimal. The seconds start at 0 and count to 9 then jump to 16. It displays erratically up to 100 and then the minutes digit changes.

So now I suppose I have to decode this somehow to decimal to display a real looking clock on the dashboard.

This seem very awkward. Is there a simpler way?

I think that is called binary coded decimal. Been ages since I’ve seen it but I’m sure google will help

Looking at the datasheet the upper 4 bits are the 10 seconds and the lower the seconds, same for the minutes etc.
I don't know Javascript good enough for bit manipulation.
In C we AND the right 4 bits to get the seconds value and then shift the left 4 bits to right to get 10th seconds value.

1 Like

Yes, I recognize it as HEXADECIMAL, and I could certainly decode it in C but that really wasn't my question.

This was very easy to do on an Arduino so I'll rephrase my question: isn't there an easier way to read this very common device (DS3231 RTC) in NODE RED? Surely someone has done this before.

Try this in a function node

var val = msg.payload
msg.payload= ((Math.floor(val/16)*10)+val%16)
return msg;

I guess I gotta do what I gotta do.

Thanks for that snippet edje11 ... it decodes and displays perfectly now.

1 Like

Did you check NPM for a node.js package ? Apparently there are two of them who are up to the job.

I did search, but nothing returned. Could you be more specific for my search?

Only today I could have a better look on them. Both are lacking a Github repository. It seems to me they are abandoned. You probably did the right choice on handling them directly with I2C node, reading the internal registers.

Well, I seemed to have really messed something up. All was working well until I followed some tutorials about how to set the HWCLOCK.

Now, when I scan using 'i2cdetect -y 1' it shows ports 57 and a UU in port 68 where the DS3231 is supposed to be. The 'UU' is supposedly a conflict but the commands 'sudo hwclock -r' and sudo hwclock -w' entered into the terminal seem to work.

Yet using the i2c Scan node, a port 87 appears. I get errors when I try to use the 'i2c in' nodes to connect to 57 or 68. It will connect to the phantom port 87 but I only receive '255' in the msg.

Can someone tell me what I've messed up here?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.