Yesterday I received my raspberry pi 3 and installed Raspbian. I am trying to use it with this relay, but when i tried to use GPIO using the commands like

echo 4 > /sys/class/gpio/export

The command is executed well but when i execute

echo out > /sys/class/gpio/gpio4/direction

Immediately the relay turns on but when i change the value with

echo 0 /sys/class/gpio/gpio4/value

But when i execute

echo 4 > /sys/class/gpio/unexport

the relay turns off.

Also I tried a python script but not works only remains turned on.

  • Does the relay close when you use echo 1 /sys/class/gpio/gpio4/value and open when you use echo 0 /sys/class/gpio/gpio4/value? – joan May 29 '16 at 7:53

The relay you are using has two characteristics which may explain your problem:

  • "2 Channel 5V...": The logic level of the pi's GPIO pins is not 5V, it is 3.3V. There are of course several 5V live pins which could be used to power the relays, but those cannot be controlled -- they are not really GPIOs. They are just on whenever the pi is. Even if you are correctly using them to supply the relays with power, the GPIO control lines likely cannot drive a signal which counts as "high" for a 5V input (sometimes they will work, but it is not a guarantee1). This means no matter what you do with those pins as outputs, their signal will count as low and pull the line down.

  • "Low level suction close, high level release": This means when the control line is low, the relay contacts are "closed" together, i.e., its circuit will be on and whatever is attached will receive power. When the control line is driven high, the relay contacts will be "released" or opened, and the power to the attached circuit cut.

Since likely you all you can do with the pi's 3.3V outputs is generate what counts here as a "low" signal, you cannot turn the relay off that way. However:

when i execute echo 4 > /sys/class/gpio/unexport the relay turns off

I believe this leaves the GPIO back in its default state, which is as an input. Inputs are not like outputs set low. They are in a "third state", high-Z (aka high impendence). This is because an input doesn't really need to take (much) current in by sinking it with low voltage, it just needs to be sensitive to the state of what it is attached to (therefore, itself should be neither high nor low, which is the "floating" state associated with high-Z). That's not the same as a low voltage signal from an output, which the relay's input would detect (it would be pulled down as well). But an input attached to an input doesn't do that. It is essentially the same as an input attached to nothing, so now the relay goes off. Notice that 0/low voltage is not the same thing as "no voltage".

I'm not sure what the risk here is for damaging the pi, but in general it is probably a bad idea to attach the GPIOs to anything involving 5V logic. To use this relay properly you will need a set of transistors or integrated circuit chip containing such (e.g., a ULN2003A, which you will find various references to here).

1. You don't mention trying to set the value high, so if you have not, you could (keeping in mind what I've said in the last paragraph above). Note that setting the value low after initializing an output does NOT "change the value" -- it leaves it the same since it starts out low by default.

| improve this answer | |
  • The description in that link says 0 turns the relay on, (closes) 1 is off (open) – PaulF8080 May 29 '16 at 7:48
  • Not knowing the type (no schematics) but there seems to be a transistor on board for each relay. I think it at least conceivable that this relay board might work with 3V3 logic. Is this not what the original question implies (though I am lost, there's too many but's there so I lost track when it works and when not). – Ghanima May 29 '16 at 9:43
  • @PaulF8080 That was exactly my point in quoting the "low level suction close, high level release". If it does work w/ 3.3V, the whole issue could just be the OP never tried to set it high, having gotten hung-up on being unable to turn it off by setting the output low. – goldilocks May 29 '16 at 12:16
  • @golilocks Well put. I was confused about his setting a 0 and should have asked for clarification instead of a statement. – PaulF8080 May 29 '16 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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