How to connect device through module with relays to Raspberry Pi or CM4 IO board
· ☕ 7 min read
· 🐧 sysadmin
I will walk you through the installation and configuration steps in this article so you can use the same module with relays to connect it with Raspberry Pi or CM4 IO board.
Here is a video tutorial; continue reading for a list of written instructions.
What You Need For This Tutorial
Raspberry Pi 4 or CM4 IO board with Compute Module 4
Micro SD Card or eMMC or NVMe/SSD drive
Power Supply aprropriate for a Raspberry Pi or CM4 IO board.
Module with two relays
Power supply for the module with relays
3 female to female jumper cables
2 female to male jumper cables
7 male to male jumper cables
Exercises to complete:
Connect wiring according to schema between module with relays and Raspberry Pi / CM4 IO board.
Use a Python script to test the module with relays.
Check does the module work as expected.
Introduction
I decided to connect the pump and fan using a module with two relays in order to control them simply because controlling the 5V pump is not possible because it is not a PWM device, and controlling solely the Noctua 5V fan with PWM didn’t make much sense to me. When the temperature rises above a predetermined threshold (for instance, 50 degrees Celsius), I wanted both devices to turn on, and I wanted them to turn off when the temperature falls below the predetermined threshold.
Due to the fact that my GND will be the low state (logical zero) from the GPIO PINs, I do not connect the GND from the Raspberry Pi / CM4 IO.
The power supply of the circuit is created by the jumper (jumper) shorting JD-VCC along with VCC and the power supply of the optoisoloator with the power supply of the relay coil. We can isolate the Raspberry Pi / CM4 circuit from the relay control circuit if we remove this jumper.The red wire from the external 5V power source should then be connected to JD-VCC in addition to being used. The jumper should be left in place if I do not wish to utilize an external power source for the relays because doing so increases the load on the Raspberry Pi / CM4’s 5V line and causes (small) interference while switching the relays.
# getting the main GPIO libraryimportRPi.GPIOasGPIO# getting the time libraryimporttime# setting a current modeGPIO.setmode(GPIO.BCM)#removing the warings GPIO.setwarnings(False)#creating a list (array) with the number of GPIO's that we use pins=[12,13]#setting the mode for all pins so all will be switched on GPIO.setup(pins,GPIO.OUT)#for loop where pin = 12 next 13forpininpins:#setting the GPIO to HIGH or 1 or trueGPIO.output(pin,GPIO.HIGH)print("GPIO HIGH")#wait 5 secondtime.sleep(5)print("sleep")#setting the GPIO to LOW or 0 or falseGPIO.output(pin,GPIO.LOW)print("GPIO LOW")#wait 5 secondtime.sleep(5)print("sleep")#Checking if the current relay is running and printing it ifnotGPIO.input(pin):print("Pin "+str(pin)+" is working")#same but the difference is that we have #for loop where pin = 12 next 13# backwardsprint("Backwards")forpininreversed(pins):GPIO.output(pin,GPIO.HIGH)print("GPIO HIGH")time.sleep(5)print("sleep")GPIO.output(pin,GPIO.LOW)print("GPIO LOW")time.sleep(5)print("sleep")#cleaning all GPIO's GPIO.cleanup()print("GPIO cleanup")print("Shutdown All relays")
Run the script
1
python3 relay.py
Issues described during the prototyping procedure.
The issue of undervoltage and throttling emerges when we add more devices—in my instance, fans—to the Raspberry Pi / CM4’s GPIO pins when I have two devices connected through a module with relays. The HDMI-connected display becomes dark (image is lost). Ensure that it is powered properly and that this behavior is usual.
#!/usr/bin/python3# getting the os libraryimportos# getting the main GPIO libraryimportRPi.GPIOasGPIO# getting the time libraryimporttime# getting the datetime libraryimportdatetime# getting the sys libraryimportsys# */1 * * * * cd /home/username/ && sudo python3 relay.py# Every minute, a crontab will check the temperature. # The script will activate the fan and pump if the temperature # rises above 31 degrees Celsius and keep doing so until it falls # below 29 degrees Celsius. The script will then end, turning off # the fan and pump simultaneously. The previous script will quit # if it is launched while the new one is running, leaving the Pi/CM4 # in hell and unable to ever go below the ACTION_END number.# Identify the pins that operate the relays. FAN_PIN=12# GPIO for fanPUMP_PIN=13# GPIO for pumpPINS=[FAN_PIN,PUMP_PIN]# Array to handle both fan and pump# Set temperature thresholds. ACTION_START=31ACTION_END=29# Get what action. If you manually turning on/off the fan and pumpaction=sys.argv.pop()defGPIOsetup():# removing the waringsGPIO.setwarnings(False)# setting a current modeGPIO.setmode(GPIO.BCM)# for loop where pin = 12 next 13forpininPINS:#setting the mode for all pins so all will be switched onGPIO.setup(pin,GPIO.OUT)defdevicesON():GPIOsetup()forpininPINS:GPIO.output(pin,GPIO.LOW)#fan and pump on. Setting the GPIO to LOW or 0 or falsereturn()defdevicesOFF():GPIOsetup()forpininPINS:GPIO.output(pin,GPIO.HIGH)#fan and pump off. Setting the GPIO to HIGH or 1 or truereturn()# Get the temperature from the system defget_temp_from_system():res=os.popen('vcgencmd measure_temp').readline()return(res.replace("temp=","").replace("'C\n",""))defcheck_devices():GPIOsetup()returnall(GPIO.input(pin)forpininPINS)defrun():current_date=datetime.datetime.now()temp=get_temp_from_system()iffloat(temp)>=ACTION_START:print(temp+' @ '+str(current_date))ifcheck_devices():print('Fan and pump are off... Starting them.')devicesON()else:print('Fan and pump are on')eliffloat(temp)<=ACTION_END:print(temp+' @ '+str(current_date))ifnotcheck_devices():print('Fan and pump are on... Shuting them down.')devicesOFF()GPIO.cleanup()return1else:print('Fan and pump are off')else:passifaction=="on":print('Turning fan and pump on')devicesON()elifaction=="off":print('Turning fan and pump off')devicesOFF()# first check if script is already runningifnotcheck_devices():print('Fan and pump are on, script must be running from another instance...')else:temp=get_temp_from_system()iffloat(temp)<ACTION_START:print('Pi/CM4 is operating under normal temperatures.')else:try:while(True):tmp=run()iftmp==1:breakexceptKeyboardInterrupt:devicesOFF()GPIO.cleanup()finally:devicesOFF()GPIO.cleanup()
Making The Script Run Automatically On Startup
Now that the script is running, we would like it to do so automatically when the computer starts up because it will cease as soon as we close the terminal window. We’re going to use crontab to accomplish this.
Enter the next command to launch crontab:
1
crontab –e
If you’re opening crontab for the first time, you’ll be asked to choose an editor; choose 1 and press enter.
To execute the script, include the following line at the end of the file:
When you finish, save the crontab file, and then reboot your Pi/CM4 to check if everything is operating as it should.
If you followed the steps exactly, you should now have a functional module with relays that launches whenever your Raspberry Pi / CM4 boots up. If you haven’t already, you can go ahead and put it into your case immediately.
Please share your thoughts on this tutorial in the space provided below. Please share your thoughts and recommendations with me.