Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Pier8283
Published © GPL3+

Relay control with CAN BUS

In this tutorial we explain how to control the Relay Board V1.0.0 for Raspberry with can bus.

BeginnerProtip1 hour1,314
Relay control with CAN BUS

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
Ii is possible to use all Raspberry Pi B with 40 pins header
×1
Compute Module 4
Raspberry Pi Compute Module 4
×1
Raspberry Pi Compute Module 4 IO Board
×1
CAN BUS Dual Base V2.1.4 Shield for Raspberry
×1
Relays Board V1.0.0
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver
Servo Motor, Premium Male/Male Jumper Wires
Servo Motor, Premium Male/Male Jumper Wires

Story

Read more

Schematics

System connectons

This is the simple layout for board connections

Code

Send CAN BUS command to relay board

Python
#print ("SG Electronic Systems srls")
#print ("Relay V1.0.0 Relays CAN controlled")
# instal ---->>>  pip install python-can
import can
import time
import argparse
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

parser = argparse.ArgumentParser()
parser.add_argument('--rel',
                    dest='relay',
                    help='Define Relay number to set',
                    type=int
                    )
parser.add_argument('--status',
                    dest='status',
                    help='Define Relay status on/off'
                    )					
args = parser.parse_args()
print('Set relay', args.relay, args.status)


#Led 1 CAN BUS Board
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,GPIO.LOW)
#Led 2 CAN BUS Board
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,GPIO.LOW)
can_interface = 'can0'
can_data = [0,0]

if (args.relay >= 1 and args.relay<=8):
	can_data[0]=args.relay
	if (args.status == 'on'):
		can_data[1] = 1
	elif (args.status == 'off'):
		can_data[1] = 0 
	else:
		print("Invalid relay status")
		can_data[1] = 5
	message = can.Message(arbitration_id=0x1B2, is_extended_id=True, data=can_data)
	bus = can.Bus(interface='socketcan', channel='can0', receive_own_messages=True)
	# send a message
	bus.send(message, timeout=0.2)
else:
	print("Invalid relay number")

Receive CAN BUS command for relay setting

Python
# install ---->>>  pip install python-can
import can
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
print ("SG Electronic Systems srls")
print ("Relay V1.0.0 Relays CAN controlled")
#Relay 1
GPIO.setup(6,GPIO.OUT)
GPIO.output(6,GPIO.HIGH)
#Relay 2
GPIO.setup(12,GPIO.OUT)
GPIO.output(12,GPIO.HIGH)
#Relay 3
GPIO.setup(13,GPIO.OUT)
GPIO.output(13,GPIO.HIGH)
#Relay 4
GPIO.setup(19,GPIO.OUT)
GPIO.output(19,GPIO.HIGH)
#Relay 5
GPIO.setup(16,GPIO.OUT)
GPIO.output(16,GPIO.HIGH)
#Relay 6
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.HIGH)
#Relay 7
GPIO.setup(20,GPIO.OUT)
GPIO.output(20,GPIO.HIGH)
#Relay 8
GPIO.setup(21,GPIO.OUT)
GPIO.output(21,GPIO.HIGH)
print ("All relays off")
#Led 1 CAN BUS Board
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,GPIO.LOW)
#Led 2 CAN BUS Board
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,GPIO.LOW)
can_interface = 'can0'
bus = can.interface.Bus(can_interface, bustype='socketcan',bitrate=500000)

def led1_blink():
	GPIO.output(27,GPIO.HIGH)
	time.sleep(1/100)
	GPIO.output(27,GPIO.LOW)
	
def led2_blink():
   	GPIO.output(4,GPIO.HIGH)
	time.sleep(1/100)
	GPIO.output(4,GPIO.LOW)

def rel_on(rel_num):
	if (rel_num == 1):
		GPIO.output(6,GPIO.LOW)
	elif (rel_num == 2):
		GPIO.output(12,GPIO.LOW)
	elif (rel_num == 3):
		GPIO.output(13,GPIO.LOW)
	elif (rel_num == 4):
		GPIO.output(19,GPIO.LOW)
	elif (rel_num == 5):
		GPIO.output(16,GPIO.LOW)
	elif (rel_num == 6):
		GPIO.output(26,GPIO.LOW)
	elif (rel_num == 7):
		GPIO.output(20,GPIO.LOW)
	elif (rel_num == 8):
		GPIO.output(21,GPIO.LOW)	

def rel_off(rel_num):
	if (rel_num == 1):
		GPIO.output(6,GPIO.HIGH)
	elif (rel_num == 2):
		GPIO.output(12,GPIO.HIGH)
	elif (rel_num == 3):
		GPIO.output(13,GPIO.HIGH)
	elif (rel_num == 4):
		GPIO.output(19,GPIO.HIGH)
	elif (rel_num == 5):
		GPIO.output(16,GPIO.HIGH)
	elif (rel_num == 6):
		GPIO.output(26,GPIO.HIGH)
	elif (rel_num == 7):
		GPIO.output(20,GPIO.HIGH)
	elif (rel_num == 8):
		GPIO.output(21,GPIO.HIGH)			

while True:
	message = bus.recv()
	print("Message Received", message)
	if (message.arbitration_id == 0x1B2):
		led1_blink()
		if (message.data[0] >= 1 and message.data[0]<=8):
			led2_blink()
			print("Relay ",  message.data[0])
			if (message.data[1] == 1):
				print("On ")
				rel_on(message.data[0])
			elif (message.data[1] == 0):
				print("Off ")
				rel_off(message.data[0])

Credits

Pier8283

Pier8283

10 projects • 5 followers

Comments