Add Adafruit's Ring LED Momentary Switch to Raspberry Pi

by jeff.cartwright.562 in Circuits > Raspberry Pi

6542 Views, 18 Favorites, 0 Comments

Add Adafruit's Ring LED Momentary Switch to Raspberry Pi

led ring switch.jpg

As part of my cord cutting system, I want a power indicator and a reset switch on a Raspberry Pi-based media center running Kodi on OSMC.

I've tried several different momentary switches. Adafruit's Rugged Metal Push Button with Blue LED is very cool.

This is the first of several instructables, which form the basis of a Kodi/OSMC hat for the Raspberry Pi.

In each instructable, I will get part of the hat to work and I think each piece of the hat is useful by itself.

Gather Parts

Parts specific to this instructable:

  • Rugged Metal Push Button with Blue LED Adafruit $4.95
  • Short breakaway pins Adafruit $4.95

Reusable parts and tools (prices in USD):

  • Breadboard Fry's $7.99
  • Breadboard wires Fry's $7.99
  • Male to Female Jumper Wires Fry's $3.99
  • MacBook Pro (a PC could be used)
  • Raspberry Pi 3 Element14 $35
  • 5.2V 2.1A USB Power Adapter from Amazon $5.99
  • Micro USB to USB cable 3ft from Amazon $4.69
  • Case from Amazon $6.99
  • SanDisk Ultra 16 GB microSDHC Class 10 with Adapter (SDSQUAN-016G-G4A) from Amazon $8.99
  • Solder, Solder Station, Tip Cleaner
  • TV with HDMI port, USB keyboard, USB mouse, HDMI Cable

Notes:

  • Text enclosed in spades, such as, ♣replace-this♣, should be replaced with an actual value. Of course, remove the spades.
  • The Raspberry Pi should be set up and running Kodi/OSMC
  • Adafruit has some excellent "instructables", but I they are difficult to find. In google, try:
    • ♣search-terms♣ inurl:https://learn.adafruit.com/
    • rugged led ring inurl:https://learn.adafruit.com/
  • Fritzing is an amazing tool for breadboarding

Ring LED

ring led schematic.png

In my applications, the ring LED is a power indicator. If the raspberry pi is powered and running then the Ring LED should be lit (or blue). If there is no power or the raspberry pi is shut down, then the ring LED should be off.

Ring LED Connections

A breadboard allows me to experiment with the circuit until it works. Male-to-female jumper wires and connecting wires that come with breadboards make this process easier. Male-to-female jumper wires connect the breadboard to the raspberry pi.

The two outer connections on the Ring Push button control the Ring LED. The ground terminal is connected to the Raspberry Pi ground in series with a 330 Ohm resistor. Ground is (-). And GPIO 24 will be connected to the positive terminal (+).

Utility software for python

The Ring LED software requires the rpi.gpio package. The following commands download, build and install rpi.gpio

$ sudo su
# apt-get update
# apt-get install python-pip python-dev gcc
# pip install rpi.gpio
# exit

Exit returns to $ prompt

Ring LED Software

Turn the Ring LED on

$ sudo nano /usr/local/bin/power_ring_led.py

and edit to be:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import subprocess
import argparse
parser = argparse.ArgumentParser()

group = parser.add_mutually_exclusive_group()
group.add_argument("-l", "--light", action="store_true")
group.add_argument("-o", "--off", action="store_true")

# Disable warnings
GPIO.setwarnings(False)

# turn on gpio pin 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
args = parser.parse_args()
if args.light:
	GPIO.output(24,True)
elif args.off:
        GPIO.output(24,False)

Type CTRL-o, CTRL-x ENTER to save the file and exit nano editor

Change the permissions on the file

$ sudo chmod 755 /usr/local/bin/power_ring_led.py

Create a start script:

$ sudo nano /etc/init.d/power_ring_led.sh

and edit to be:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

# Starts and stops power_ring_led.py
# Directory: /etc/init.d/power_ring_led.sh
# Permsissions: chmod 755 /etc/init.d/power_ring_led.sh

# Start or stop power_ring_led.py
case "$1" in
  start)
    /usr/local/bin/power_ring_led.py --light &
    ;;
  stop)
    /usr/local/bin/power_ring_led.py --off &
    ;;
  *)
    echo "Usage: /etc/init.d/power_ring_led.sh {start|stop}"
    exit 1
    ;;
esac
exit

Type CTRL-o, CTRL-x ENTER to save the file and exit nano editor

Change the permissions on the file

$ sudo chmod 755 /etc/init.d/power_ring_led.sh

Register the script to run on boot

$ sudo insserv power_ring_led.sh 

Start the script

$ sudo /etc/init.d/power_ring_led.sh start

The Ring LED should light up!

Reset Switch

raspberry pi 3 run.png
ring led and reset schematic.png

Reset Switch

In general, my Raspberry Pis run all the time. A Raspberry Pi 3 consumes very little power.

Ideally, I would like a power switch to safely shut down the pi, by calling sudo shutdown -h 0 before cutting off the power. However, implementing a power switch is more complicated and will be rarely used.

This is a reset switch, basically, it restarts the Raspberry Pi.

Solder two pins to Raspberry Pi 3

On the Raspberry Pi 3, find the Run holes - see image, box in yellow. The Run holes are near the gpio header.

Remove all cables (power, HDMI, ethernet, etc.) and the Micro SD card from the Raspberry Pi.

From the bottom of the board, solder two short breakaway male pins through the Run holes.

Connect everything back together and make sure it still works.

Connect N01 (normally open) on the momentary switch to one of the Run pins, and C1 to the other Run pin. NC1 (normally closed) is not used.

Press the button, and the system should reboot!