Skip to main content

How to Set up a Real-Time Clock in Embedded Systems

This guide shows how to set up a real-time clock (RTC) in embedded systems with the ROCK 4 SE to allow real-time monitoring with no main power or connection to the internet.

Learn how to set up the real-time clock (RTC) on the ROCK 4 SE in just 10 steps and learn how to use a Serial-to-USB cable to access the console for both boards. The officially supported operating system, Debian Bullseye (Desktop) supports these features.

The ROCK 4 SE (220-9536) and ROCK 4C+ (249-3158) single board computers come with a built-in Real Time Clock (RTC) function as standard, which, when set up, can keep accurate time even when not connected to a network time service. They also have a Serial UART attached to the GPIO header for Serial Console access to the operating system for monitoring and debugging.

Rock - Real-Time Clock

Note: The ROCK 4C+ does not have the RTC battery connector fitted, but it does have the Serial UART connected to the GPIO header.

What is a real-time clock (RTC) in embedded systems?

A real-time clock (RTC) is used in embedded systems to keep track of the precise time and date even when the device is not connected to the network or the main power is off. The RTC is powered by its own internal lithium battery, and even if the power of the system is turned off, the RTC clock keeps running.

Applications of the real-time clock on the ROCK 4 SE

What are the applications of RTC?

The RTC is widely used in various applications, including industrial manufacturing, commercial machine maintenance, healthcare machine management, agriculture installations, and many more. The ROCK board, with its RTC capability, can be integrated into any electronic device where real-time monitoring is required.

Some applications of the RTC include:

  • Scheduled maintenance of industrial and commercial machines
  • Manufacturing robots
  • Street light management
  • Traffic lights signals
  • Speed cameras
  • Remote agricultural installations
  • Laptops, PCs and tablets
  • Digital cameras
  • Healthcare devices
  • Etc.


So, let’s discover how to set up the RTC on the ROCK 4 SE and start tracking time!

Difficulty: Easy Time: 1 Hr Steps: 11 Credits: None License: None

Parts Needed to Get Started

  • ROCK 4 SE Single-Board Computer (220-9536)
  • OKdo Multihead USB Quick Charge PSU (243-6356)
  • Ttl-232R-Rpi Debug Cable For Raspberry Pi (767-6200)
  • RTC Battery - CR2032, 3.0V Battery with lead and XH1.25mm connector
  • 32 GB MicroSDHC Card Class 10
  • Host computer Windows/Mac/Linux
  • Ethernet cable

Step 1: Real-Time Clock

Most desktop and laptop PCs have a battery-backed RTC built into the motherboard, but many SBCs do not. The ROCK 4 SE & ROCK 4C+ both have RTC functions supported by the PMIC, but only the ROCK 4 SE has an RTC battery connector located on the underside of the board. It requires a battery (not supplied) of the type as shown below:

  • Type: CR2032
  • Voltage: 3.0V
  • Connector: XH1.25mm

RTC Battery

Tip: RTC batteries are widely available – make sure yours has a connector with the correct polarity.

Step 2: Battery Installation

Ensure the ROCK 4 SE is powered off before connecting the battery to the battery connector terminal.

Ground (Black lead) is nearest to the USB-C socket (Top Left), and

Positive (Red lead) is nearest to the HDMI connector (Top Right), as viewed from the underside of the board below:

RTC Battery connected to Rock Board

Step 3: Operating System Setup

Set up your operating system for the ROCK 4 SE with Debian Bullseye. There are separate Getting Started instructions on how to do this here:

To obtain an accurate time setting, the ROCK 4 SE needs to be connected to the internet using either Ethernet or WiFi. Once the RTC has been set up, networking can be disconnected, and the time will be kept from the RTC.

Step 4: Set the timezone

Set up the time zone. A list of available timezones can be displayed by running the following command in the Terminal:

timedatectl list-timezones

This example shows setting the timezone to London, UK time zone. The system automatically knows that it is currently in British Summer Time (BST).

Tip: Use TAB completion by typing the first few letters of the timezone, then press the TAB key.

sudo timedatectl set-timezone Europe/London

View the clock settings with the timedatectl command:

timedatectl

When the system starts up, the system clock may not have synchronised – System clock synchronised: no.

Timezone

Step 5: Sync clocks

Wait until the System clock has synchronised. This can take five minutes or more. Keep refreshing timedatectl until System clock synchronised is marked yes.

Sync Clocks

After the system clock has synchronised, run the following command to write the system clock time to the hardware clock:

hwclock -r

Now, if you run timedatectl again, you should find that the Universal time (UTC) and RTC time are synchronised. It is recommended to run the RTC in UTC time, i.e. with RTC in Local TZ: no.

If you power off and disconnect networking, you can now run the system without being connected to a network, and the system will keep accurate time from the hardware clock.

RTC in local TZ set to no

Step 6: Serial Console

Both the ROCK 4 SE and ROCK 4C+ have a Serial UART connected to the GPIO header, so you can attach the board to a host PC using a USB-to-Serial cable and Serial Console application. This allows access to the boot sequence and kernel ring buffer output as the board boots up for monitoring and debugging, and once the system has booted, you can log in to the shell from the console.

Step 7: USB-to-Serial Cable

There are many USB-to-Serial cables available on the market. The ROCK board’s UART operates at a data rate of 1.5Mbps, so the cable must be able to support this. 3.3V cables based on the FTDI FT232R USB to Serial UART are suitable, for example,this one (767-6200) .

Warning: Some USB-to-Serial cables operate at 5.0V – DO NOT USE these as they will damage your board.

USB to Serial Cable

Step 8: GPIO Connections

The ROCK’s serial console is exported on UART2 by default, which is connected to the following pins on the GPIO header. With the board powered off, connect the cable as shown below:

ROCK GPI Pin GPIO Function Cable Connector Cable Function
Pin 6 GND Black GND
Pin 8 UART2_TX White RXD
Pin 10 UART2_RX Green TXD
Not Connected - Red 5V Power

Tip: Tape the Red power terminal (5.0V) on the cable safely away so that it cannot accidentally touch any of the GPIO pins, as this will damage your board.

Cable connected to board

Step 9: Serial Terminal

Several Serial Terminal applications are available that run on the host PC. We used Minicom on a Debian Linux host.

Install Minicom with:

sudo apt install minicom

To run Minicom without root privileges, make sure that your user is in the dialout group:

groups

Groups

If not, run the following command to add your user to the dialout group, then log out and back in again:

sudo usermod -aG dialout $USER

With the USB-to-serial cable plugged into your host, check which serial device it is attached to:

sudo dmesg

In our case, the device is on ttyUSB0.

New USB Serial Device

Create a minicom configuration file in your home directory named .minirc.rock (note the preceding dot) with the following contents:

pu port /dev/ttyUSB0
pu baudrate 1500000
pu bits 8
pu parity N
pu stopbits 1
pu rtscts No

Now start minicom in a Terminal on the host with:

minicom rock

You should see output similar to the below:

Start minicom

Tip: Hardware flow control “rtscts” must be set to “no” to allow keyboard input.

Now, power on the ROCK. The U-boot messages will stream through the console, then Linux will initialise, and the kernel boot messages will appear, ending with a login prompt.

Step 10: Log in

When the OS has finished booting, you may have to press “enter” to make the login prompt appear.

At the prompt, log in with username rock, and then you can run bash commands in the shell. There may be times when keyboard input is interrupted by kernel messages, so just press “enter” to return to the prompt and repeat the command.

Tip: Debian's default username is rock with password rock – we advise changing this to a more secure password.

Login

Summary

This guide showed how to set up and configure the RTC so that your ROCK 4 SE board can keep accurate time even when it is not connected to the internet or a network with time services.

This is necessary for stand-alone projects that use time and date information. It is also important for some cloud services that use access tokens which contain time stamps which must be set correctly.

For users who want to get under the bonnet of Linux on the ROCK 4 SE or ROCK 4C+, we also showed how to use a USB-to-Serial cable to access the ROCK’s serial console. This gives access to the boot sequence and kernel messages. It also provides a way to log into the OS from a host PC without having a monitor attached to the ROCK or when SSH access is not possible.

Reference:

I'm an engineer and Linux advocate with probably more SBCs than a Odysseus moon lander

Comments