top of page
Writer's pictureCatherine Nicoloff

T-Minus Three Days: Adding a Real Time Clock

As mentioned in my previous post, with only three days left until launch, I made one last major change to STAR: I added a Real Time Clock (RTC).


The Raspberry Pi does not have a RTC. Instead, users are expected to keep the Pi connected to the internet so that it can poll a time server to keep its clock synchronized. Under normal operating conditions, this is fine.


We are about to send a Pi Zero W up on a weather balloon. We are not going to have internet. Normally, this would not be an issue, as the Pi does contain a clock of sorts that counts time elapsed since boot. As long as the Pi does not reboot, this is fine.


We are about to send a Pi Zero W into some of the coldest, lowest pressure regions of the atmosphere. I've learned from experience that minor fluctuations in power delivered to the Pi will cause it to reboot. I have no idea what is going to happen to the Pi or the Geiger circuit when it gets very cold, passes through condensation, or gets jostled by the wind.


I do not feel I can trust the Pi not to reboot. If it reboots, all the timestamps on my recorded data will become untrustworthy.


I've been tracking my major and minor goals since the beginning of the project. Creating STAR produced not only a homemade Geiger counter but a 152 page lab notebook of notes, schematics, parts lists, and goals. My launch day goals all have green check marks next to them. I have time to pick a stretch goal. I chose to add the RTC.


This is the RTC module I used. It is based around the DS1307 RTC chip.


These modules are cheap and easy to find. Search Amazon.com for "rtc arduino" and you will find packs of them for around $1 each.


IMPORTANT: These RTC modules are intended for use with an Arduino! To use them safely with a Raspberry Pi, a modification must be made.



RTC Modification


This RTC, sold for use with an Arduino, is configured to use 5V logic. The DS1307 chip itself will work with the 3.3V logic of a Raspberry Pi, but we must delete pull up resistors R2 and R3. They are clearly labeled at the bottom of the photo above. I used a soldering iron with a very small tip to soften the solder on each one and then pulled them away from the board with tweezers. You could also just clip them off with flush cutters.


For more information about the DS1307 (and to see why removing these resistors does not harm anything), check out https://www.elecrow.com/tiny-rtc-for-arduino-p-323.html


DS1307 schematic

Click the link labeled "Datasheet and related data". You will receive a ZIP file containing a schematic. A cropped region of the schematic to the right shows R2 and R3 connected between VCC and the SCL and SDA pins. Removing them simply severs the 5V pull up connection to VCC. It does not prevent us from using SCL or SDA.


The I2C pins on the Raspberry Pi have built-in pull up resistors to 3.3V, so we don't have to replace R2 and R3 with anything. Bonus!


If the idea of pull up (or pull down) resistors does not make sense, an excellent explanation can be found at https://www.electronics-tutorials.ws/logic/pull-up-resistor.html


Wiring Up the RTC


Raspberry Pi header pins, labeled

Using the pin header guide above, here is how the RTC was wired.


RTC input pin wiring table

Configuring the RTC



If my abbreviated instructions here do not work for you, I suggest going to the link above.

> sudo raspi-config

Under Advanced select I2C and turn it on.

Reboot.

> sudo i2cdetect -y 1

You should see ID #68 show up - that's the address of the DS1307

(If you have a much older Pi 1, you will have to run sudo i2cdetect -y 0 as the I2C bus

address changed from 0 to 1)

> sudo nano /boot/config.txt

Add:

dtoverlay=i2c-rtc,ds1307

Save and reboot.

> sudo i2cdetect -y 1

You should see UU show up where 0x68 should be.

> sudo apt-get -y remove fake-hwclock
> sudo update-rc.d -f fake-hwclock remove
> sudo nano /lib/udev/hwclock-set

Comment out these three lines:

#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

Reboot.


Plug in Ethernet or turn on WiFi to let the Pi sync the time from an NTP server on the Internet. Wait for the time to properly sync. Then:

> sudo hwclock -w 

To write the time to the RTC, and then:

> sudo hwclock -r 

To read the time from the RTC.


After adding the RTC, STAR now looked like this:


STAR, May 10 2018, now with Real Time Clock (left)
STAR, May 10 2018, now with Real Time Clock (left)

Post-Launch RTC Update



Nevertheless after a reboot sometimes the date/time is set to '3 Nov 2016 17:16 the different working and failing scenarios I found that the wrong date/time occurs when the RPi connects successfully to a WLAN, but has no access to the internet. Obviously NTP cannot find/access a time server. But I wonder why the correct value in the RTC is not used at such a moment.

This issue happened to me on launch day because STAR was configured to connect to a portable WiFi access point (I'll explain why in another post). If the WiFi access point was on when STAR booted, it would connect to it and then try to locate an NTP server. Obviously, it could not, as the portable access point was a local network only and not connected to the internet. For mysterious reasons, the Pi Zero did not then fall back to using the RTC, choosing instead to use the default date and time.


The solution appears to be to de-configure NTP as a client after the RTC is set up.

14 views0 comments

Comments


bottom of page