Built-in project · Arduino Uno R3

PCF8563 clock on an Arduino Uno

A PCF8563 real-time clock on the Uno’s I2C pins, read with RTClib: it sets the time if the VL flag says it was lost, then prints the date and time every second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $29. On Mokxi they're free.

PCF8563 clock · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Sensors
  • School
  • Using the Real-time clock
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

A PCF8563 real-time clock on the Uno’s I2C pins, read with RTClib: it sets the time if the VL flag says it was lost, then prints the date and time every second.

Arduino Uno R3 runs the sketch below. The circuit around it: Real-time clock, PCF8563. delay waits 10 milliseconds. Serial.begin opens the serial port at 9600 baud.

Parts list

2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Real-time clock, PCF8563 pin VCCArduino Uno R3 pin 5V
Real-time clock, PCF8563 pin GNDArduino Uno R3 pin GND
Real-time clock, PCF8563 pin SDAArduino Uno R3 pin A4
Real-time clock, PCF8563 pin SCLArduino Uno R3 pin A5

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
/*
 * A PCF8563 real-time clock on the Uno's I2C pins, read with RTClib.
 *
 * The PCF8563 answers at 0x51. Bit 7 of its seconds register, VL, says the
 * time can no longer be trusted (a fresh chip, or one whose cell ran flat):
 * RTClib's lostPower() reads it, and adjust() clears it by writing the time.
 *
 * Wiring: VCC to 5V, GND to GND, SDA to A4, SCL to A5.
 */
#include <RTClib.h>

RTC_PCF8563 rtc;

const char *const DAYS[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("No PCF8563 at 0x51: check SDA and SCL");
    while (true) delay(10);
  }
  Serial.println("PCF8563 found at 0x51");
  if (rtc.lostPower()) {
    Serial.println("VL is set: the time was lost, so it is set to when this sketch was built");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  rtc.start();  // clears STOP, in case a sketch before this one stopped it
}

void loop() {
  DateTime now = rtc.now();
  char line[24];
  snprintf(line, sizeof line, "%04u-%02u-%02u %02u:%02u:%02u", now.year(), now.month(), now.day(), now.hour(),
           now.minute(), now.second());
  Serial.print(line);
  Serial.print(' ');
  Serial.println(DAYS[now.dayOfTheWeek()]);
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("No PCF8563 at 0x51: check SDA and SCL");
    while (true) delay(10);
  }
  Serial.println("PCF8563 found at 0x51");
  if (rtc.lostPower()) {
    Serial.println("VL is set: the time was lost, so it is set to when this sketch was built");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  rtc.start();  // clears STOP, in case a sketch before this one stopped it
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  DateTime now = rtc.now();
  char line[24];
  snprintf(line, sizeof line, "%04u-%02u-%02u %02u:%02u:%02u", now.year(), now.month(), now.day(), now.hour(),
           now.minute(), now.second());
  Serial.print(line);
  Serial.print(' ');
  Serial.println(DAYS[now.dayOfTheWeek()]);
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.