1
\$\begingroup\$

I'm trying to connect an ESP8266 gpio to one of the relay modules (please check the image).

As I understand with a very basic background, ESP should be able to trigger this relay modules since all the electronics are embedded in them (they are not just single relay components). I have powered both ESP and relay module with different power sources, with the proper VCC for each (3v3 and 5v, sharing GND). But setting HIGH and LOW outputs from ESP have an erratic behavior (some times the relay module triggers, sometimes it just stays in the same state no matter the gpio output delivered).

Now, digging not just this community but some other sources in the web, I know there should same transistor to help driving a higher voltage to the relay input(s) than the one provided but the ESP gpios.

Its important to point out that:

  1. both relay modules work just fine when they are connected to an Arduino Nano, which of course has a HIGH of 5v, and that seems to confirm the issue with the ESP gpio
  2. 2-relay module works "better" than 1-relay module: in terms of accuracy LOW gpio -> relay on; HIGH gpio -> relay off. 1-relay module seems to trigger only once, and then keeps the relay on no matter the gpio value (just like if relay IN is always shorted to ground).
  3. I measured ~2.72v in both IN pins of the 2-relay module, and ~4.33v from the 1-relay module. So that makes me think ESP can't turn off the relay in the later because it will never reach the normal pin voltage value, and almost everything lower will be acknowledge as GND LOW (thus, relay on).

My question is: how can I properly drive LOW and HIGH values from ESP gpio to this relay modules?

Relay modules

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Use a transistor, more specifically a MOSFET, to buffer the 3V3 to 5V. As stated on the relay packaging, they are designed for 5V and will not reliably function with a 3V3 signal. \$\endgroup\$
    – uint128_t
    Apr 3, 2017 at 4:19
  • \$\begingroup\$ Thank you @uint128_t, I have a 2N2222 transistor that I think will do the job. Could you please provide a schematic? I'm assuming the connections are not without some resistors and I'd really like to do it right to prevent frying anything. \$\endgroup\$
    – Dario
    Apr 3, 2017 at 20:54
  • \$\begingroup\$ Ah, disregard my above comment, it doesn't take into account the additionally circuitry feeding the relays. Passerby has a great answer. \$\endgroup\$
    – uint128_t
    Apr 3, 2017 at 20:58

1 Answer 1

6
\$\begingroup\$

Both modules handle things differently. The single relay module uses a direct PNP transistor to control the relay. Active low. Since your base never gets lower than 4.3V (5v - 0.7v VBE drop), it will never turn off. This is also why you see ~4.3V when IN is floating. The leakage current through the VBE diode is enough to cause this, and can also keep the transistor on by itself.

enter image description here

Your 3.3V output can never turn this off. You can fix this with a driver circuit. Any common NPN and two resistors. The logic is inverted due to the NPN. So Output High is NPN on which is PNP on which is Relay on.

schematic

simulate this circuit – Schematic created using CircuitLab

The other module is more complicated. It uses an Opto-coupler to drive the relay transistor.

enter image description here

The opto's led side has multiple problems for your ESP though. It's tied to VCC. It's resistor was chosen based on 5V VCC. It has an inline status led. Since your GPIO is 3.3V, that means it can never really turn the opto couple off. At logic High, the opto sees 5V - 3.3V = 1.7V across everything, so the transistor side stays conducting.

Also, your picture shows that the VCC to JD-VCC jumper is connected. This means you didn't power the Opto side from 3.3V, and you maybe caused the non-5V tolerant ESP GPIO could have been damaged.

But you can test with the same thing as above. A NPN + Base Resistor is all you need. You don't need the pull up (R2 10k).

\$\endgroup\$
17
  • \$\begingroup\$ isn't the 1-relay module an active low? I've tested it with an Arduino uno and triggers the relay on when gpio is LOW \$\endgroup\$
    – Dario
    Apr 3, 2017 at 17:17
  • \$\begingroup\$ @Dario no, it is active high. It's a simple design. Maybe You connected the Normally Closed contacts of relay instead of the Normally opened. So by turning the transistor on, which turns the relay on, you break the connection on the switched side. The 2TY is a npn transistor and those are typically used as low side, active high setups. \$\endgroup\$
    – Passerby
    Apr 3, 2017 at 17:49
  • \$\begingroup\$ I'm considering the specs received by the seller and the fact that by grounding the IN pin, the green LED on the relay board turns on, meaning the relay should be on too. But as you said, the design and also the npn nature of the transistor tells otherwise. So I'll check how the C, NC and NO contacts work (I still didn't connect anything to them) \$\endgroup\$
    – Dario
    Apr 3, 2017 at 18:13
  • \$\begingroup\$ Oh wait, the 2ty is PNP? In that case, you are right, grounding it turn the relay on. But that also means that you can never fully turn it off. To turn off a PNP high side driver, the base needs to be under one diode drop below the collector. For 5v, that means 4.3 or above. So at 3.3V, you are still conducting and the relay stays on. \$\endgroup\$
    – Passerby
    Apr 3, 2017 at 18:17
  • \$\begingroup\$ As I said, I have a poor electronics background, so I've just tested the way the relay module works and I'm pretty sure grounding IN turns ON the relay. Also, the transistor soldered in the module is labeled as "2TY". I don't know if even with that label, the transistor can be NPN or PNP. I've searched and I always find a description like this one. In that case, I think I'm right by saying this is an PNP transistor and the relay module is active low. \$\endgroup\$
    – Dario
    Apr 3, 2017 at 20:28

Your Answer

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

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