User avatar
TLoATDaE
Posts: 12
Joined: Sun Jul 16, 2017 1:48 pm

Adding a Power Switch+LED to a RPi3 with an I2C Clock

Sun Jul 16, 2017 2:22 pm

Greetings, friendly folks!
I am new here, and have what seems to me to be a rather advanced question (particularly for a newbie like me).
Here goes:

I have a Raspberry Pi 3 equipped with a RealTime Clock (DS3231 for PI)
My Pi is running the Lubuntu 16.04 LTS version of Linux.
The RTC board has been plugged onto GPIO pins 1,3,5,7,9.
The software support has been installed and activated, so it works OK.
(See reference: https://thepihut.com/blogs/raspberry-pi-tutorials/17209332-adding-a-real-time-clock-to-your-raspberry-pi)

What I am trying to do is: Add an Illuminated Power Switch that will power my Pi down safely.
Please see references:
https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi
and it's companion piece:
https://howchoo.com/g/ytzjyzy4m2e/build-a-simple-raspberry-pi-led-power-status-indicator

So far, I have the RTC working well, but I have it placed on pins that the switch and LED both call for in the article...

Now, a few questions pop to mind here:
First, the pins used by the RTC are mapped as follows:
1 - 3.3V
3 - IO2
5 - IO3
7 - IO4
9 - GND

Can I move the RTC to the alternate pins as follows:
17 - 3.3V
19 - IO10
21 - IO9
23 - IO11
25 - GND
and have it still work? Do I have to tinker with something in the settings?

If I can, indeed, move it to those other pins, the ones referenced by the Power Switch/LED articles become freed up, and should be a piece of cake to install.

Barring that, is it easier to connect the LED and Power switch to other pins?
I have attached the only wiring reference I have for the switch, and have used a 220 ohm resistor I got out of my CanaKit (Raspberry Pi Kit for Dummies) to the negative side of the built-in LED of the switch.

Please let me know what other info I need to add here for coherent replies, and I'll gladly do so.

Thanks in advance!
TLoATDaE (Val Barlow)
More than a n00b, less than fully proficient.
Attachments
Illuminated Power Switch (Small).jpg
Illuminated Power Switch Reference
Illuminated Power Switch (Small).jpg (23.13 KiB) Viewed 3044 times

pcmanbob
Posts: 4460
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding Power Switch+LEDto a system with an I2C Clock

Sun Jul 16, 2017 2:52 pm

HI.

I think it would be easier to just use different pins for the switch and LED as they will work on other GPIO pins without any issues you just need to change the program to reflect your used GPIO pins.

so switch how to uses GPIO 3
if you used GPIO 5 instead you would just need to change the code thus.

Code: Select all

#!/usr/bin/env python


import RPi.GPIO as GPIO
import subprocess


GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.wait_for_edge(5, GPIO.FALLING)

subprocess.call(['shutdown', '-h', 'now'], shell=False)
and the LED link does not use any of the rtc pins as its connected to GPIO 14 and 15
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
TLoATDaE
Posts: 12
Joined: Sun Jul 16, 2017 1:48 pm

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Sun Jul 16, 2017 3:06 pm

Thanks for the prompt reply (and the code reference)!

I'll have to give this a try, and let you know what the results are :)

Also: Quick Question:
Is the referenced article(s) good, or are there better references for adding a Power Switch, and I just haven't seem them yet?

Thanks a bunch!
(Wish me luck!)

pcmanbob
Posts: 4460
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Sun Jul 16, 2017 3:13 pm

There are probably more examples out there as to which are good or bad I don't know, if an example works for you the go with it.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

alphanumeric
Posts: 1537
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Sun Jul 16, 2017 8:36 pm

They try to pick a GPIO that's not commonly used. You can use any one you want to though, as mentioned above. Look at three different tutorials and odds are good they each use a different GPIO pin. ;) I have a DS3231 on several of my Pi's that run Raspbian. They use I2C for communication though.

User avatar
TLoATDaE
Posts: 12
Joined: Sun Jul 16, 2017 1:48 pm

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Wed Jul 19, 2017 7:10 am

OK, here's my progress report:

I tried the reference and script I found here:
https://www.element14.com/community/doc ... berry-pi-b

The author has you make a Scripts directory, then write the little Python script. After that, he has you add it to the list of programs to run on start-up.

Code: Select all

#!/bin/python
# Simple script for shutting down the RPi at the press of a button.
# by Inderpreet Singh - Edited by Val Barlow

import RPi.GPIO as GPIO
import time
import os

# Use the Broadcom SOC Pin numbers
# Setup the Pin with Internal pullups enabled and PIN in reading mode.
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Our function on what to do when the button is pressed.
def Shutdown(channel):
    os.system("sudo shutdown -h now")

# Add our function to execute when the button is pressed.
GPIO.add_event_detect(18, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

# Now Wait!
while 1:
    time.sleep(1)
Not too complicated, there.

It references GPIO 18 (pin 12) and the Ground connection (pin 14)

I followed the instructions, saved off the files, shutdown the Pi, connected the pins accordingly, started things back up, and...

After it boots up completely, I press the button.
After a short pause, it shuts down! 8-)

Now, all I need to do, is get the LED on the switch to work...

I reference https://howchoo.com/g/ytzjyzy4m2e/build ... -indicator

So, into the /boot/config.txt file I go (to add enable_uart=1)

Save off the file and shut down.

OK, now I connect my circuit to the following pins:
Pin 6 - GND (Resistor to Negative pole of the LED)
Pin 8 - GPIO14 [TXD] (Positive pole of the LED)

Now it's time to cross my fingers and boot up.
The test LED lights up accordingly! :D

So, all in all, a Great Success!

To be fair, I had to debug my script, as I had a couple of typos in it :oops: and we all know just how finicky Linux Code can be, so I had to fix those to get it to work.

There are a few pics I took of my little project, but I am unsure of how to get those posted here without going over the size limits, or getting myself in trouble, so I'll just give credit where it's due here:

To pcmanbob and alphanumeric: Thanks for the quick replies and encouraging posts!
The authors of the articles I referenced: ipv1, Zach, tyler, etc (sorry if I messed anyone)
And the folks that provided the Raspberry Pi for Dummies Kit (CanaKit) and the useful bits and pieces in there.

Let me just say this: I am glad I came here, and I hope others find this helpful.
Looks like a fun place to find help and suggestions.

If you folks want pics, and I can get away with posting them, just ask!

N00b with dirty fingers,
TLoATDaE

P.S. If someone has more elegant code for this, Please let me know!
Thanks again everyone!

alphanumeric
Posts: 1537
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Wed Jul 19, 2017 9:19 am

I usually put my Pics in my Public OneDrive folder and then just post a link here. I put my Python files in /home/Pi. I run them via crontab. As with a lot of this stuff, its what ever works for you. ;)

pcmanbob
Posts: 4460
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Wed Jul 19, 2017 9:56 am

Glad you got it all working and it sounds like you are on your way to being hooked on the raspberry pi welcome to the club.

To post your pictures the way I use is to sign up to imgur.com its free.
upload your picture there ( simple drag and drop interface) you can create your own album to put them in or just put them in your images folder. then you click on the image and to the right of it is a list of commands for posting the image just click on the copy button under the one that says BBCode and just paste the line in to your post here and your image will appear in your post when you preview it /post it.

Hope that's clear if you need any help just ask.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
TLoATDaE
Posts: 12
Joined: Sun Jul 16, 2017 1:48 pm

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Thu Aug 03, 2017 2:25 pm

OK, I got myself an imgur account, and have uploaded the assortment of progress pics.
I want to apologize for the crappy quality up front, as I only have an ancient Alcatel M'pop phone to use for these things...

Here we go:
Test Circuit: Image
Test Circuit Running: Image
Pin Closeup: Image
Pre-assembly Test: Image
Final Assembly (Open): Image
End Product (Working): Image

For the record, I used the Forged Copper heatsinks: BMR-C1 (15x15x15mm) for the main processor, MOS-C10 (10x10x15mm) for the Ethernet Controller, and a 15x15x1mm "Copper Shim" on the bottom Memory Chip (Please feel free to correct me if I am wrong on the chip identification).

The case is an "eleduino" aluminum alloy case that comes with (rather puny) heatsinks.

Beside the I2C Real-Time Clock module I have nestled the other connections, I found a 5 connector wide cable I had lying around from ages past, and transferred the single contacts to their corresponding places for ease-of-connection (leaving the center spot empty). Add to that, the strategically placed shrink-wrap tubing on the back of the switch, and I don't have errant contact situations to worry about.
Something tells me, if I wanted the LED to shine more brightly, I could try removing the resistor. Right now, the less-than-blinding light is good for what might be used as a OSMC box from time to time.

All in all, it's not too shabby looking, and doesn't come off as hacked together by a rabid 9-year-old.

What does everyone think?

Sincerely,
The smudge-fingered pseudo-n00b.

pcmanbob
Posts: 4460
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Thu Aug 03, 2017 2:48 pm

Nice looking project you have there.

From your picture it looks like you used a blue LED, blue LEDs have a forward voltage of 3.3V and a forward current of 30ma max so you wont be able to drive it direct from the GPIO to full brightness, you would need to use a transistor which would allow you to power the LED from the 5v but control it from the GPIO.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

alphanumeric
Posts: 1537
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Adding a Power Switch+LED to a RPi3 with an I2C Clock

Thu Aug 03, 2017 3:02 pm

Looks really nice. I have 4 10mm LED's in one of my projects. Red, Yellow, Green and Blue. To get full brightness, and limit the current drawn from the GPIO I used this, https://www.adafruit.com/product/757 The GPIO are on the 3.3v side and the LED's on the 5V side. Four channels and four LED''s, worked out perfect for me. A lot easier and smaller than bread boarding my own circuit up. Overkill in your case, but the principle is the same. Darlington's and resistors. The 5V that feeds the LED's comes from my Power supply, not the Pi. Each LED has its own series load resistor so I can set the brightness I want.

Return to “Advanced users”