Relay controlled viy GPIO

1Set up the Raspberry Pi
2Electrical Connection of Sensores and Actuators
2.1AD Converters connected via SPI
2.2Relay on GPIO via Opto-Coupler
2.3PWM Control of Ventilation via i2c
2.4OneWire Temperature Sensor
2.5Ultra-sonic Distrance Measurement
2.6Serial RS232 connection
2.7Stepper Motor Control
2.8Get Weather Forecast
3Heating Control
4Ventilation Control (Paul Lüftung, Thermos 200)
5 z-wave


Schematic direct connection

Raspberry_Outputs

Connection of the relays to the RaspberryPi. Some output pin (GPIO22) is used to stear a relay. To keep the currenct over the RaspberryPi < 3 mA I use a resistor of 1.2 kOhm and a Transistor to switch the control current of the relay. The diode 1N4148 is used to get rid of inductive voltage spike when relay is shut off.

The simple solution above came with the disadvantage that, at some point of my development, I observed voltage drops due to operation of the relays, that affected my temperature measurements!

Therefore I use opto couplers to have a galvanic separation of my control board and the relays. By this design I moved the relays working at dangarous 230 V to an exterior PCB and may use indiendent supply voltage of 5V or 12V.

Raspberry_Relay.svg

Since the opto coupler PC817 draws a current of 20 mA, which is too much for the GPIO, I use the transistor BC337 as amplifier, R1 = 1.2 kOhm and R2 = 200 Ohm.

Test the GPIO Pin

To test the output relays you may toggle the appropriate pin via shel:
This works only if you have root privilegues
sudo -i
Activate port 22 and configure it as output pin
echo "22" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio22/direction

Now toggle the port
echo "1" > /sys/class/gpio/gpio22/value
echo "0" > /sys/class/gpio/gpio22/value
If you did connect a multimeter to the relay you will hear the beep.

Access the GPIO Pins

I created a new group
sudo -i
groupadd -f --system gpio

and added me to that group
nano /etc/group

gpio:x:99:pi
Than I created the access to the four pins used by me:
echo "22" > /sys/class/gpio/export
echo "23" > /sys/class/gpio/export
echo "24" > /sys/class/gpio/export
echo "25" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio22/direction
echo "out" > /sys/class/gpio/gpio23/direction
echo "out" > /sys/class/gpio/gpio24/direction
echo "out" > /sys/class/gpio/gpio25/direction

and changed owner and writing permissions:
chown -R root:gpio /sys/class/gpio/gpio22/*
chown -R root:gpio /sys/class/gpio/gpio23/*
chown -R root:gpio /sys/class/gpio/gpio24/*
chown -R root:gpio /sys/class/gpio/gpio25/*
chmod g+w /sys/class/gpio/gpio22/value
chmod g+w /sys/class/gpio/gpio23/value
chmod g+w /sys/class/gpio/gpio24/value
chmod g+w /sys/class/gpio/gpio25/value

This way I can switch the relays with my user privilegues - as long as I do not reboot the PI.

Finally I"ve put these tasks to the startup script /etc/init.d/S999raspberry
!/sbin/sh
#
# Template for custom rc script
PATH=/usr/sbin:/usr/bin:/sbin
export PATH

case $1 in
"start_msg")
        echo "Starting the RaspberryPi ports"
        ;;

"stop_msg")
        echo "Stopping the RaspberryPi ports"
        ;;

"start")
         # All required instruction to start the process. Can be a call to another script.
        #sudo echo "4" > /sys/class/gpio/export
        sudo echo "14" > /sys/class/gpio/export
        sudo echo "15" > /sys/class/gpio/export
        sudo echo "17" > /sys/class/gpio/export
        sudo echo "18" > /sys/class/gpio/export
        sudo echo "27" > /sys/class/gpio/export
        sudo echo "22" > /sys/class/gpio/export
        sudo echo "23" > /sys/class/gpio/export
        sudo echo "24" > /sys/class/gpio/export
        sudo echo "25" > /sys/class/gpio/export
        #sudo echo "out" > /sys/class/gpio/gpio4/direction
        sudo echo "in"  > /sys/class/gpio/gpio14/direction
        sudo echo "out" > /sys/class/gpio/gpio15/direction
        sudo echo "out" > /sys/class/gpio/gpio17/direction
        sudo echo "out" > /sys/class/gpio/gpio18/direction
        sudo echo "out" > /sys/class/gpio/gpio27/direction
        sudo echo "out" > /sys/class/gpio/gpio22/direction
        sudo echo "out" > /sys/class/gpio/gpio23/direction
        sudo echo "out" > /sys/class/gpio/gpio24/direction
        sudo echo "out" > /sys/class/gpio/gpio25/direction
        #sudo /bin/chown -R root:gpio /sys/class/gpio/gpio4/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio14/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio15/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio17/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio18/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio27/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio22/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio23/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio24/*
        sudo /bin/chown -R root:gpio /sys/class/gpio/gpio25/*
        #sudo /bin/chmod g+w /sys/class/gpio/gpio4/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio14/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio15/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio17/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio18/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio27/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio22/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio23/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio24/value
        sudo /bin/chmod g+w /sys/class/gpio/gpio25/value
        sudo chown root:gpio /dev/spidev0.*
        sudo chmod g+rw /dev/spidev0.*
        sudo chown root.gpio /dev/gpiomem
        sudo chmod g+rw /dev/gpiomem
        cd /var/www/ && sudo -u www-data php HV_loop.php&
        rval=1
        ;;


"stop")
        # All required instruction to stop the process. Can be a call to another script.
        pid=$(pgrep -f HV_loop)
        echo $pid
        kill $pid
        pid=$(pgrep -f thermos200)
        kill $pid

        rval=1
        ;;

*)
        echo "usage: $0 {start|stop|start_msg|stop_msg}"
        rval=1
        ;;

*)
        echo "usage: $0 {start|stop|start_msg|stop_msg}"
        rval=1
        ;;
esac
exit $rval
and activated this by
update-rc.d S999raspberry start NN 3

Such init.d script may be tested by
sudo /etc/init.d/S999raspberry start

The apache may be restarted by:
sudo /etc/init.d/apache2 restart

Then I added the GPIO access to my test program and checked it worked.

Configuration of GPIO Pins

For further information on how to configre the GPIO pins have a look to Raspberry Pi GPIO Belegung. There the GPIO access via bash, wiringPI or python is explained.

Login
Valid HTML 4.01!