# Relay interface

<abstract>
Two relays are available on the CM-Home board. This article explais how to use them.
</abstract>

<img src="./relays.jpg" class="img-responsive">

Features:

* Normally-Opened and Normally-Closed contacts available on screw terminals 
* Max rated: 24 VAC/DC @ 1A 
* Snubbers already on-board to increase reliability with inductive loads

GPIO used

* RL1 (left relay) GPIO21
* RL2 (right relay) GPIO22

### Manage the relays using RPi.GPIO

	import RPi.GPIO as GPIO
	import time
	
	RL1=21
	RL2=22

	GPIO.setmode(GPIO.BCM)
	GPIO.setwarnings(False)
	GPIO.setup(RL1,GPIO.OUT)
	GPIO.setup(RL2,GPIO.OUT)

	for i in range(5):
	    GPIO.output(RL1,GPIO.LOW)
	    GPIO.output(RL2,GPIO.HIGH)
	    time.sleep(0.5)
	
	    GPIO.output(RL1,GPIO.HIGH)
	    GPIO.output(RL2,GPIO.LOW)
	    time.sleep(0.5)
	
	GPIO.output(RL1,GPIO.LOW)
	GPIO.output(RL2,GPIO.LOW)

### Manage the relays using GPIOD


* Set RL1 on: <code>gpioset $(gpiofind GPIO21)=1</code>
* Set RL1 off: <code>gpioset $(gpiofind GPIO21)=0</code>
* Set RL2 on: <code>gpioset $(gpiofind GPIO22)=1</code>
* Set RL2 off: <code>gpioset $(gpiofind GPIO22)=0</code>

List all GPIO chips, print their labels and number of GPIO lines.

	$ gpiodetect
	
	gpiochip0 [pinctrl-bcm2711] (58 lines)
	gpiochip1 [brcmvirt-gpio] (2 lines)
	gpiochip2 [raspberrypi-exp-gpio] (8 lines)

Print information about all lines of the specified GPIO chip

	$ gpioinfo pinctrl-bcm2711

	line   0:     "ID_SDA"       unused   input  active-high 
	line   1:     "ID_SCL"       unused   input  active-high 
	line   2:       "SDA1"       unused   input  active-high 
	line   3:       "SCL1"       unused   input  active-high 
	line   4:  "GPIO_GCLK"       unused   input  active-high 
	line   5:      "GPIO5"       unused   input  active-high 
	line   6:      "GPIO6"       unused   input  active-high 
	...	
	
### Python example using gpiod

Turn on and off the relay RL1 each seconds

	import gpiod
	import time
	
	chip=gpiod.Chip('gpiochip0')
	line = gpiod.find_line("GPIO21")
	lines = chip.get_lines([line.offset()])
	lines.request(consumer='foobar', type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0])
	
	while True:
		print("RL1 on")
	    lines.set_values([1])
	    time.sleep(1)
		print("RL1 on")
	    lines.set_values([0])
	    time.sleep(1)



<hr>

@include='adv_cmpanel'
