Relay Chattering When GPIO pin is held by my hand or metal

#define BLYNK_TEMPLATE_ID "TMPLHYty_y66"
#define BLYNK_DEVICE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "TR4GHJjb6fXPkgPSeeLU2c1q7s8nlorR"

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Espalexa.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "Wifi.h"
#include "ota.h"


char auth[] = BLYNK_AUTH_TOKEN;
//char ssid[] = "Access With Permission_2.4GhZ";
//char pass[] = "Tank9555";
//OUTPUT
#define OUT1           16   //D0
#define OUT2            5   //D1
#define OUT3            4   //D2 
#define OUT4            0   //D3
//INPUT
#define SWITCH1           2   //D4
#define SWITCH2          14   //D5
#define SWITCH3          12   //D6
#define SWITCH4          13   //D7
         

bool R1FLAG = 0;
bool R2FLAG = 0;
bool R3FLAG = 0;
bool R4FLAG = 0;
bool R5FLAG = 0; 


bool S1FLAG = 1;
bool S2FLAG = 1;
bool S3FLAG = 1;
bool S4FLAG = 1;

String Device_1_Name = "Light";
String Device_2_Name = "Fan";
String Device_3_Name = "Night Lamp";
String Device_4_Name = "Socket";

void LightChanged(uint8_t brightness);
void FanChanged(uint8_t brightness);
void NightLampChanged(uint8_t brightness);
void SocketChanged(uint8_t brightness);

Espalexa espalexa;
BlynkTimer timer;
WiFiUDP ntpUDP; 

String dayofWeek[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

NTPClient timeClient(ntpUDP, "pool.ntp.org",19800);

BLYNK_WRITE(V0) { 
  R1FLAG = param.asInt();  
  digitalWrite(OUT1,!R1FLAG);   
  EspalexaDevice* d1 = espalexa.getDevice(0);
  if (R1FLAG == 1) {
    d1->setPercent(100); 
  }
  else
  {
    d1->setPercent(0);
  }
   
}

BLYNK_WRITE(V1) { 
  R2FLAG = param.asInt();  
  digitalWrite(OUT2,!R2FLAG); 
  EspalexaDevice* d2 = espalexa.getDevice(1);
  if (R2FLAG == 1) {
    d2->setPercent(100); 
  }
  else
  {
    d2->setPercent(0);
  } 
}

BLYNK_WRITE(V2) { 
  R3FLAG = param.asInt();  
  digitalWrite(OUT3,!R3FLAG);
  EspalexaDevice* d3 = espalexa.getDevice(2);
  if (R3FLAG == 1) {
    d3->setPercent(100); 
  }
  else
  {
    d3->setPercent(0);
  }  
}

BLYNK_WRITE(V3) { 
  R4FLAG = param.asInt();  
  digitalWrite(OUT4,!R4FLAG);
  EspalexaDevice* d4 = espalexa.getDevice(3);
  if (R4FLAG == 1) {
    d4->setPercent(100); 
  }
  else
  {
    d4->setPercent(0);
  }  
}

BLYNK_WRITE(V4) {
  //R5FLAG = param.asInt(); 
  digitalWrite(OUT1,HIGH);
  digitalWrite(OUT2,HIGH); 
  digitalWrite(OUT3,HIGH);
  digitalWrite(OUT4,HIGH);

  Blynk.virtualWrite(V0, LOW);
  Blynk.virtualWrite(V1, LOW);
  Blynk.virtualWrite(V2, LOW);
  Blynk.virtualWrite(V3, LOW);


}

BLYNK_WRITE(V5) {
  //R5FLAG = param.asInt(); 
  digitalWrite(OUT1,LOW);
  digitalWrite(OUT2,LOW); 
  digitalWrite(OUT3,LOW);
  digitalWrite(OUT4,LOW);

  Blynk.virtualWrite(V0, HIGH);
  Blynk.virtualWrite(V1, HIGH);
  Blynk.virtualWrite(V2, HIGH);
  Blynk.virtualWrite(V3, HIGH);


}
void checkPhysicalButton()
{
  
  //FOR SWITCH 1
  if (digitalRead(SWITCH1) == LOW && S1FLAG == 0) {
    digitalWrite(OUT1, LOW);    
    Blynk.virtualWrite(V0, HIGH); 
    EspalexaDevice* d1 = espalexa.getDevice(0);
    d1->setPercent(100);    
    R1FLAG = 1;
    S1FLAG = 1;  
   }
   if (digitalRead(SWITCH1) == HIGH && S1FLAG == 1) {
    digitalWrite(OUT1, HIGH);
    Blynk.virtualWrite(V0, LOW);
    EspalexaDevice* d1 = espalexa.getDevice(0);
    d1->setPercent(0);     
    R1FLAG = 0;
    S1FLAG = 0;   
   }
//FOR SWITCH 2
  
  if (digitalRead(SWITCH2) == LOW && S2FLAG == 0) {
    digitalWrite(OUT2, LOW);
    Blynk.virtualWrite(V1, HIGH);
    EspalexaDevice* d2 = espalexa.getDevice(1);
    d2->setPercent(100); 
    R2FLAG = 1;
    S2FLAG = 1;   
 
  }
  if (digitalRead(SWITCH2) == HIGH && S2FLAG == 1) {
    digitalWrite(OUT2, HIGH);
    Blynk.virtualWrite(V1, LOW); 
    EspalexaDevice* d2 = espalexa.getDevice(1);
    d2->setPercent(0);    
    R2FLAG = 0;
    S2FLAG = 0;   
 
  }
//FOR SWITCH 3
  
  if (digitalRead(SWITCH3) == LOW && S3FLAG == 0) {
    digitalWrite(OUT3, LOW);
    Blynk.virtualWrite(V2, HIGH);
    EspalexaDevice* d3 = espalexa.getDevice(2);
    d3->setPercent(100); 
    R3FLAG = 1;
    S3FLAG = 1;   
 
  }
  if (digitalRead(SWITCH3) == HIGH && S3FLAG == 1) {
    digitalWrite(OUT3, HIGH);
    Blynk.virtualWrite(V2, LOW); 
    EspalexaDevice* d3 = espalexa.getDevice(2);
    d3->setPercent(0);    
    R3FLAG = 0;
    S3FLAG = 0;   
 
  }
//FOR SWITCH 4
  
  if (digitalRead(SWITCH4) == LOW && S4FLAG == 0) {
    digitalWrite(OUT4, LOW);
    Blynk.virtualWrite(V3, HIGH);
    EspalexaDevice* d4 = espalexa.getDevice(3);
    d4->setPercent(100); 
    R4FLAG = 1;
    S4FLAG = 1;   
 
  }
  if (digitalRead(SWITCH4) == HIGH && S4FLAG == 1) {
    digitalWrite(OUT4, HIGH);
    Blynk.virtualWrite(V3, LOW);  
    EspalexaDevice* d4 = espalexa.getDevice(3);
    d4->setPercent(0);    
    R4FLAG = 0;
    S4FLAG = 0;   
 
  }
}


void LightChanged(uint8_t brightness)
{

  if (brightness == 255)   {
    digitalWrite(OUT1,LOW); 
    Blynk.virtualWrite(V0,HIGH); 
  }
  else 
  {
    digitalWrite(OUT1,HIGH); 
    Blynk.virtualWrite(V0,LOW); 
  }
}

void FanChanged(uint8_t brightness)
{

  if (brightness == 255)   {
    digitalWrite(OUT2,LOW); 
    Blynk.virtualWrite(V1,HIGH); 
  }
  else 
  {
    digitalWrite(OUT2,HIGH); 
    Blynk.virtualWrite(V1,LOW); 
  }
}

void NightLampChanged(uint8_t brightness)
{

  if (brightness == 255)   {
    digitalWrite(OUT3,LOW); 
    Blynk.virtualWrite(V2,HIGH); 
  }
  else 
  {
    digitalWrite(OUT3,HIGH); 
    Blynk.virtualWrite(V2,LOW); 
  }
}

void SocketChanged(uint8_t brightness)
{

  if (brightness == 255)   {
    digitalWrite(OUT4,LOW); 
    Blynk.virtualWrite(V3,HIGH); 
  }
  else 
  {
    digitalWrite(OUT4,HIGH); 
    Blynk.virtualWrite(V3,LOW); 
  }
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
  
  if (WiFi.status() == WL_CONNECTED) {    
    int b1 = Blynk.connected(); 
    if (b1 == 0) {
      Serial.println("Blynk not connected"); 
      Blynk.virtualWrite(V8,"Internet Not Connected");
      Blynk.config(auth);
      Blynk.connect();       
  }
    else {
     Blynk.virtualWrite(V8,"Internet Connected");
  }
}
else {
  
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
  Serial.println("Reconnecting");
}
}

void timeupd() {

 if (WiFi.status() == WL_CONNECTED){  
  timeClient.update();
  Blynk.virtualWrite(V6,timeClient.getFormattedDate());
  int day = timeClient.getDay();  
  Blynk.virtualWrite(V7,dayofWeek[day]); 
  }
  
}

void setup () { 
  Serial.println("insetup");
  Serial.begin(115200);
  readwifi();
  //Blynk.begin(auth,ssid,pass);  
  
  timer.setInterval(3000L, checkBlynkStatus);
  ota();

    
  pinMode(OUT1, OUTPUT);
  pinMode(OUT2, OUTPUT);
  pinMode(OUT3, OUTPUT);
  pinMode(OUT4, OUTPUT);
  pinMode(LED_BUILTIN,OUTPUT);

  
  pinMode(SWITCH1, INPUT_PULLUP);
  pinMode(SWITCH2, INPUT_PULLUP);
  pinMode(SWITCH3, INPUT_PULLUP);
  pinMode(SWITCH4, INPUT_PULLUP);
  

  
  digitalWrite(OUT1, !R1FLAG);
  digitalWrite(OUT2, !R2FLAG);
  digitalWrite(OUT3, !R3FLAG);
  digitalWrite(OUT4, !R4FLAG);
  Serial.println("Setting up");
  
  Serial.println("inespalexa");
  espalexa.addDevice(Device_1_Name, LightChanged); //simplest definition, default state off
  espalexa.addDevice(Device_2_Name, FanChanged); 
  espalexa.addDevice(Device_3_Name, NightLampChanged); 
  espalexa.addDevice(Device_4_Name, SocketChanged); 
  espalexa.begin();

  timeClient.begin();  
  timer.setInterval(1000L, timeupd);      
  
   
}

void loop() {
  ArduinoOTA.handle();  
  espalexa.loop();  
  Blynk.run();    
  checkPhysicalButton();
  timer.run(); // Initiates SimpleTimer
  
    
}

When i upload this code to my ESP8266 and run it using usb power supply the code works fine. But when i power it through my 5v Adapter (SMPS power supply) and held the physical input pins by my hands the relay chatters . Even if i touch the insulation wires the relay chatters.

@rajsimmy Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Also, I’d take the opportunity to elaborate on your problem in the body of the post, rather than having a very short summary of the problem in the title of the topic.

Pete.

1 Like

Edited.

Some clues about which GPIO pin the relay is attached to, what type of relay you are using, how the relay is powered, what sort of load you have connected to the relay etc etc etc would be a step in the right direction.

Pete.

Hello Pete,

The relays connected are the 5v dc active low drive relay. It is connected to pins D0,D1,D2,D3 . The input physical switches are connected to pins d4,d5,d6,d7. The relays are powered by the same 5v supply which is used to power the Node MCU.

When i power it through the computer usb the circuit works fine without any chattering. but when i connect it to the external 5v power supply and i hold any one of the input pins by my hand or i touch the wires to any metal surface on the ground the relays starts chattering.

I have tested it by disconnecting the relay module also. When i disconnect the relay module i can see whenever i held the input gpio pins by my hand the output pins go low and high which i can clearly see in the blynk app. Seems the issue in the power supply but how can i stop it ?

Which pins are these? Do you mean D4-7 inclusive?

Pete.

Yes .you are right

I’d guess that a schematic and some photos of your setup might help.

Pete.