"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Sunday, 3 June 2018

Simple hardware interface for the Raspberry Pi Zero


I spent some hours, last weekend, working to another little step into my Raspberry Zero camera project. After experimenting remote software interface possibilities I started building some minimal bare hardware interface in order to, at least, safely turn on and off the Raspberry Pi.



A bare minimum interface
The bare minimum hardware interface for a Raspberry project is made of a power push-button and a status LED telling if the device is still active or it can be safely removed from power.
Plenty of tutorials are available on the Internet, I mostly followed instructions from here and here.
The power status information is directly provided from the UART pin without the need of writing additional software. UART must be enabled in ‘/boot/config.txt’ configuration file


enable_uart=1

Then I just connected a LED diode between UART pin and ground (trough a current limiting resistor of course).
Also the wake-up functionality is implemented by briefly connecting GPIO pin 6 to ground trough a normally-open push button.
The power-off functionality, at last, need writing a simple Python script listening for power button press then calling the shutdown command.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)
subprocess.call(['shutdown', '-h', 'now'], shell=False)

I then copied the script in ‘usrl/local/bin’ folder and made it executable

sudo mv listen-4-shutdown.py /usr/local/bin/sudo chmod +x /usr/local/bin/listen-4-shutdown.py

The script must run as root, in order to have shutdown privilege, and must start as the Raspberry is powered on. This can be achieved by writing a start-up shell script that allow the python program to be executed as a service.
#! /bin/sh
### BEGIN INIT INFO
# Provides: listen-for-shutdown.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting listen-4-shutdown.py"
/usr/local/bin/listen-4-shutdown.py &
;;
stop)
echo "Stopping listen-4-shutdown.py"
pkill -f /usr/local/bin/listen-4-shutdown.py
;;
*)
echo "Usage: /etc/init.d/listen-4-shutdown {start|stop}"
exit 1
;;
esac
exit 0
This shell script can be then installed by moving it in “/etc/unit.d/” folder and activated with the following command
sudo mv listen-4-shutdown.sh /etc/init.d/
sudo chmod +x /etc/init.d/listen-4-shutdown.sh
sudo update-rc.d listen-for-shutdown.sh defaults


What’s next ...

A simple point-and-click camera needs at least one more push-button to take shots and one more, software controlled, status LED to inform user camera is working. So next step will be a more complex Python script to handle multiple buttons and LED.

No comments :

Post a Comment