0

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 Jul 17 '18 at 11:06
  • @Majenko: consider the reset/booting behavior of all pins but 4+5 and it can matter a lot! – dandavis Jul 17 '18 at 21:32
  • Good point. So it all depends: what relay module is it? – Majenko Jul 17 '18 at 22:20
2

try changing the connection in relay moduleenter image description here

enter 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.

0

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? – عيسى عبد العزيز Jul 17 '18 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 Jul 17 '18 at 9:10

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.