MyRaspberryAndMe

Tinkering with Raspberry (and other things)

Real Time Clock (Hardware)

1 Comment

The Raspberry Pi comes without a real time clock. The official FAQ says so and we have to go with it, although on the forums many people complain and want a RTC added to the Raspberry. Well, there is no reason to do so. There are lots of free connections on the Raspberry that could be used to connect a clock. And hey, that is where the fun starts, anyway.

Do I need an RTC?

Well, that depends. So in most cases a plain “No” would be the correct answer. If you have your Raspberry connected to a network all the time and this network has access to the internet there is absolutely no reason (apart from the tinkering fun) to add an RTC. The Raspberry uses NTP, the Network Time Protocol, the get the correct time from the internet. Every time  the Raspberry boots, the time is set.

Additionally there is something called the “fake hardware clock”. But we need to start somewhere else to understand the principle:
When the Raspberry boots, it starts with a default system time (which, of course, has nothing to do with the real time). It then tries to get the time from an NTP server and, when successful, overwrites the system time with this current time. Every hour a script is called that saves the current time in a file. This is the fake hardware clock.

So, if the Raspberry starts without having access to a time server, the system takes the fake time from this file and sets it as the system time. Now why would one do that? Easy answer: to get the timestamps on files (amongst other things) to never be dated back to the past. This would cause lots of problems with log files or when you were searching for the most recent file in any directory.

Whaaaat?
OK, an example: The Raspberry gets a current time from NTP, say 2013/06/28, 2:44pm. This time is the new system time for the internal clock. Every hour the time is saved to a file. Now we shutdown the Raspberry, disconnect the network and boot again.
This time there is no NTP server and so the last “good time” is taken and your Raspberry thinks it is now 2013/06/28, 2:50pm. All files you create, modify etc. will have a timestamp that is incorrect, but not in the past (from the “when was I last running point of view”).
If there were no fake time, the system time would be set to a default time, that is January the 1st, 1970. If you were to modify any file, its timestamp would read “1970” and that would be an earlier date than the previous version of the file. Problem! Unless you are Doc Emerett Brown. Or Marty McFly.

It would make sense to add an RTC if the Raspberry does usually not have a connection to the internet or any other NTP server. And, of course, you rely on correct timestamps. Another very good reason for having an RTC is even simpler: it’s fun to build one and integrate it with the Raspberry.

I want one!

Yes, me too. And it’s very easy to do. The RTC boards you can buy are usually based on the DS1307. This integrated circuit is a real time clock that is buffered by a small coin cell. One of the types you will find in kitchen timers, your garage door opener etc. The DS1307 needs only 4 wires to work with the Raspberry, because its protocol is based on the I2C standard, a two-wire communication between electronics. Add two lines for power supply and ground and that makes four. With some modifications in configuration files the Raspberry “understands” I2C, so this is the way to go.

Now let’s dive into Electronics.

If you look at the description for the DS1307 and scroll down that page a little bit, you will see a typical operating circuit. The lines called SCA and SDL are the I2C connections that “talk” to the Raspberry. There are two resistors (RPU) that connect these outputs to the supply voltage. They are called “Pull Up Resistors” (German language here) and they are responsible for the SDA/SCL lines being on 0V or 5V level. I won’t go into detail here, because then I would have to cover “open collector circuits” (for German readers) and basics of Transistor operations (German link).

The Raspberry Pi is running on 3.3 Volts. So what we definitely do not want is using 5 Volts on SDA/SCL. As far as I know and what I see from the Raspberry PCB I am pretty sure that even 5 Volts would not hurt, but I won’t give it a try. So we are discarding those pull-up resistors, the Raspberry will detect and interpret the voltage levels on SDA/SCL perfectly OK as the Raspberry has some internal pull-up resistors an SDA/SCL.
By removing those pull-ups, we force the RTC module to work with 3.3V on its SDA/SCL lines even though we connect the module to 5 Volts as Vcc.

This makes things easier as we need only 4 parts: the DS1307, a Crystal (32.768 kHz), a Coin Cell and a Coin Cell holder. And some PCB, wires and the tools for soldering the whole thing together. Here is a quick sketch of the schematic:

rtc_Pi.jpg_blue

Nothing more is needed. On a breadboard this would look something like this (I have added a blocking capacitor between VCC and GND):

breadboard_rtc

For those interested, I made that image with Fritzing, a cool software for developing electronics stuff.

Where can I buy these things?

There are numerous sources where you can buy RTC modules from. In the US this would be Adafruit for example, in Germany Watterott has loads of stuff. You can find these modules on Ebay, also. One important thing is to remember that we do not want the pull up resistors. On most of the pre-assembled modules these are present. So take a close look if you can identify these resistors and give a quick thought about how easy you can desolder/remove them. Second, read the description carefully. Not all modules offered “speak” I2C! If you are not sure, go with Adafruit’s model. They (that would better be “she”. Sorry, Lady Ada) even mention this in their tutorial.

Update (02/07/2013): Example RTC from ebay

I ordered a tiny RTC module, which, in fact, is called “Tiny RTC I2C module”. It has, as I have mentioned, those pull-up resistors on board so I needed to identify and remove them. This is fairly easy as we know to which pins these resistors are connected. So grab your multimeter and measure where there is a connection between the pins of the IC and one of the resistors. In the picture below you can see the modules. The resistors we want out are R2 and R2 just at the bottom.

rtc_tiny

The actual module is just 28×26 mm in size and has a coin cell attached to its back.

From the things I had lying around I made a quick connector to fit on the Raspberry. It does not look very nice but this is prototyping, all right?! You only need (check the breadboard picture above) Vcc, GND, SDA and SCL.

What next?

This has been the hardware part. We need to make this RTC module work with the Raspberry on a software basis, too. This will have to wait until the stuff I have ordered arrives. In the meantime I am trying to make an RTC module with SMD parts that is so small that it can be put directly onto the Raspberry’s GPIO pins. Yes, I know, one can buy these too. But I want to do it by myself and, in doing so, checking out how Fritzing’s PCB service works, if I am too old to hand-solder SMD parts and if I am still able to route connections in my head without an autorouter.

Advertisements

One thought on “Real Time Clock (Hardware)

  1. Pingback: Real Time Clock (Software – I2C) | MyRaspberryAndMe