Skip to content

Instantly share code, notes, and snippets.

@grubernd
Last active June 14, 2023 20:19
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save grubernd/aed721614b36aaa31fd97ef5ab1ec6be to your computer and use it in GitHub Desktop.
Save grubernd/aed721614b36aaa31fd97ef5ab1ec6be to your computer and use it in GitHub Desktop.
install & setup of an I2C RTC on RaspberryPi (both Arch Linux ARM and Raspbian)
#!/bin/bash
#-----------------------------------------------------------
# Copyright (C) 2014-2016 GRUBERND http://grubernd.at
# released under a FreeBSD License
#-----------------------------------------------------------
# CATE installer module :: device RealTimeClock RPi
#
# !! needs to be run as root / sudo !!
#
# works both for DS1307 and DS3231 chips.
# this script is also designed to run standalone for
# both Arch Linux ARM and Raspbian installs.
#-----------------------------------------------------------
#_CATECONF__ # RTC service requires no configuration
if [[ "$(which pacman)" != "" ]]; then
echo -e ""
echo -e ".. installing I2C RTC for Arch Linux ARM"
echo -e ""
modules_file="/etc/modules-load.d/raspberrypi.conf"
pacman -S --needed --noconfirm i2c-tools
fi
if [[ "$(which apt-get)" != "" ]]; then
echo -e ""
echo -e ".. installing I2C RTC for Raspbian"
echo -e ""
modules_file="/etc/modules"
apt-get install --yes i2c-tools
fi
echo -e ""
echo -e ".. activating I2C devicetree in /boot/config.txt"
sed --in-place 's/^#device_tree_param=i2c_arm=on/device_tree_param=i2c_arm=on/' "/boot/config.txt"
#-----------------------------------------------------------
echo -e ""
echo -e ".. adding i2c and rtc modules to the conf"
cp "$modules_file" /tmp/rtc.tmp
echo rtc-ds1307 >> /tmp/rtc.tmp
echo i2c-dev >> /tmp/rtc.tmp
sort /tmp/rtc.tmp | uniq > "$modules_file"
#-----------------------------------------------------------
echo -e ""
echo -e ".. creating init script for the RTC"
mkdir -p /usr/lib/systemd/scripts/
cat > /usr/lib/systemd/scripts/rtc << ENDRTCSCRIPT
#!/bin/bash
# create an i2c device DS1307 (works also for DS3231)
# set systemclock from external i2c-rtc
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s
ENDRTCSCRIPT
chmod 755 /usr/lib/systemd/scripts/rtc
echo -e ""
echo -e ".. creating service for the RTC"
cat > /etc/systemd/system/rtc.service << ENDRTCSERVICE
[Unit]
Description=RTClock
Before=network.target
[Service]
ExecStart=/usr/lib/systemd/scripts/rtc
Type=oneshot
[Install]
WantedBy=multi-user.target
ENDRTCSERVICE
#-----------------------------------------------------------
echo -e ""
echo -e ".. enabling and starting RTC service"
systemctl enable rtc
systemctl start rtc
#-----------------------------------------------------------
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. systemctl status rtc"
#_CATETEST__ systemctl status rtc
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. testing i2c activation in /boot/config.txt"
#_CATETEST__ grep "^device_tree_param=i2c_arm=on" /boot/config.txt
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. testing connection of i2c devices"
#_CATETEST__ i2cdetect -y 1
#_CATETEST__ echo -e ""
#_CATETEST__ echo -e ".. testing rtc hwclock"
#_CATETEST__ hwclock
@dmlad4uu
Copy link

I know this is simple. How do I execute this script on my rpi 4.? What is the cammand to do that? Can i retrieve the github script with 1 command and run it?

Thank you

Dave

@grubernd
Copy link
Author

“I’m sorry, Dave. I’m afraid I can’t do that.”

Besides, this script has neither been tested nor updated since 2016.
I have no idea if it still works. I have not used it for at least five years.

@dmlad4uu
Copy link

dmlad4uu commented Jun 14, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment