Raspberry Pi boards don’t have real-time clocks (RTC) on board.  When the board is powered-off, time stands still, meaning if you power off your Pi at 14:45 and reboot it an hour later, the time will read 14:46 instead of 15:46.  Network-connected Raspberry Pis can use the Network Time Protocol (NTP) to set the correct system time when the board boots, but not all Raspberry Pis use network connections.

Unlike the Raspberry Pi (which omitted the RTC to save cost and power), many electronic gadgets that interface with a Raspberry Pi do contain clocks, like  cameras and GPS units.    If your Pi project needs a true and accurate system time, simply “borrow” the current time from an attached device.  Here’s a simple technique for using digital cameras.

gphoto2

gphoto2 is a popular Linux tool for controlling cameras over USB, and lots of people are building projects that use a Raspberry Pi, gphoto2, and a digital camera.   gphoto2 cameras that support remote control and either the “All” or “DateTime” features can be used.  Test your camera by running:

gphoto2 --get-config datetime

When gphoto2 queries the camera for date and time, it will return a unix epoch time, which does not contain timezone or daylight savings information.  Some cameras can compensate for daylight savings, but may still return standard time when queried (like the Nikon D5000).  gphoto2 will use the current system timezone to adjust the camera’s epoch (aka “Current”) to local time to create the “Printable” output:

Label: Camera Date and Time                                                    
Type: DATE
Current: 1365443288
Printable: Mon Apr  8 10:48:08 2013

Bash

Here’s a simple Bash script that will set the current system time to match the current camera time:

Make the script executable (chmod +x camdate.sh) and add it to /etc/rc.local to run it at boot time.  The script must be run as root/sudo because only the root user has permissions to modify the system time.

Java

Included as part of the examples in the libgphoto2-jna source is a Java application called ‘camdate’, which will set the system time using libgphoto2 and the libgphoto2-jna Java classes.

Here is a script which will download, build, and install the CamDate tool on a Raspberry Pi:

If you would prefer not to install the development packages (git, ant, jdk) on your Raspberry Pi, you could always build the CamDate jar on another system, in which case you would only need the libgphoto2-2, openjdk-7-jre, and libjna-java packages installed on the Raspberry Pi.

CamDate must be run as root/sudo because only the root user has permissions to modify the system time.

 

Tagged with →  
Share →

3 Responses to How to Use a Camera to Set the Time on a Raspberry Pi

  1. m says:

    Is there any reason not to use NTP which is installed by default of RPI?

    • abythell says:

      Network-connected Raspberry Pis can use the Network Time Protocol (NTP) to set the correct system time when the board boots, but not all Raspberry Pis [have] network connections.

      No Internet = No NTP!

  2. Simon N says:

    This worked for me! If you have a camera with GPS then the set time will be even more precise!
    Thanks for posting

Leave a Reply

Your email address will not be published.