I'm using NodeMCU with 5v relay module. my problem is that when the output of NodeMCU is high the relay is off. Which means that the relay module work in an inverted way so how can I treat this issue while when the output is high the relay become on?
-
Why do you care if HIGH is on and LOW is off, or HIGH is off and LOW is on?Majenko– Majenko07/17/2018 11:06:39Commented Jul 17, 2018 at 11:06
-
@Majenko: consider the reset/booting behavior of all pins but 4+5 and it can matter a lot!dandavis– dandavis07/17/2018 21:32:31Commented Jul 17, 2018 at 21:32
-
Good point. So it all depends: what relay module is it?Majenko– Majenko07/17/2018 22:20:50Commented Jul 17, 2018 at 22:20
2 Answers 2
try changing the connection in relay moduleenter image description here
u can see that there are three pins in a relay. when you connect the output from the NodeMCU between the common and the NC(normally closed) pin of relay, makes the switch close and current flows (when the relay is not powered.) try connecting the output in between common and NO(normally opened). when you connect your output between common and NO the current will only flow when the relay is powered up.
Software solution:
The easiest way is to drive the pin inverted by the NodeMCU, so set it LOW when you want the relay to be activated, otherwise set it HIGH.
Example:
void setup()
{
pinMode(13, OUTPUT); // sets the digital pin 13 as output
digitalWrite(13, HIGH); // switches OFF the relay
}
void loop()
{
digitalWrite(13, LOW); // switches ON the relay
delay(1000); // waits for a second
digitalWrite(13, HIGH); // switches OFF the relay
delay(1000); // waits for a second
}
Hardware solution:
A hardware solution is to use an inverter IC, but since you use a microcontroller it would be only cost space, time and money.
-
Sorry but I'm new to this region, I didn't understand the first solution. Do you mean that there is a pin named inverted I can connect it ground and it will solve the problem or what?عيسى عبد العزيز– عيسى عبد العزيز07/17/2018 09:07:14Commented Jul 17, 2018 at 9:07
-
There is no pin name inverted, but instead of sending a 1 / HIGH signal (digitalWrite(pin, HIGH), when you want to activate the relay, you send a 0 / LOW signal (digitalWrite(pin, LOW)... And in the setup you set it to HIGH (to switch it off initially).Michel Keijzers– Michel Keijzers07/17/2018 09:10:13Commented Jul 17, 2018 at 9:10