Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyfontaine/129537280d2ea073cc5ee57a2a6df435 to your computer and use it in GitHub Desktop.
Save troyfontaine/129537280d2ea073cc5ee57a2a6df435 to your computer and use it in GitHub Desktop.
Raspberry Pi with LED Power Button and RTC

How to Set up a Raspberry Pi with LED Power Button and RTC

After a lot of trial and error, I've figured out how to set up a Raspberry Pi (in my case, I tested with a 2B and 3B+, this may not work on a Pi 4B). This involves using the i2c0 bus (which normally isn't recommended), but this is what I was able to get working as re-mapping would communicate with the RTC via i2c, but the overlay for the RTC wouldn't adjust its configuration when told to use i2c3 or higher.

Required hardware

  1. Power button (this should be a momentary switch type)
  2. Power LED (for me, this was actually built into the power button)
  3. 330K Ohm resistor (this is connected between the LED and the 3.3v header
  4. A Real Time Clock (RTC) Module (preferably, one with built-in pull up resistors)
  5. Jumper wires
  6. Depending on what you're planning, you may need a soldering iron and related tools

Below are the steps to configure the Raspberry Pi OS (the assumption is that you're using Raspberry Pi OS).

  1. Install the i2c tools sudo apt-get update && sudo apt-get install -y i2c-tools

  2. Execute sudo raspi-config nonint do_i2c 0 to enable i2c

  3. Update your /boot/config.txt with the lines from the 2-boot-config.txt file in this gist

  4. Shutdown your Pi and disconnect power

  5. Connect your power button via jumper wires to board pins 5 and 6

  6. You will need to attach a 330K Ohm resister to the Anode of your LED between it and the board

  7. Connect the anode of the LED to board pin 8 (GPIO pin 14)

  8. Connected the cathode of the LED via jumper wire to board pin 14 (ground)

  9. Now, you will need to wire up the RTC module

  10. Using jumper wires

  11. Connect the positive header on the RTC to board pin 1 (3.3v Power)

  12. Connect the D header on the RTC to board pin 27 (GPIO pin 0)

  13. Connect the C header on the RTC to board pin 28 (GPIO pin 1)

  14. Connect the negative header on the RTC to board pin 20 (Ground)

  15. Connect power to your pi once again

  16. Press your power button, the Pi should now power on

  17. Log into your OS and confirm that the RTC is working by running the command sudo i2cdetect -y 0, to which you should see output that shows UU on row 60 column 8. This confirms that your RTC is working and detected properly

  18. Disable the fake hardware clock by running the following commands

    sudo apt-get -y remove fake-hwclock
    sudo update-rc.d -f fake-hwclock remove
    sudo systemctl disable fake-hwclock
    
  19. Next up, open /lib/udev/hwclock-set in your favorite text editor as root and comment out the following statements

    if [ -e /run/systemd/system ] ; then
        exit 0
    fi
    
    /sbin/hwclock --rtc=$dev --systz
    
  20. Check the time set on the RTC by running sudo hwclock -r

  21. Once you've verified that it is reading the time from the hardware clock, sync the hardware clock by running sudo hwclock -w

  22. To test that it worked, shutdown your Pi by pressing the power button (wait for the green LED beside the red power LED to stop flashing), then disconnect the network, unplug the power wait a few seconds and re-connect the power

  23. Sign into the Pi and run timedatectl status to confirm that the date and time were properly set via the RTC module

  24. That's it!

# Comment out the dtparam for i2c_arm as it automatically disables i2c0
#dtparam=i2c_arm=on
# Enable the i2c0 bus for us to communicate with the RTC
dtparam=i2c0=on
# Enable the Serial Console as it will provide the power to the power LED
enable_uart=1
...
[cm4]
...
[all]
# Enable shorting GPIO 3 (board pin 5) to ground to execute a controlled shutdown
dtoverlay=gpio-shutdown
# Use the overlay for the ds3231 RTC (you may need to change this to the module you're using)
# the key take away here is the i2c0 portion which tells the kernel that the
# rtc can be accessed on GPIO pin 0 for SDA (board pin 27) and GPIO pin 1 for SCL (board pin 28)
dtoverlay=i2c-rtc,ds3231,wakeup-source,i2c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment