I am trying to make my Solenoid lock work as I expect. The code is simple - it should unlocks and locks again. The issue I face is that when I start my 12V power source the lock just unlocks and stays like this all the time and looks like the relay can't control it.
Here is my code:
#define RELAY_PIN 14
void setup() {
// initialize digital pin as an output.
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
digitalWrite(RELAY_PIN, HIGH); // unlock the door
delay(5000);
digitalWrite(RELAY_PIN, LOW); // lock the door
delay(5000);
}
Disconnect the solenoid and see if the relay activates. Check NO-COM with an ohmmeter, you should see it make & break. If no, check input wiring, else recheck 12v power wiring. Does the solenoid kick when tested on 12V power directly?
Do you have a snubber/kickback diode across the coil of the solenoid? It's REQUIRED.
How is Arduino powered?
Sounds like the issue is just what @ paulpaulson said. ESP32 has a 3.3v logic level and every relay I have seen has a 5v logic level, if that is the case you need to get a logic level shifter before doing anything with your script.
Hey guys, thank you all for your quick response. I already tried the lock and the relay separately - they work fine. The code also looks good. I am actually using ESP32Cam for the project. Let me upload some photos of the wiring:
The relayPin is on GPIO2. I am using a 5V power source from my laptop and 12V from a battery holder. The lock heats quickly also . It could be realted to the logic level like @paulpaulson said.
Hi @awneil, that hacked cable surely works because I recently powered some servos with it. The relay is blinking green and red which means it works I think. Here is a link to the relay module: Модул 1 реле 5v - it's a Bulgarian site.
I would guess this means that the red LED should stay on with the power, and the green one should go on/off according to whether the relay is activated?
Again, you should be able to hear the relay clicking...
I tried a different relay module and it worked! I am very happy to see that I can continue my project now! Thank you @awneil and all of you who answered on this topic.