The Challenge

Raspberry Pi Zero with a piSugar2

Raspberry Pi Zero with a piSugar2

The Raspberry Pi doesn't have a real time clock(RTC) or the button battery to keep the time circuitry going when there's no power attached. The Pi's are designed to be connected to the Internet and that is how they set their time. My current project involves capturing images of sunrises with a Raspberry Pi Zero. I didn't want this device to be tethered to a power cable so that it could be deployed at the location of my choosing. Since it would be self powered I'd need a battery and circuitry to monitor the battery state. I found the awesome solution of the PiSugar2. The PiSugar2 is a 1200mAh Li-Ion battery coupled with a circuit board that interfaces with the Zero while providing it's own charging circuit as well as an RTC. The PiSugar team have written their own monitoring service that you can sue to query information and also set variables.

The run-time on the PiSugar2 on a Zero is somewhere north of eight hours so of course that  will not work for catching sunrise shots. The goal is to use the power up function of the Sugar to boot the device at the appropriate time of day. Python code will then take a picture each minute for 15 minutes, calculate tomorrow's sunrise time, and then set tomorrow's boot time. By having a run time of less than 20 minutes per day, I'll greatly increase the number of days the battery will keep powering the Zero.

In testing the re-booting feature I noticed that every time my Zero booted the system time was way off. So my first challenge became writing a script to set the system time and then configuring systemd (the main service process for the OS) to run the script at boot.

Interfacing with the PiSugar2

The PiSugar2 connects with the Zero with the I2C interface, so the first thing to do is install the power manager software and then test to make sure the interface to the Sugar is working.

The command that needs to be run to set the system time to the RTC time is "rtc_rtc2pi". Looking at the docs I found that I can use the python socket library to communicate with the Sugar. I wrote the following script to do that.

#!/usr/bin/python3 #This program will set the raspberry pi's time to the RTC on the piSugar board. import socket

HOST = '' PORT = 8423 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as S: S.connect((HOST, PORT)) S.sendall(b'rtc_rtc2pi')

This code only sends the command to set the time. Now that the action code works, we need to set the system to run it at boot.


Up to this point I've not created any of my own systemd services so I searched the Internet for a how-to and found this one. It showed me how to write the simple service unit below, which I named set-clock.service and saved in /etc/systemd/system.

[Unit] Description=Syncing system and hardware clock

[Service] ExecStart=/home/pi/code/


To test this out before rebooting, run:

sudo systemctl daemon-reload sudo systemctl start set-clock.service sudo systemctl status set-clock.service

You'll get output like below.

Setting Clock Service

Future Improvements

This certainly gets the job done. It should be expanded to log that entry in /var/log/messages so you don't just have a huge time gap in the log without an explanation.

- Keith