Turn on a led when power to mag lock has been cut ie door open

Im new to the coding aspect here.
I think this should be simple but I'm doing something wrong !

I have a board connected to a small led with cables to 5v , grnd and s8 (digital pin 8)
To turn on my led I have code; so effectively the light is on once I plug in power.

#define LaserPin 8

void setup ()
{
pinMode (LaserPin, OUTPUT);
}
void loop () {
digitalWrite (LaserPin, HIGH);
delay (10000);
digitalWrite (LaserPin, LOW);
delay (0000);
}

Id like to detect a mag lock opening ie no power and then turn this light on.

Im not sure how to put the eif statement in and what to wire. I have a relay that I connected to the mag lock but not sure how to detect the low of power from the relay or what wires to connect to relay.

Im hoping that all make sense in some way and someone can help?

not sure why you want to use a microcontroller for this but assuming your mag-lock is wired in a similar fashion as below, IMHO, a simple transistor circuit would do!

image

hope that helps...

1 Like

you probably didn't intend this to be zero

you might consider doing the following just to get you bearings by controlling the LED using an external input

#define InputPin 9
#define LaserPin 8

void setup ()
{
    pinMode (LaserPin, OUTPUT);
    pinMode (InputPin, INPUT_PULLUP);
}

void loop ()
{
    digitalWrite (LaserPin, digitalRead (InputPin));
}

The circuit shown by haffyj is possibly AC so using leds can be a bit tricky. If the NO contact is available use it with a diode, led, and resistor in series be sure the cathode of one connects to the anode of the other. This is to protect the LED from to high of a reverse voltage. If that contact is not available then connect the LED, diode and resistor across the Mag Lock (NC) and ground It should not draw enough to keep the lock on. If it is DC the diode is not needed. Either case you will have to determine the resistance since you did not state the voltage.


Here is a diagram of what I'm trying to do...

The relay has two contacts, 1 normally open, the other normally closed, use one for the lock the other for the LED.

in case the relay you are using is single pole, its a relay of at least this type that is being suggested:
DPDT relays

hope that helps...

Then you can use the unused set of contacts if there is one or match the voltage with what the relay is switching and connect the LED to the other contact. You can use a multimeter to check this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.