power button with led for rpi3

  • I just created this for my setup, perhaps it is useful for others:

    required items:

    • rpi3 with libreelec
    • switch (normally open) between ports 5 and 6 of the GPIO pins
    • led between ports 6 (or some other GND) and 8 of the GPIO pins (TX uart) (add 330 ohm resistor)
    • install package adafruit-libraries

    The uart is off by default, so you need to turn it on in the config.txt

    Code
    mount -o rw,remount /flash

    Edit /flash/config.txt`` and insert the following somewhere logical

    Code
    dtoverlay=pi3-miniuart-bt

    ```

    create the following files

    • /storage/scripts/off-button.py

    `

    • /storage/scripts/poweroff.sh
    Bash
    #!/bin/sh
    
    echo "standby 0" | cec-client RPI -s -d 1 -p 0
    shutdown -h now

    Make both scripts executable:

    Code
    chmod +x /storage/scripts/poweroff.sh /storage/scripts/off-button.py

    Then make sure the off-button.py script is started when the rpi starts up:

    • /storage/.config/autostart.sh
    Code
    (
      python /storage/scripts/off-button.py
    ) &

    After rebooting the rpi3, it should work.

    Thanks to AndrewH7 of instructables for the python script and everyone on the internet who wrote about adding a power button with or without LED to the rpi.

    (The only thing I added/changed was the cec-client stuff via an external script)

    Edited 3 times, last by pooh22 (January 22, 2022 at 1:37 PM).

  • NB, I tried to do this same thing on the raspberry pi 4 (rpi4) and it doesn't work entirely the same.

    The pi4 needs a recent firmware (the first ones produced have a firm-ware which doesn't let the power button works. If you have this problem, best use a different sd card with raspian to update the firmware with ``````rpi-update

    Furthermore, the cec-client seems to have issues with some TV's or hdmi cables. And it's only supposed to work on the hdmi port closest to the powersupply port. For me (Philips tv), it still doesn't work. It could be the cable, the TV or something else.

    For the most part though, it can work on the rpi4 as well.

  • The pi4 needs a recent firmware (the first ones produced have a firm-ware which doesn't let the power button works. If you have this problem, best use a different sd card with raspian to update the firmware with ``````rpi-update

    IIRC, rpi-update doesn't load any firmware to chip (it's just bootloader & kernel), so switching back to LE would still be running the same bootloader & kernel. Maybe you meant rpi-eeprom-update for the Pi4? Which LE10 comes with a fairly recent "firmware", but it has to be loaded to EEPROM (otherwise it is factory default). You can run rpi-eeprom-update on LE10.

    I believe Raspberry Pi OS automatically updates the EEPROM at boot, so perhaps that is what it took?

  • I believe Raspberry Pi OS automatically updates the EEPROM at boot, so perhaps that is what it took?

    I guess that would do it.
    There seems to be some confusion about the term "firmware", I thought it was the stuff that stays on the pi, rather than the sd-card, but it looks like it may be just "drivers" for the kernel in raspian. Or perhaps rpi-update calls the eeprom update as well?

    Anyway, if it was the booting of raspberry OS or running rpi-update, it does boot when I press the button now...

    I wonder what gives with the cec-client, perhaps the cable is the problem, as that's different with the one I used for the rpi3...

  • There seems to be some confusion about the term "firmware", I thought it was the stuff that stays on the pi, rather than the sd-card, but it looks like it may be just "drivers" for the kernel in raspian. Or perhaps rpi-update calls the eeprom update as well?

    rpi-update definitely doesn't update the eeprom. It does deliver "firmware" files, I just don't think they are persisted to chip based on what I have seen. I would expect the eeprom has a stage 1 bootloader, then there is a stage 2 bootloader on the FAT partition (which is updated by rpi-update), and stage 2 should load the kernel image. I believe the other bits like "start.elf" may be for the GPU, so those might be firmware files loaded in stage 1 or 2 bootloaders -- but I would maybe consider that a soft-firmware like your Broadcom drivers.

    LE is using the same bootloaders, firmware files, etc that Raspberry Pi OS uses -- but the update method & timeline may be different than Raspberry Pi OS, so they could be different versions. And I think the biggest difference is as I mentioned earlier, LE doesn't automatically update the eeprom but Raspberry Pi OS does at boot up. See below for output of one of the systemd services running there, it's dependent on new eeprom files delivered via the "rpi-eeprom" package (once you run apt-get upgrade or via the GUI, install new packages, and reboot it will stage the eeprom for loading, might require 2 reboots, never really looked closely).

    In any case, just wanted to mention that it was perhaps the eeprom that need to be updated, in case some one else wanted to use your solution. Good to hear that your solution works on the RPi4.

  • LE settings add-on > Updates > Firmware .. if any bootloader eeprom or USB firmware updates are needed they will be visible here and you can enable the update (it will then be done on next reboot).

  • is this supposed to work on rpi4B? i have tried on mine and it doesnt work

    Don't see why not..

    I have power button with led setup on a rpi2 but slightly different setup then this post regarding led control..

    So assuming the eeprom issue is resolved since the start of this thread and you want this setup...you'll have to install rpi tools addon for gpio button control...not sure why it says

    install package adafruit-libraries