5V Relay

Basic tutorial of how to setup a 5V Relay with the Raspberry Pi.

PARTS:
RPI 3 – https://amzn.to/2VA9pQY
4 Amp Power Adapter – https://amzn.to/2CTptWu
16GB micro SD – https://amzn.to/2SFMwd3
120 pcs jumper cable: https://ebay.to/2VAb9cY
5 Volt Relays: https://amzn.to/2SYHxnH
12 Volt Power Supply – https://amzn.to/2srHmWv
5.5×2.1mm Male+Female DC Power Socket: https://amzn.to/2VA5ZNW

SCHEMATIC:

 

CODE:

import RPi.GPIO as GPIO
import time

channel = 21

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)


def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn motor on


def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn motor off


if __name__ == '__main__':
    try:
        motor_on(channel)
        time.sleep(1)
        motor_off(channel)
        time.sleep(1)
        GPIO.cleanup()
    except KeyboardInterrupt:
        GPIO.cleanup()