Solid State Relay

Basic tutorial of how to setup a Solid State Relay with the Raspberry Pi.


 

PARTS:
RPI 3 B+ – https://amzn.to/2DcfywB
4 Amp Power Adapter – https://amzn.to/2Q77KPL
16GB Micro SD – https://amzn.to/2DaPZMF
4 Channel SSR – https://ebay.to/2zwpD3Z
Jumper Cables – https://ebay.to/2KF6GAr
DC wire – https://ebay.to/2Skv1y3

*Optional*
DC variable power supply – https://ebay.to/2E5XDYQ
Yellow DC motors – https://ebay.to/2SoJcCD

 

SCHEMATIC:

VCC >> 5V
GND >> GND
CH1 >> GPIO17
CH2 >> GPIO27
CH3 >> GPIO22
CH4 >> GPIO23

 

CODE:

import RPi.GPIO as GPIO
import time

# map SSR channel to GPIO output
channel_1 = 17
channel_2 = 27
channel_3 = 22
channel_4 = 23

def setup():
    """setup GPIOs"""
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(channel_1, GPIO.OUT)
    GPIO.setup(channel_2, GPIO.OUT)
    GPIO.setup(channel_3, GPIO.OUT)
    GPIO.setup(channel_4, GPIO.OUT)


def motors_on(one,two,three,four):
    """turn motors on"""
    print('motors on')
    GPIO.output(one, GPIO.HIGH)
    GPIO.output(two, GPIO.HIGH)
    GPIO.output(three, GPIO.HIGH)
    GPIO.output(four, GPIO.HIGH)


def motors_off(one,two,three,four):
    """turn motors off"""
    print('motors off')
    GPIO.output(one, GPIO.LOW)
    GPIO.output(two, GPIO.LOW)
    GPIO.output(three, GPIO.LOW)
    GPIO.output(four, GPIO.LOW)


if __name__ == '__main__':
    try:
        setup()
        motors_on(channel_1,channel_2,channel_3,channel_4)
        time.sleep(10)
        motors_off(channel_1,channel_2,channel_3,channel_4)
        time.sleep(2)
        GPIO.cleanup()
    except KeyboardInterrupt: # exit gracefully
        motors_off(channel_1,channel_2,channel_3,channel_4)
        GPIO.cleanup()