Relay module randomly turns on/off after some time

I have a 3V Relay module that i have connected to my Wemos D1 Mini 3.3V out. I trigger it with a digital pin.

I do not hve a chematic, but is there any change that you can find some problem in my code regarding to this?

Here is my code.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int relay = 16;
int magnet = 4;
int buzzer = 5;
int magnetValue;
int buttonPin = 13;
int ledState = 0;
int ButtonState;


bool Flag_buttonPressed = false; // Sets the button state as false

//Put WiFi cridentials and token here----------------------------------

char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";

//---------------------------------------------------------------------

BlynkTimer timer; // For activating the Blynk Timer

#define DoorIsOpened 1
#define DoorIsClosed 0

#define SignalCloseDoor LOW
#define NoSignalCloseDoor HIGH

void setup() {
  Blynk.begin(auth, ssid, pass); // Something that Blynk needs to connect to wifi
  Serial.begin(9600);
  digitalWrite(relay, NoSignalCloseDoor); // Sets the relay HIGH (that means open circuit)
  pinMode(buttonPin, INPUT);
  pinMode(relay, OUTPUT); // sets the relay as OUTPUT
  pinMode(magnet, INPUT_PULLUP); // Sets the magnet sensor as an INPUT_PULLUP
  pinMode(buzzer, OUTPUT); // Sets the buzzer as an output
  timer.setInterval(1000L, magnetSensor); // Runs the "magnetSensor" function every second
  timer.setInterval(100L, switchPin);
}

void switchPin(){
  ButtonState = digitalRead(buttonPin);
  if(ButtonState == 1){
      digitalWrite(relay, LOW);
      delay(1000);
      digitalWrite(relay, HIGH);
      Serial.println("Button pushed!");
    }
    else{
      Serial.println("Button released!");
    }
}

void magnetSensor() { // Reads the state of the door (closed or open)
  magnetValue = digitalRead(magnet);

  if (magnetValue == DoorIsOpened) { // If the door is open...
    Blynk.virtualWrite(V2, "GARASJEPORT ÅPEN"); // ...write "garage door is open" (translated to english) to the Blynk app
  }
  else { // If the door is closed...
    Blynk.virtualWrite(V2, "GARASJEPORT STENGT"); // ...write "Garage door is closed" (translated to english) to the Blynk app
    Flag_buttonPressed = false; // reset the flag because door is closed
  }
}

void buzzerTone() { // Makes a warning sound before garage door closes after button in Blynk app is pushed
  if (!Flag_buttonPressed && magnetValue == DoorIsOpened ) {
    Flag_buttonPressed = true;

    for (int i = 0; i < 5; i++) {
      tone(buzzer, 600);
      delay(500);
      noTone(buzzer);
      delay(500);
    }
  }
}

BLYNK_WRITE(V1) { //Blynk app button
  int pinData = param.asInt(); // Some code for sync the Blynk app button
  magnetValue = digitalRead(magnet);

  if (pinData == 1 && magnetValue == DoorIsOpened) {
    buzzerTone(); // Activate the warning sound
    digitalWrite(relay, SignalCloseDoor); //... Close the garage door
    timer.setTimeout(1000L, RELAYoff); // Makes the signal to the door last for half second to ensure that it closes
  }
  if (pinData == 1 && magnetValue == DoorIsClosed) {
    digitalWrite(relay, SignalCloseDoor);
    timer.setTimeout(1000L, RELAYoff); // Gives the garage opener half second of power to ensure that it closes
  }
}

void RELAYoff() { // turns the signal to the garage opener off
  digitalWrite(relay, NoSignalCloseDoor);
}

void loop() {
  Blynk.run();
  timer.run();
}

What does the debug output say?

If everywhere you change the state of your relay you put some debug you will have your answer.

Missing pull down perhaps?

  pinMode(buttonPin, INPUT);

It is an latching button, so i do not think i need PULLUP. I have tried that, and it do not helps.

I resoldered my button pins now and for now, all works fine. I will let it stay for a while and report back :slight_smile:

Have you tried writing the code to run on WiFI server rather than the BLYNK app?

That would tell you if it's in the the BLYNK portion or the "local" portion of the code.

I've never really used BLYNK because the first programming language I learned was HTML, so I like using my WeMos as a "local server" to do tasks like this

Bjerknez:
It is an latching button, so i do not think i need PULLUP. I have tried that, and it do not helps.

Latching just means when you press it it closes, and stays closed when you release.
You have to press a second time to open the switch, and then it stays open.

It says nothing about if the switch has an internal pull-up or pull-down.

When the switch is latched in the OPEN position you need an appropriate pull-up/pull-down resistor to prevent your input pin floating. If you don’t have that you will get random operation. Sometimes it may even appear to work, at least until the input pin picks up some random noise.

It's looks like my solder joint was bad. After i resoldered the pins to my button, all works fine. I think. :slight_smile: