The Raspberry Pi itself doesn't bring a hardware clock. It's assumed that a Pi always has a internet connection to get the current time. Since we want to use OTCamera in the field and thus may not have a Wi-Fi connection to access internet, we'll need a dedicated hardware clock (i.e. real time clock or RTC). It contains a backup battery (a coin cell) to keep track of time. The Pi will use the hardware clock time to set the system time.
There are several RTC chips out there. There are even several RTC modules that are specifically designed to use with a Pi. One of the most precise RTC chips is the DS3231. We are using a Adafruit PiRTC - Precise DS3231 Real Time Clock for Raspberry Pi.
Adafruit also provides a good (but little bit outdated) tutorial on how to use a RTC on the Pi. The follwing description is based on that tutorial.
First, we'll need to enable I2C to communicate with the RTC.
sudo raspi-config and navigate to:
- Interface Options → I2C → Yes
Or just use the command-line version of raspi-config:
sudo raspi-config nonint do_i2c 0
Let's install and use
i2c-tools to see if the RTC works:
sudo apt install i2c-tools -y
sudo i2cdetect -y 1
You should see an output of several lines containing
68 in one of them.
We now need to add a device tree overlay in
/boot/config.txt by adding the highlighted line at end of the file:
|/boot/config.txt (end of file)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
I'm not using the DS3231
If you are using a different RTC than the DS3231, check out the Adafruit tutorial.
In a nutshell: Use
After rebooting the Pi (
sudo reboot) you can run
sudo i2cdetect -y 1 again.
68there should be a
Now we'll disable the fake hardware clock:
sudo apt remove fake-hwclock -y
sudo update-rc.d -f fake-hwclock remove
sudo systemctl disable fake-hwclock
Additionally, we'll need to comment out some lines in
sudo nano /lib/udev/hwclock-set and add
# at the beginning of the lines:
1 2 3 4 5 6 7 8 9 10 11 12
The Pi should now be able to communicate with the RTC. Let's try it:
sudo hwclock -r
The Pi should now have automatically synced the time. If not, check if the Pi knows the correct time and sync it once:
sudo hwclock -w
Your OTCamera should now be able to keep track of time without any USB power connected and without access to the internet. We recommend booting your OTCamera once within Wi-Fi range before each recording to update the time, although the DS3231 is fairly accurate.