I'm sorry not being here a month ago, because that relay board is the perfect one for Raspberry Pi and it works smoothlessly.
Main things to consider are three:
- these relays are 5V (Amazon main picture wrongly shows 12V version)
- the inputs are "active low" meaning that connecting them to ground results in corresponding relay coil to receive current and the corresponding NO relay output being connected to the corresponding relay common
- a JDVcc jumper is present to differentiate power between logic section of the board and relays section
You did two things wrong:
- you wired both the relays and the rest of the board to 5V
- your program is supposing to activate the relays with a HIGH pin level
How to solve:
- power the board logic section with 3.3V, connecting Vcc with 3.3V Raspberry Pi pin
- power the relays with 5V, connecting [JD-Vcc][1] with 5V Raspberry Pi pin
- swap the logic levels for ON and OFF in your code, this way (minimum modifications)
import RPi.GPIO as GPIO
import time
import requests
from datetime import datetime
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) # button
GPIO.setup(22, GPIO.OUT) # IN1 on relay
GPIO.setup(23, GPIO.OUT) # IN2 on relay
GPIO.output(22, 1) # set relays initially non active
GPIO.output(23, 1)
try:
while True:
input_state = GPIO.input(18)
if input_state == False:
print('Button push')
GPIO.output(22, 0)
GPIO.output(23, 0)
sleep(0.5)
GPIO.output(22, 1)
GPIO.output(23, 1)
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup()
From an electrical point of view, my measurements says that:
- when both GPIO22 and GPIO23 are LOW, those pins drew less than 1mA together (0.44mA each)
- when both GPIO22 and GPIO23 are HIGH, both 5V and 3.3V pins do not reach 1mA current
- when both relays are active they drew from 5V pin a total of 101mA, more or less half of it each
[both][2] these measurements are totally compatible with Raspberry Pi.
In my current applications these relays do not manage mains (230V here) and I strongly encourage you to do the same, given your apparent level of experience and the concrete risk of electrocution or Raspberry Pi blowing.
[1]: JD seems to mean "relay" in chinese, find this and other more deep and interesting information at https://electronics.stackexchange.com/questions/505318/how-to-properly-use-a-relay-module-with-jd-vcc-from-arduino-raspberry
[2]: What is the maximum current the GPIO pins can output?, https://www.raspberrypi.org/forums/viewtopic.php?p=158827