3

I connected a protective relay to raspberry pi +5V (to relay VCC), GND (to relay GND) and pin 17 ports (to relay IN) . This relay controls a DC motor. The DC motor has a separate power supply connected to it. This power supply goes over the relay thus by opening and closing the relay I will be able to control the motor actually.

When I start raspberry I can run the motor continuously. I wrote a code like below to control the power going to relay but it didn't work. Similar code was working for different pins to control a led component.

import RPi.GPIO as GPIO 
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(2  , GPIO.OUT)
while True:
    GPIO.output(2, True)
    time.sleep(10)
    GPIO.output(2, False)
    time.sleep(10) 

Is there another way to control the relay without manually disconnecting it from the board?

Edit: This is the picture of my relay.. enter image description here

Edit2: I changed the pin 17 to pin 11 then relay started to working properly. I can now control it by the code

import RPi.GPIO as GPIO 
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11  , GPIO.OUT)
while True:
    GPIO.output(11, True)
    time.sleep(10)
    GPIO.output(11, False)
    time.sleep(10) 
Ghanima
  • 15,958
  • 17
  • 65
  • 125

2 Answers2

5

I'm not sure from your answer whether you're using a relay module or a bare relay. I assumed the latter in writing the following.

Notes:

  1. You'll need a flyback diode to protect whatever output pin you're using. When you switch off an inductive load (i.e. a coil), which includes relays and motors, a large voltage spike can be generated as the coil resists changes in current. This spike is beyond the capabilities of logic outputs and small transistors, even for rather small coils.
  2. You might be able to find a relay that doesn't draw too much current (16mA per GPIO pin, total max 51mA according to Element 14's forum) and that can also drive your load (you don't say how big the DC motor is).
  3. There's also more choice of relays at 5V than at 3V (and it sopunds like you might have a 5V part already).

For developing something I'd use the open collector outputs of my gertboard, but to buy a gertboard specially would be excessive. I suggest building an open collector circuit (you will need a transistor and probably a resistor in addition to the relay and the diode I mentioned above) attached to the GPIO pin. The exact components aren't too important, with a junk box of through-hole electronics you'd have good chnce of finding something that would work. Alternatively a very quick ebay search suggest you could get the parts for £3 including postage (and you'd have enough to do this several times).

A complete alternative is a solid state relay, which is a semiconductor chip that serves the same purpose as a relay but works completely differently.

Chris H
  • 585
  • 2
  • 14
2

The 5v pin on the Raspberry pi is simply not controllable, at all.

Your edit provides a picture of a relay module with an optocoupler input. It is quite likey that you can successfully drive this from a pi GPIO output (I've used similar ones with an ATmega at 3.3v), ideally using low side switching where the pi connects to the cathode of the LED and the anode goes to the 3.3v rail through an appropriate current limit resistor (the resistor can really go on either side as long as it is in the path). You would then output a "low" to enable the relay.

Ultimately you should determine the part number of the optocoupler and check its data sheet for the required current. If that is more than the pi can handle, you can use the pi to enable an NPN transistor which functions as a low-side switch in the optocoupler LED's drive circuit (much as how TV remote projects typically drive high brightness IR LED's), but that will probably not be necessary

Chris Stratton
  • 993
  • 5
  • 11