[SOLVED] ESP8266 for controlling Selenoid door lock

Hello, I'm new in electric and I'm trying to use ESP8266 for controlling selenoid door lock, as picture attached.

I've checked ESP8266 works fine it could get data from firebase and send data to pin.
But I could not make it works with Selenoid Door Lock.
For battery I used 9v alkaline battery and it did work for selenoid door lock without relay when I checked it out. I've also already tried to replace battery with Adaptor 12V 2A.
I've already tried to use a diode parallel to magnetic locks but still does not work.

Could you please tell me where did the wiring go wrong?

Did it not work because I use 3.3v in ESP8266 as VCC in Relay Module?
Should I add Arduino Uno for Mikrocontroller to get 5v VCC?

Here is my circuit diagram :

And here is my source code in Arduino IDE:

#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
                          
#define FIREBASE_HOST "esp8266-fb11b-default-rtdb.firebaseio.com"                     //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "6r9o0ufnXx8AgOtILrnou7sJqrJOtAGEbgH6DEs2" //Your Firebase Database Secret goes here

#define WIFI_SSID "Rachmawatha"                                               //your WiFi SSID for which yout NodeMCU connects
#define WIFI_PASSWORD "081357581200"                                      //Password of your wifi network 
 

// Declare the Firebase Data object in the global scope
FirebaseData firebaseData;

// Declare global variable to store value
int val=0;


void setup() {
  pinMode(D7, OUTPUT);

  Serial.begin(115200);                                                      // Select the same baud rate if you want to see the datas on Serial Monitor

  Serial.println("Serial communication started\n\n");  
           
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                                     //try to connect with wifi
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);


  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  
  Serial.println();
  Serial.print("Connected to ");
  Serial.println(WIFI_SSID);
  Serial.print("IP Address is : ");
  Serial.println(WiFi.localIP());                                            //print local IP address
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);   // connect to firebase

  Firebase.reconnectWiFi(true);
  delay(1000);
  digitalWrite (D7, LOW);
  
}

void loop() {


// Firebase Error Handling And Reading Data From Specified Path ************************************************

if (Firebase.getInt(firebaseData, "/esp8266-fb11b-default-rtdb/data")) {                           // On successful Read operation, function returns 1  

    if (firebaseData.dataType() == "int") {                            // print read data if it is integer

      val = firebaseData.intData();
      Serial.print ("Kunci Pintu: ");
      Serial.println(val);
      digitalWrite(D7, val); 
      Serial.println("\n Change value at firebase console to see changes here."); 
      delay(2000);
      
    }

  } else {
    Serial.println(firebaseData.errorReason());
  }
 }

I would really appreciate it if someone could help me.
Thank you.

As others will be quick to point out, for a test requiring only a minute or so of operation, a "smoke alarm" battery may survive, but you should certainly not use that afterward.

Correct. For a relay module, you need 5 V. And you should use a 5 V relay module. While 3.3 V relay modules are available, the 3.3 V regulator on the NodeMCU will not be able to supply the necessary current for the relay. Please provide a Web link to the relay module you actually have (or a clear photo).

You must be powering this with 5 V. Connect the Vcc pin of the relay module to 5 V. I believe the "Vin" pin of the NodeMCU actually provides this.

Hello Paul,
Thank you for your reply.

I've tried to use Vin pin as you've said.

I could hear the "click" sound in relay it means that relay should be working.
But when I change the value in D7 pin, it still does not change the state of selenoid lock.

When I tried to measure the voltage in pin D7 when the value is "HIGH", the voltage measured only 2.67V. Is that why the relay does not work?
I'm using JQC-3FF-S-Z relay module.
Should I change my relay module?

I've powered up NodeMCU using USB cable provided plugged in to my Laptop.
I measured the voltage in VCC is around 4.35V.

Do you know what I should do?

That is what you get as the ESP8266 operates at 3.3 volts.

The what should I do? The relay does not trigger when I change the value in pin D7.
Should I add Arduino for Mikrokontroller then?

The relay module shows the relay is triggered when the control pin is set low. Is that what you are doing? The "low" is between 0 and about 2 volts. Look at the specs for the exact value. A high value above that low means the relay is off.
But you do need 5 VOLTS to power the module/relay.

Hello Paul, thank you for your kind reply,
I'm giving an update for all the people who were kind enough to help me trying to solve this problem.
I've decided on switching the module to 3.3 V relay module instead and it works out fine now.
I've successfully making the circuit in the picture below from that.

Note that switching the ESP pin from D7 to D1 works out like a magic I don't know why.
Still failed in using 5V Module relay for that though.
I don't know how the people in youtube make that works.
Maybe I am the problem here.