2 Channel Relay Problems

I have created a project using a WEMOS mini D1V2 and a 2 channel relay that allows me to control two different lights with separate momentary switches, the blynk app, or Amazon Alexa. The problem I am having is that when I turn on relay2 using any of the above mentioned methods, relay1 randomly turns on. However, when I turn on relay1 using any of the above mentioned methods, relay2 functions as intended (i.e. it does not turn on).

Here is my code. Does anyone see any logic errors I might be missing. I have checked all hardware and wiring connects and everything seems in order. Thanks in advance for the help.

//Common libraries
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"

//WiFiManager specific libraries
#include <DNSServer.h>          //Local DNS server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>   //Local WebServer used to serve as the configuration portal
#include <WiFiManager.h>        //Core library

//ArduinoOTA specific libraries
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#define D0 16
#define D1 5  // I2C Bus SCL (clock)
#define D2 4  // I2C Bus SDA (data)
#define D3 0
#define D4 2  // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO 
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3  // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)

#define VPIN1 V7  //Use a unique virtual pin for each device using the same token / dashboard
                  //In order to control a multitude of devices with a single Blynk dashboard, each ESP8266
                  //should be programmed with a unique virtual pin assignment, corresponding to a Blynk switch.

#define VPIN2 V8  //Use a unique virtual pin for each device using the same token / dashboard
                  //In order to control a multitude of devices with a single Blynk dashboard, each ESP8266
                  //should be programmed with a unique virtual pin assignment, corresponding to a Blynk switch.

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //Get token from Blynk

//ON and OFF callbacks
void light1On();
void light1Off();
void light2On();
void light2Off();

WemoManager wemoManager;
WemoSwitch *relay1 = NULL;
WemoSwitch *relay2 = NULL;

boolean LEDState1 = 1;
boolean LEDState2 = 1;

SimpleTimer timer;   // allocate a name (timer) to the timer

const int relay1Pin = D1;
const int relay2Pin = D4;

const int button1 = D6;
const int button2 = D7;

const int led1 = D2;
const int led2 = D5;

int relayState1 = LOW;
int relayState2 = LOW;

int buttonState1 = HIGH;
int buttonState2 = HIGH;

void setup() 
{
  Serial.begin(115200);
  //WiFi.disconnect();  //uncomment to clear SSID and PASSWORD from EEPROM
  WiFi.hostname("FOBWilson_07fc8b");  //uncomment to change hostname for device.  Replace XXXX with your own unique name
  WiFiManager wifi;  //WiFiManager local initialization

  //Attempt to get SSID and PASSWORD from EEPROM and connect to network
  //If it does not connect it creates an Access Point with the
  //Specified name
  wifi.autoConnect("WilsonWemo");  //Replace XXXX with your own unique name

  wemoManager.begin();
  // Format: Alexa invocation name, local port no, on callback, off callback
  relay1 = new WemoSwitch("West Deck Light", 80, light1On, light1Off);
  relay2 = new WemoSwitch("Dining Room Outlet", 81, light2On, light2Off);
  wemoManager.addDevice(*relay1);
  wemoManager.addDevice(*relay2);

  pinMode(relay1Pin, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(button2,INPUT_PULLUP);
  pinMode(led2, OUTPUT);
  digitalWrite(relay1Pin, relayState1);
  digitalWrite(led1, !relayState1);
  digitalWrite(relay2Pin, relayState2);
  digitalWrite(led2, !relayState2);

  Blynk.config(auth);
  ArduinoOTA.begin();
  
  timer.setInterval(100L, checkPhysicalButton1); // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton2); // Setup a function to be called every 100 ms
}

void loop()
{
  wemoManager.serverLoop();
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
}

// Toggle relay1 on
void light1On()
{
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(led1, LOW);
  Blynk.virtualWrite(VPIN1, HIGH);  // Sync the Blynk button widget state
}

// Toggle relay1 off
void light1Off()
{
  digitalWrite(relay1Pin, LOW);
  digitalWrite(led1, HIGH);
  Blynk.virtualWrite(VPIN1, LOW);  // Sync the Blynk button widget state
}

BLYNK_WRITE(VPIN1)
{
   relayState1 = param.asInt(); // Get the state of the relay
   digitalWrite(relay1Pin, relayState1);
   digitalWrite(led1, !relayState1);
}

void checkPhysicalButton1()
{ 
  //Relay1
  if (digitalRead(button1) == LOW)
    {
      if (buttonState1 != LOW)
      {
       relayState1 = !relayState1;
       Blynk.virtualWrite(VPIN1, relayState1);
       digitalWrite(relay1Pin, relayState1);
       digitalWrite(led1, !relayState1);
      }
    buttonState1 = LOW;
    }
    else
    {
      buttonState1 = HIGH;
    }
}

// Toggle relay2 on
void light2On()
{
  digitalWrite(relay2Pin, HIGH);
  digitalWrite(led2, LOW);
  Blynk.virtualWrite(VPIN2, HIGH);  // Sync the Blynk button widget state
}

// Toggle relay2 off
void light2Off()
{
  digitalWrite(relay2Pin, LOW);
  digitalWrite(led2, HIGH);
  Blynk.virtualWrite(VPIN2, LOW);  // Sync the Blynk button widget state
}

BLYNK_WRITE(VPIN2)
{
   relayState2 = param.asInt(); // Get the state of the relay
   digitalWrite(relay2Pin, relayState2);
   digitalWrite(led2, !relayState2);
}

void checkPhysicalButton2()
{ 
  //Relay2
  if (digitalRead(button2) == LOW)
  {
    if (buttonState2 != LOW)
    {
      relayState2 = !relayState2;
      Blynk.virtualWrite(VPIN2, relayState2);
      digitalWrite(relay2Pin, relayState2);
      digitalWrite(led2, !relayState2);
    }
  buttonState2 = LOW;
  }
  else
  {
    buttonState2 = HIGH;
  }
}

Sounds like your Relay 1 pin is floating and needs physical pull down resistor (10k ?) to keep it low unless you intentionally pull it high in your code.

It would be good practice to do this with both of the relay pins.

Pete.

If you are using Ardiuno 2-channel relay block from Aliexpress, RelayState should be HIGH, not LOW.

Pete, thanks for the reply. Breaking out the soldering iron now to add some resistors. To be continued…

Sean

** FOLLOW UP **

So, Pete, I put two 10k resistors to ground; one for each signal wire to the relay but when I did that, my wifi no longer worked. When I removed one of the pulldown resistors, the wifi came back but I still am having the same problem.

Basically, when i press the button associated with relay pin 1 all works as it should. When i press the button associated with relay pin 2, relay 1 activates as well. After more testing, it is not random. It happens every time. Sometimes its immediate while other times it may take a couple of seconds. It only happens when I am going from an OFF state to an ON state.

back to testing.

Sean

Zodiac, here’s the relay I am using. http://s.aliexpress.com/n6VbE3yu

The HIGH/LOW logic does not seem inverted. Like I said, relay1 works correctly. When I push the button, the light turns on and when I push it again, the light turns off. Same goes for relay2 except that at random times relay1 turns on when I turn on relay2.

Sean

Ok, now it’s clear, you’re not using relay’s but SSR. There must be some kind of short between two relays on board, channels are separated, so I think you have faulty SSR.

Thanks Zodiac. That was my first thought so I changed the SSR for another one that I new worked but I am still having the same problem. I am rechecking my wiring and soldering now but all looks good.

Sean

It looks like pullingD3 or D4 low at power-up puts the Wemos into a different boot mode.
https://www.letscontrolit.com/forum/viewtopic.php?t=3338
Maybe try a different pin for that relay. However, if the issue isn’t now random then maybe it’s not a floating pin anyway.

What happens if you trigger the relay without it connected to the Wemos?
If triggering relay1 also triggers relay 2 then you have a faulty relay board. If not then it’s either your wiring with the Wemos or your code.

Testing with a meter and using a process of elimination will track down the problem.

Pete.

2 Likes