[Solved] ESP8266 GPIO + Relay

I’m using ESP8266 development board. It came with NodeMCU flashed. Using latest version Arduino IDE and necessary ESP8266 and Blynk libraries installed.

In IDE I selected NodeMCU as the board for port COM. Through IDE I’m able to flash my program. program runs and connects to Blynk cloud fine and I’m able to use widgets. I have customized functions working fine.

I connected 2 channel relay and connected 5v power, ground wires correctly. Also connected D1 and D2 to relay IN1 and IN2.

Through NodeMCU I can control D1 and D2 and the relay clicks.
– set pin index 1 to GPIO mode, and set the pin to high.
pin=1
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
gpio.write(pin, gpio.LOW)
gpio.write(pin, gpio.IN)

But following code written in Ardunio flashed to the board is not working with relay.

Pin=1
pinMode(Pin, OUTPUT);
digitalWrite(Pin, HIGH);
delay(1000);
digitalWrite(Pin, LOW);
pinMode(Pin, INPUT);

This code doesn’t do anything on the relay. Please let me know if I’m missing anything. Appreciated for your help.

Have you looked at other similar topics on the forum?

Thanks Pavel for the reply. Yes I looked at that example. I’m using same functions to set HIGH and LOW on GPIO pins but some reason it’s not working.

Can you post the exact code of your ESP program? Please start and end your code with three backticks (`), it’ll make really nice code :slight_smile:

What kind of relay do you use? Maybe there is a jumper missing to select if the relay should react on low or high input?
And why do you set the pin to input after your instructions?

I’m using sainsmart 2 channel relay with all jumpers and wires connected look good. It works with NodeMCU.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#define WIFI_SSID "iHome"
#define WPA_PASSWORD "xxx"


// Auth Token for the Blynk App.
char auth[] = "xxx"; 
int DoorPin = 1;

//Time Object
SimpleTimer timer;

//Set Door Pin to HIGH/LOW
BLYNK_WRITE(V1)
{  
  pinMode(DoorPin, OUTPUT);
  if(digitalRead(DoorPin) == LOW)
  {
    digitalWrite(DoorPin, HIGH);    
    Serial.print("Pin set to HIGH");
  }
  else
  {
    digitalWrite(DoorPin, LOW);
    Serial.print("Pin set to LOW");
  }
  pinMode(DoorPin, INPUT);
}


//Try reconnecting to Blynk
void reconnectBlynk()
{
  if (!Blynk.connected())
  {
    if (Blynk.connect())
    {
      Serial.print("Blynk Reconnected");
    }
    else
    {
      Serial.print("Blynk Not reconnected");
    }
  }
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, WIFI_SSID, WPA_PASSWORD); //insert here your SSID and password

  while (Blynk.connect() == false) 
  {
    // Wait until connected
  }
  Serial.print("Blynk Setup");
  
  // Setup a function to do reconnection attempts once a minute
  timer.setInterval(60000, reconnectBlynk);
}

void loop()
{
  // Initiates Blynk Connection
  if (Blynk.connected())
  {
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
}

Have you tried removing all reconnection logics, timers and load the clear example just with BLYNK_WRITE(V1)

Or even better just directly control the digital pin?

Also, do the pinMode(x, OUTPUT) in setup loop only. It just takes up program space and you don’t need to switch input/output mode for the relay.

changed the code to set pin mode in setup()

Yes, I removed all re-connection logics and went with just a basic program. Still didn’t work :fearful:

I’m including screen shots of my Arduino IDE settings. Please let me know if I need to change anything.

Libraries Installed:

Settings in the Tools Menu for the board:

Connections:

Connected red to 5v and brown to ground… White and grey are for D1 and D2 pins

I think I know these relays, I have a couple of those and they switch on with LOW and off with a HIGH signal. Have you tried “manually” controlling the relay’s by just putting a wire on them with +5v or GND? So, bypassing your programming and logic to see if the relays physically work?

I learned the hard way that pins numbers are differently addressed between NodeMCU and Arduino. In NodeMCU D1 and D2 can be referred as pin 1 and 2 but in Arduino we have to use actual GPIO numbers. For D1 GPIO address is 5 and for D2 GPIO 4…

4 Likes

Thanks My Problem Solved

How you fix it?
I’m facing the same problem

@Javaded This topic is very old. Please create a new topic with details of your equipment and issue, thanks .