Power questions for project to add a Shutdown button / LED to Raspberry Pi

Discussion in 'Embedded Systems and Microcontrollers' started by stb, Apr 11, 2016.

  1. stb

    Thread Starter New Member

    Apr 11, 2016

    I'm planning a simple project to add a shutdown button to a Raspberry Pi. Basically, when a button is pressed and held for a couple of seconds the Pi will issue a shutdown command.

    For feedback I'm thinking something along the lines of, once the button press is detected I want to light an LED and, once the time held has been reached to issue the shutdown command, light another LED (or probably change the colour using an RGB LED).

    I might add a small buzzer when the button is pressed in case it's being pressed on by something accidentally.

    I don't really have any experience of electronics past a little electrical set I had about 40 years ago but I've always been interested.

    The 'Build Raspberry Pi Controllers: LED Flasher' series of articles by Don Wilcher are a great start for ideas on what can be done and how, but I have a couple of questions ...

    1) Is the opto-isolator really necessary when driving the LED? Or could a transistor be used given that the power is coming from the Pi anyway?

    I'm guessing it's there because the GPIO is 3.3v and it's using the 5v supply line for the LED (and presumably I'm incorrect in thinking that a transistor would work). Presumably it wouldn't be necessary if using the 3.3v supply though?

    2) I am actually planning on using the opto-isolator (despite question 1) as I'm planning on powering the LED with an external source (3v battery). I'm thinking that I should connect the -ve from the battery side of the circuit to the Pi's ground, is that correct or should they stay separate?

    (Why a battery when there's a perfectly good Pi with 3.3 and 5v supplies? I have a small touchscreen which I haven't used yet other than a quick test to make sure it works. If this were to be plugged into the Pi at the same time then it would cover the lower 26 pins meaning no easy access to the +ve pins. Also, I have a couple of new 3v button cells that aren't any use anymore so I thought I might as well use those rather than worry about stacking connections or similar.)

    Thanks for reading this, I'd be grateful for any help.
  2. KMoffett

    AAC Fanatic!

    Dec 19, 2007
    I used this circuit for Push-on/Push-off or Program-off with a PICAXE micro. It should work for the Raspberry Pi.
  3. stb

    Thread Starter New Member

    Apr 11, 2016
    Thanks Ken

    That looks interesting for controlling the power to the Pi if I run it from battery, which I will (at least I think that's what it does!) but I'd still need to send something to a GPIO pin to tell it to shutdown nicely first. (The Pi does still eat power once shutdown - primarily the ethernet apparently.)

    I'm happy with the idea of connecting a simple push button to a GPIO pin, detecting that and issuing the command. But I'm not sure about -ve / GND connection when powering the "button received, shutting down now" feedback LED (and maybe buzzer - if I can stop blowing them!) from a battery.

  4. Marley


    Apr 4, 2016
    A shutdown command, from the command line, for example, just shuts down the operating system. After that, the CPU is still clocking and peripherals are still powered up. If you are interested in saving power - not really worth doing.

    If you want to switch off the power after the OS has shut down what you need to have is a small program on the Pi that runs all the time and regularly toggles a spare GPIO. Then you can have a small external circuit that watches this GPIO and keeps the power switched on as long as the output keeps toggling.

    When the OS is shutdown, all applications are killed. The toggle program stops. The external circuit detects this and, after a short delay, switches off the 5V power (probably using a series MOSFET or relay contact).

    I am currently designing something like this for my Kodi Media Center. This will use an 8-pin PIC to do the monitoring and timing. Also need to have a button to press to start it up. I have a half-done design if you are interested. Must finish it!
  5. stb

    Thread Starter New Member

    Apr 11, 2016
    Thanks, sounds interesting and something I'd like to try longer term. (Probably a bit too clever for me as a first project!)

    The Pi runs headless to provide an Access Point / VPN (forwarding via a public wifi network). Removing power once the Pi OS is shut down isn't a problem as I can just unplug it / remove the battery pack.

    Issuing the shutdown command isn't impossible either, because it's an Access Point and I can ssh to the Pi and issue it (I need ssh to configure new wifi networks anyway). But, I'm lazy, may only be connecting via a phone sometimes and don't want to be messing with ssh on that. Also, sometimes my nieces might be using it and won't want to be logging in and issuing commands to stop it nicely.

    So a simple push switch with LED feedback should be a workable solution and a simple 1st project to construct and to test out GPIO use (and let me practice my soldering on nothing too sensitive!).

    LED feedback isn't strictly necessary, you'll know if the Pi has gone down when you lose the AP, but I think it would be a lot nicer (and impress the nieces more!) and try out using the Pi output too. The idea of the (momentary contact) push switch / delay is in case it gets accidentally pressed while things are being moved around.