I needed a relaymodule and I specifically needed one that is the standard Input LOW -> relay not active, input HIGH, relay active.
Once that used to be the norm for relaymodules, but lately many relaymodules have an optocoupler on board, that inverts the signal.
Anyway, I got myself a module kinda like in the picture above and it stated ‘Active HIGH’. Judging from the picture indeed it seemed OK, just having the standard transistor or FET, driving the relay, a flyback diode and 3 1k resistors that I presumed to be for the basis of the transistor and for the two LED’s for “Power ON” and for “relay activated”.
I ordered it at Aliexpress.
It turned out te be a decent relay, but sadly it worked just the other way around: Active LOW. Darn!
Ofcourse I could solve it in Software, by always sending a HIGH signal to deactivate the relay in the setup and I could opt to use the NC contact rather than the NO contact, but that would mean the relay would be on if ‘not in use’.
It seemed better to just add an inverter, but before I wanted to do that I was wondering what the circuit on the board looked like. Well, that wasn’t so hard to find out. As I already expected it turned out to be a PNP transistor (the 2TY or S8550) driving the relay. Why? I do not know.
Apart from having an inverted signal response, this circuit has another drawback: The signal voltage needs to be near the Vcc to shut off the relay. The Vbe saturation voltage is -1.2Volt. So if fed with 5 Volt, anything lower than 5-1.2=3.8V will keep the transistor in saturation.That means that you cannot feed the circuit with 5Volt and use a 3.3 Volt microprocessor to give the input signal, because a HIGH of 3.3 Volt is not high enough to close the PNP transistor. That means that if you want to use it on a raspberry Pi or an ESP8266, you will have to use the 3.3 Volt pin to feed the relay. The relay is a 5 Volt type, but it will work on 3.3 Volt but at 70 ohms it draws an extra 47mA from the 3v3 LDO.
Would this have been a standard NPN driven relay, the Voltage needed for the relay would have been irrelevant. The only thing that would matter is if the trigger voltage would be high enough to open the transistor.
To invert the signal transistor seemed the easiest. It would require 1 NPN transistor, a 10k collector resistor and a 1 k base resistor. But add a diode and you already have a fully functional driver for a bare relay. However, from an earlier project, I had a spare inverter gate, a 74AHC1G04 that seemed ideal for the job, albeit a tad tiny for my eyes.
The question remains then if using that inverter would also solve the issue of the relay voltage. The datasheet teaches u that roughly the HIGH output of the gate is 0.5 Volt below Vcc. So at 5Volt, we may have just enough to close the relay. However, at 5Volt Vcc the gate needs at least 3.8 Volt on its input to see it as a HIGH.That means that with using the inverter, if we supply the circuit with 5Volt, we MAY be able to shut off the relay, but we will likely not be able to switch it on again with a 3.3Volt signal.
So it seems the single gate is not the best solution, which brings us back again to using a transistor:
The 4.7-10k resistor is probably not really necessary, as it is sufficient to pull down the collector (and thus the signal line of the module) to activate the relay. Yet it might still be a good practice to add it to be able to pull the line up to deactivate the relay.
A tiny 5×3 hole perfboard is enough to house 2 connectors, a transistor and 1 (or 2) SMD resistors.
With this slight ‘expansion’ it is now possible to use a 3V3 signal as input and 5Volt on the relay. Also the signal is no longer inverted: A HIGH on the input will pull the collector LOW and thus activate the relay board. A LOW on the input will pull the collector HIGH (if a 10k resistor is used).
As a final word, the added resistor when left floating, will function as an amplifier and even small voltage on the base (like when you are keeping it in your hand, will lead to a current through the collector and thus pull the base of Q1 a bit low(er). Q1 will also work as an amplifier and will start allowing current to flow through the relay and the LED. The Green LED may start to glow and possibly the relay may begin to flutter.
This should not happen if the entire circuit is attached to a micro’ s GPIO pin that has a defined LOW or HIGH state. You could always opt to have a 10k resistor pull the input of our circuit LOW. However……should you add this pull-down resistor, you can no longer use it on e.g. GPIO0 and GPIO2 of an ESP8266-01 as these pins need to be pulled high for normal operation.