Snippet - RTC SD2405

RTC SD2405

This is code for using DF Robots SD2405 Real-Time clock Module.
I based this off of EriSan500 (Eric)
It allows you to read the datetime and enables writing the datetime.


@ MikeCormier - Cheers Mike, i have one of those in my stash that DF just sent me :slight_smile:

Mike do you want to add this to:

sure. That will be helpfull.
Do you want me to add it or do you want to?

Your call, but I’ve added you as a developer to the project so you should be able to do as you wish.

I have no idea how to add the code so why don’t you do it.

I’ll build it as a driver and such and put it up. The SD2403 should be rather similar to this one as well.

The driver, open to suggestions, comments and improvements before I post it:

using System;
using Microsoft.SPOT.Hardware;
using GTI = Gadgeteer.Interfaces;
using GTM = Gadgeteer.Modules;

namespace Gadgeteer.Modules.DFRobot
    /// <summary>
    /// A SD2405 Real-Time Clock module for Microsoft .NET Gadgeteer
    /// </summary>
    public class SD2405_Real_Time_Clock : GTM.Module
        #region Constants

        private const ushort SD2405Address = 0x32;


        #region Fields

        private readonly GTI.I2CBus _i2C;
        private const int I2CClockRateKHz = 400;
        private const int I2CTimeout = 1000;
        private readonly Socket _socket;


        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public SD2405_Real_Time_Clock(int socketNumber)
            _socket = Socket.GetSocket(socketNumber, true, this, null);

            _socket.EnsureTypeIsSupported(new char[] {'I'}, this);

            _i2C = new GTI.I2CBus(_socket, SD2405Address, I2CClockRateKHz, this);

        /// <summary>
        /// Gets current Real Time Clock date time.
        /// </summary>
        public DateTime GetDateTime()
            I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(new byte[] {0x00});

            var returnedDateTime = new byte[7];

            I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(returnedDateTime);

            var readTransaction = new I2CDevice.I2CTransaction[] {write, read};

            // Lock the _i2C bus so multiple threads don't try to access it at the same time
            lock (_i2C)
                // Execute the transation
                _i2C.Execute(readTransaction, I2CTimeout);

            int sec = bcdToDec(returnedDateTime[0]) & 0x7f;
            int min = bcdToDec(returnedDateTime[1]);
            int hour = bcdToDec(returnedDateTime[2]) & 0x3f;
            int dayofmonth = bcdToDec(returnedDateTime[4]);
            int month = bcdToDec(returnedDateTime[5]);
            int year = bcdToDec(returnedDateTime[6]) + 2000;
            var dt = new DateTime(year, month, dayofmonth, hour, min, sec);
            return dt;

        /// <summary>
        /// Set Real Time Clock to passed Date Time.
        /// </summary>
        /// <param name="datetime">DateTime containing date time to set RTC to.</param>

        public void SetDateTime(DateTime datetime)
            var writeon1 = new byte[2] {0x10, 0x80};

            var writeon2 = new byte[2] {0x0F, 0x84};

            var sb = new byte[8]
                    0x00, this.decToBcd(datetime.Second), this.decToBcd(datetime.Minute),
                    this.decToBcd(datetime.Hour), this.decToBcd((int) datetime.DayOfWeek),
                    this.decToBcd(datetime.Day), this.decToBcd(datetime.Month),
                    this.decToBcd(datetime.Year - 2000)


            var writeon3 = new byte[2] {0x12, 0x00};

        private void WriteRegister(Byte[] value)
            I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(value);
            var writeTransaction = new I2CDevice.I2CTransaction[] {write};

            lock (_i2C)
                _i2C.Execute(writeTransaction, I2CTimeout);

        #region Methods

        private byte bcdToDec(byte val)
            return (byte) ((val/16*10) + (val%16));

        private byte decToBcd(int val)
            return (byte) ((val/10*16) + (val%10));


There are some other features in this chip which aren’t included here (alarms, 12/24 clock etc).

Hi everyone,

I would like to use SD2405 RTC module on NETMF 4.3, but I can’t find associated dll and code.
And I don’t know how to create its associated dll.

Does anyone can help me?

Thanks by advance,


Actually, it’s so old that this post was on the old tinyclr version of the forum The new codeshare location is

Hi andre.m and Brett,

Thanks to your quick reply. (I had to wait for 8 hours for my second post.)

My question was maybe unclear. I currently use this module on NETMF 4.2 and I would like to upgrade my project to NETMF 4.3. Unfortunately, I only found the driver of this module on NETMF 4.1 and 4.2:

So my question is: do you know if Gadgeteer 4.3 driver already exists? If it doesn’t exist, how can I manage to use this module on NETMF 4.3? I am not sure to know how to use the code you gave me with the module on my Fez Raptor mainboard.

Thanks by advance,


@ Laura - The 4.1 and 4.2 source and drivers are here but I’ll take a look at this tonight and see if I can cook you up a 4.3 driver. Is this on a breakout board or the DF Robot module?

@ Duke Nukem - Thanks a lot. What do you call a breakout board? I am looking for SD2405 RTC driver on NETMF 4.3.
Thanks for the link of source codes. I have already watched them and modify it on my NETMF 4.2 project. In fact, I had some problems with “hours” which was incorrect. I think it is because of the bit 12/24h which could be wrongly initialize in your code. That’s why I was also looking for the source code on NETMF 4.3. I don’t have my code now, but I will post it when I will have it.

Thanks again.