Let's add a real time clock so we can timestamp our captured footage.


Joining us here? Make sure you've got your Raspberry Pi's hardware up and running as described in part three of this how-to guide. If you're struggling, leave a comment and I'll see if I can help.

 

Adding a real time clock

When switched off, our Raspberry Pi has no powered supplied to it at all. This means it has no way of keeping track of time during the time it's powered off. Laptops, cameras and other devices that keep the time often contain a coin battery that supplies power to a clock so that even when the main power supply or battery for the device is disconnected, when it's reconnected the device still knows the correct (real) time.

Raspberry Pis automatically set the time when they are powered on and have an internet connection so if you're using your trail camera in range of a WiFi network then you needn't bother with this step. However, I want to use my trail cam in locations out of range of a WiFi connection and I want to be able to tell when the footage was being recorded. Here's where our real time clock (RTC) comes in.

The RTC we'll be using is just a very small component powered by a tiny lithium coin cell that plugs directly into our Raspberry Pi's GPIO. With a few commands we can make the Raspberyy Pi aware of it and instruct it to use the time according to the RTC. Once set, it means we can disconnect from our local network and switch off our Raspberry Pi knowing that when we switch it back on, it will know the correct time.

I bought a couple of RTCs in case the battery on it ever dies.

 

Enabling and attaching the RTC to the Raspberry Pi

The RTC utilises something called the I2C (pronounced 'eye squared see') bus on the Raspberry Pi. Going into I2C in detail is beyond the scope of this guide however, in order to use the RTC you must enable the I2C interface as we did with the camera - do this BEFORE attaching the RTC.

sudo raspi-config

Use the arrow to move down to 5 Interfacing Options and then down to P5 I2C. Select Yes to enable and automatically load this module. Shutdown your Raspberry Pi.

sudo halt

Make sure your Raspberry Pi is powered down and unplugged. Attach the RTC to the inner row of the GPIO on Pin 1 so the chip points inward (i.e. not covering any other pins).

Now you can follow the RTC set up instructions from The Pi Hut here. Make note of which type of Raspberry Pi you have as some of the commands differ very slightly. Unless you have a very old Raspberry Pi, the default commands should work.

Typing

date

in the terminal should still give you the correct time if everything is set up correctly, however this is slightly hard to test because the likelihood is that you're still connected to the internet.

To conduct a proper test to check everything is working as expected:

  • Turn off your Raspberry Pi
  • Turn off your router
  • Turn the Raspberry Pi back on
  • Wait until it has recorded some video (remember to activate the PIR)
  • Turn off the Raspberry Pi
  • Turn your router back on
  • Turn on the Raspberry Pi again and SSH in to it.

If everything works, you should be able to see log files and video footage on the USB stick with the correctly time-stamped filenames. Furthermore, if you remove the USB stick and watch the footage on your computer, the timestamp on the footage should be correct!

 

Conclusion

You should now have a working RTC on your trail cam. It's important not to remove the RTC where possible. Doing so could disrupt the timing on the Raspberry Pi. If you have to remove it for another project, just follow the instructions in the link above to reset it.

Next time we'll look at options for making a casing for our trail cam. Spoiler alert - it's an air-tight food storage box (and it works brilliantly!).