1

My 12v DC motor is only turning on once then off forever. It should turn on and off in the loop indefinitely. What am I doing wrong?

I have a simple circuit...

Parts:

  • Raspberry Pi 4 (fully updated libraries as of 1/25/2022)
  • 5v relay
  • 12v DC motor
  • power supply set to 12v

Circuit:

  • Raspberry Pi 4 (fully updated libraries as of 1/25/2022),
  • 5v pin (board pin 2) in the 5v relay's DC+
  • ground pin (board pin 6) in the 5v relay's DC-
  • board pin 7 / GPIO 4 in the relay's IN

On the high-voltage side of the circuit:

  • one of the 12v wires goes directly to the motor
  • the other 12v wire goes into the relay's COM
  • the relay's NO connects to the other terminal of the motor

Python 3 code:

import RPi.GPIO as GPIO
import time
import signal
import sys

treadmill_motor = 7                                     # GPIO 4 / pin 7 controlls the treadmill motor

GPIO.setmode(GPIO.BOARD)                                # use the pin's Board name, not Broadcomm name (board is pin 7, Broadcomm is GPIO4)
GPIO.setup(treadmill_motor, GPIO.OUT)
GPIO.output(treadmill_motor, False)

def signal_handler(sig, frame):
    print('Control+C !!')
    print('ending & cleaning up GPIO...')
    GPIO.cleanup()
    sys.exit()

signal.signal(signal.SIGINT, signal_handler)            # when Control+C is pressed, call signal_handler

if __name__ == '__main__':
    while True:
        print('on')
        GPIO.output(treadmill_motor, GPIO.HIGH)
        time.sleep(3)

        print('off')
        GPIO.output(treadmill_motor, GPIO.LOW)
        time.sleep(1)
3
  • (1) Your relay might have the "once switched on, cannot switch off" problem. The following post might help: electronics.stackexchange.com/questions/505318/…, (2) Also please give us the link to your relay.
    – tlfong01
    Jan 26, 2022 at 8:27
  • What is the specification of your solid state relay?
    – CoderMike
    Jan 26, 2022 at 12:58
  • the problem was two-fold: (1) there was a problem with the Raspberry Pi. the voltage limiter always kicked in; when I swapped RPi's it worked. and (2) I was using a 5v relay when I should have been using a 3v relay Jan 31, 2022 at 20:48

1 Answer 1

-1

I think RPIs output pin protection is kicking in. You are doing these things wrong -

  1. do not drive relay directly from GPIO, use mosfet to drive coil. Something like BS170 will do. Connect diode in reverse to relays input pins.
  2. you are driving 5V relay with 3.3V output,

While you turn pin off, a voltage induced in coil will kick back into RPI, and sometimes it can damage the pin gue to high voltage. I suppose RPI designers were aware for such occasions and put some protection in it, or BCM chip. That is why after reboot or voltage reset it can work again.

Put relay coil to +5V other relay coil to DRAIN of mosfet Source of mosfet to GND. Connect 10k resistor between GATE and SOURCE. Connect 100 ohm resistor between GPIO and GATE.

schematic

simulate this circuit – Schematic created using CircuitLab

6
  • What "output pin protection" - no such thing!
    – Milliways
    Jan 26, 2022 at 8:34
  • could I instead use a 3.3v relay? Jan 26, 2022 at 18:07
  • I doubt that your answer has much relevance to the OP, who claims to have a SSR. Nonetheless if you are going to post circuits you should use best practice. There is NO NEED for the series resistor, indeed all it would do is reduce the Vgs (admittedly only by 1%). The BS170 threshold is typically 2.1V (for very low drain current) but can be higher.
    – Milliways
    Jan 27, 2022 at 0:31
  • k, but OP wants to know if the simple way of buying a 3.3v relay would do the trick :) Jan 27, 2022 at 1:54
  • This is not a good answer for all the reasons @Milliways mentioned. If you'll edit it to make it relevant to the question, I'll remove my downvote.
    – Seamus
    Jan 28, 2022 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.