Connecting your Raspberry Pi with a real-time clock

Lead Image © Joanne Weston, 123RF.com

Keeping Time

Your Raspberry Pi comes with many powerful components, but it doesn't have a built-in clock. If your project needs to keep time, and you don't have a reliable Internet connection for NTP updates, you'll need a real-time clock.

This article is the first of a bimonthly series of columns talking about basic electronics, software, and connecting things to your Raspberry Pi and Arduino. My goal is to show you how to extend the reach of your small computers using sensors, motors, actuators, and other components to sense and interact with your environment.

In this column, I show you how to set up your Raspberry Pi with a real-time clock. A real-time clock is necessary for any project requiring accurate timekeeping, especially when you don't always have an Internet connection – for example, a Raspberry Pi set up in a balloon or a Pi working in the remote Caribbean, as in Project Curaçao.

Introduction to Real-Time Clocks

One of the components left off of the Raspberry Pi for cost reasons is a real-time clock (RTC). The RTC will continue keeping time via a battery, even when the Raspberry Pi is shut off. The Raspberry Pi has the ntp daemon software (ntpd), which will periodically sync the Raspberry Pi clock with external timekeeping, but if you don't have a constant network connection, the daemon doesn't do you much good.

Furthermore, if your Raspberry Pi boots up without a network connection, it will set the system clock to the last time it had before it was shut down. The solution to all these problems is to add an RTC to the Raspberry Pi. From a software standpoint, the Raspberry Pi offers two major ways to use the RTC. The first way is to read and write to the clock directly from your user program. The second way is to integrate the clock into the operating system – a significant undertaking that is more than I can cover in this column. I'll stick with the first way and show you how to read and write to the clock directly. The examples in this article use the Adafruit DS1307 breakout board RTC modified to work with the Raspberry Pi.

What is the I2C Bus?

An I2C bus is often used to communicate with chips or sensors that are on the same board or located physically close to the CPU. I2C stands for standard Inter-IC device bus. I2C was first developed by Phillips (now NXP Semiconductors). To get around licensing issues, the bus is often called TWI (Two-Wire Interface). SMBus, developed by Intel, is a subset of I2C that defines the protocols more strictly. Modern I2C systems, including the Raspberry Pi, take policies and rules from SMBus, sometimes supporting both with minimal reconfiguration needed.

I2C provides good support for slow, close peripheral devices that only need to be addressed occasionally. For example, a temperature measuring device will generally only change very slowly, so it is a good candidate for the use of I2C, whereas a camera can generate lots of data quickly and potentially change often.

I2C uses only two bidirectional open-drain lines. (Open drain means the device can pull a voltage down to ground but cannot pull the line up to the supply voltage [Vdd] – hence the open-drain in the name.) Thus, a requirement of the I2C bus is that both lines are pulled up to Vdd (see the "Pulling a Line" and "Vcc and Vdd" boxes). This is an important concept, and not properly pulling up the lines is the first and most common mistake you make when you first use an I2C bus. More on pull-up resistors later in the next section. The two lines are the SDA (Serial Data Line) and the SCL (Serial Clock Line).

Vcc and Vdd

Vcc (5V on the Rasp Pi) and Vdd (3.3V on the Rasp Pi) both mean "voltage in." GND and Vss also generally both mean ground. There are historical differences, but today, Vcc usually is one power supply, and if there is a second, it is called Vdd.

When the I2C bus is used on the Raspberry Pi, the Pi acts as the master, and all other devices are connected as slaves (Figure 1).

Figure 1: An I2C bus with one master (the Raspberry PI), an Analog to Digital Converter (ADC), a Digital to Analog Converter (DAC), and an Arduino as an I2C slave. Rp are the pull-up resistors. (Wikipedia)

Pulling a Line

A voltage is a measure of how much potential charge is on a particular connector or line. A high voltage (e.g., 5V) means lots of charge potential, and a low voltage (e.g., 0V and ground [GND]) means little charge potential. Pulling a line to 5V means connecting the line (usually through a resistor) to limit current to 5V. For a bus such as the I2C, you can't just tie the line directly to 5V. You must use a resistor. If you use a resistor, then you can still take the line to ground without shorting your power supply.

The two types of devices you can connect to an I2C bus are master devices and slave devices. Typically, you have one master device (the Raspberry Pi, in this case) and multiple slave devices, each with their individual 7-bit address (e.g., 0x68 in the case of the DS1307). It is possible to use 10-bit addresses and multiple-master devices, but I won't cover them in this column.

The I2C protocol uses three types of messages:

  • Single message, where a master writes data to a slave;
  • Single message, where a master reads data from a slave;
  • Combined messages, where a master issues at least two reads and/or writes to one or more slaves.

Lucky for you, most of the complexity of dealing with the I2C bus is hidden from the user by drivers and libraries.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content