Bitbull Tech Notes - home of free minds ...

Control Relais Shield by Raspberry pi GPIO

I needed to use a Arduino relay shield with arduino GPIO.

Ther are some nice tools around which can do everything, but for me, I need a solid and robust on/off handler.

This are the tools:

http://raspberrypiguide.de/howtos/raspberry-pi-gpio-how-to/

This is the simple Script I wrote:

#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ $# -ne 2 ] && echo 'usage: gpio-relay.sh PIN [0|1]'
[ $# -ne 2 ] && exit 1

PIN=$1
MODE=$2

test -L /sys/class/gpio/gpio$PIN || echo $PIN > /sys/class/gpio/export
test -L /sys/class/gpio/gpio$PIN || exit 1
cat /sys/class/gpio/gpio$PIN/direction | grep -q out || echo out > /sys/class/gpio/gpio$PIN/direction
cat /sys/class/gpio/gpio$PIN/direction | grep -q out || exit 1
echo $MODE > /sys/class/gpio/gpio$PIN/value
echo "OUT-PIN: $PIN / VALUE: $(cat /sys/class/gpio/gpio$PIN/value)"

Result: fire same command multiple time, does no unneeded switch of  relay state

This are the PINs on RPI:

 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | OUT  | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | ALT0 | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | ALT0 | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 |   IN | 0 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+

FAZIT:

IT WORKS GREAT!!!

Do not use RxD and TxD PINs for Relay if there are other free PINs.
Because this two PINs are flickering during reboot of PI. :-)

 

 

 

Install watchdog on Raspberry Pi debian

With Raspberry Pi, you can build nice things, but sometimes it is hanging around (sdcard, power supply, ...).

To avoid such a state, I recommend to install Watchdog.

Here are my notes for debian 7.9 on Rasberry Pi B+

Install the Software:

apt-get update
apt-get dist-upgrade
rpi-update
reboot
apt-get install watchdog sendmail-bin

ls /etc/rc2.d/S0*watchdog
update-rc.d watchdog disable
ls /etc/rc2.d/S0*watchdog

modprobe bcm2708_wdog
echo "bcm2708_wdog" | tee -a /etc/modules
lsmod | grep bcm2708_wdog

 

Configure Watchdog as you need: /etc/watchdog.conf

#ping			= 192.168.223.254
#file			= /tmp/easyiot.log
#change                  = 600
watchdog-timeout = 10 max-load-1 = 12 min-memory = 1 watchdog-device = /dev/watchdog pidfile = /var/run/watchdog.pid realtime = yes priority = 1

 

Test it!!!

 

Enable it:

ls /etc/rc2.d/S0*watchdog
update-rc.d watchdog defaults
ls /etc/rc2.d/S0*watchdog
reboot

 

Test it again:

tail -f /var/log/syslog

 

Note:

There is a bug in debian watchdog startscript, which produces error message on watchdog restart.
But this is not a problem when booting the pi:

watchdog[2021]: cannot open /dev/watchdog (errno = 16 = 'Device or resource busy')

 

Home