so this is my first time trying to use a Raspberry pi and I wanted to automate a motor using a 5v relay module. I'm using a Raspberry Pi Zero W. I am using a 5v 2amp power supply to power the Raspberry pi.
The relay's VCC pin is connected to the pi's physical pin 2 (5v power). The relay's IN pin is connected to physical pin 16 (GPIO 23). The relay's GND pin is connected to a ground pin on the pi. When I power up the pi, both the red and green LEDs turn on on the relay and I hear a click from the relay. If I disconnect the IN pin, the green LED turns off and I hear another click.
Obviously, I'd like to control the relay from Python code instead of disconnecting wires. Here's the code:
import RPi.GPIO as GPIO import time pin = 16 GPIO.setmode(GPIO.BOARD) GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) try: while True: GPIO.output(pin, True) time.sleep(0.5) GPIO.output(pin, False) except KeyboardInterrupt: GPIO.cleanup()
When I run the script, nothing happens. There are no error messages, no clicks from the relay, and the LEDs stay the same. This has been the case for every GPIO pin for IN that I've tried. I've tried using the other 5v pin on the Pi for VCC as well, nothing. Changing False or True in GPIO.output to GPIO.HIGH, GPIO.LOW, 1, or 0, has also changed nothing.
Switching the pin numbering using GPIO.BCM instead of BOARD has also not changed anything, even when using a variety of pins.
I also followed this tutorial exactly, and nothing (I even have the exact same relay module): https://www.instructables.com/5V-Relay-Raspberry-Pi/
At this point, I'm out of ideas as to what is going wrong. The GPIO pins are obviously fine because the relay is detecting a signal from it when the board is first powered up, it's just that the code doesn't seem to be doing anything.
I've done most of my testing without the motor connected to my relay, but I've also tried it with the motor connected. Whether or not the motor is connected makes no difference. I just want to be able to simply switch the relay, but it seems that's not working. Any ideas on what's going wrong, or do you know anything I could do to troubleshoot? Thanks. It's possible I might be missing something really obvious because this is my first time using a Raspberry pi. Let me know if you need more information.
This is the relay module I'm using: https://www.ebay.com/itm/1-Channel-DC-5V-Relay-Switch-Board-Module-for-Arduino-Raspberry-Pi-PIC-ARM/233756832628
I've also tried using this relay module, but on this one a red LED corresponding to the channel I'm using lights up, and I never hear a click from the relay (even when powering up the board): https://www.ebay.com/itm/2-Channel-DC-5V-Relay-Switch-Module-for-Arduino-Raspberry-Pi-ARM-AVR-DSP/383012579928?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649
Picture: (on the relay module, from left to right, the pins are: IN - GND - VCC)