Easy way to know when an electromagnet latches

Ideas and instructions how you can make your own bondage toys.
Post Reply
swswl
*
Posts: 13
Joined: 11 Feb 2020, 00:48

Easy way to know when an electromagnet latches

Post by swswl »

Just discovered something useful I wanted to share.

I use an electromagnet controlled by an Arduino to "tie the last hand". However, it's a bit clumsy to just add a delay between the time the Arduino is powered on and the time everything is done and I can drop the plate on the magnet before starting, ahem, other things. So I figured out a simple way to detect the latching of the plate on the magnet, at which point the countdown timer really starts.

Turns out there is a sharp drop in the current consumed by the electromagnet when the plate latches. See image below.
Capture.PNG
Capture.PNG (4.66 KiB) Viewed 2750 times
So I used a resistor as a shunt on the low side (since the high side is 12v and Arduinos can only read up to 5V) to measure the associated voltage drop. All I had were 1/4 watts and my smallest one was 2.2ohms. The magnet uses about 350mA, so that would have meant just above 1/4 watts. Not wanting to risk anything catching on fire, I went with two 2.2ohms in parallel, effectively making the equivalent resistance 1.1ohms. This halved the dissipated power with the added benefit that the dissipation would occur in 2 resistors instead of one. One can expect a normal voltage of ~0.4v accross the shunt. That kind of voltage drop is not a problem for the magnet, but a little low for a good resolution on the Arduino side. Since I had no other analog input in use, I just modified the analog voltage reference in software (INTERNAL makes it 2.56v on an Uno). Good enough.

Schematics:
magnetLock_schem.png
Code
The code is pretty simple. It just starts the magnet as soon as the Arduino powers on, and starts a countdown timer when the latch is detected. To be on the safe side, there is also a timer that powers down the magnet if no latch has been detected in a certain time, in case the detection didn't work (otherwise the countdown would never start and thus never release you!)

Here is the code:

Code: Select all

#define magnetPin 4
#define sensorPin A0
#define duration 60000 // 1 minute
#define latchWait 60000 // 1 minute.

unsigned long start;

void setup() {
  Serial.begin(9600);
  digitalWrite(magnetPin, HIGH);
  
  printTime();
  Serial.println("Magnet powered. Stabilizing reading.");
  
  analogReference(INTERNAL);
  delay(5000);  // arbitrary
  
  printTime();
  Serial.println("Sensing...");
  
  waitForLock();
  
  printTime();
  Serial.println("Magnet plate latched. Beginning session.");
  
  start = millis();  
}

void loop() {
  // do something devious here

  if (millis() - start > duration) {
    // we're done here
    digitalWrite(magnetPin, LOW);
    
    printTime();
    Serial.println("We're done here. Magnet powered off.");
    
    while (true) {};  // grinds everything to a halt
  }
}


void waitForLock() {
  int initCurrent = analogRead(A0);
  int current;
  unsigned int start = millis();
  do {
    current = analogRead(A0);
    if (millis() - start > latchWait) {
        printTime();
        Serial.println("That took too long. Powering off magnet...");
      digitalWrite(magnetPin, LOW);
      while (true) {};
    }
  } while (current > 0.75 * initCurrent);
}

void printTime() {
  Serial.print(millis() / 1000);
  Serial.print("s: ");
}
You can modify the sessionDuration and latchWait times depending on your needs. Here's what it looks like from the serial monitor:

Successful latch detection:
working.PNG
working.PNG (6.41 KiB) Viewed 2750 times
Unsuccessful latch detection:
tooLong.PNG
tooLong.PNG (4.83 KiB) Viewed 2750 times
Safety
- Resistors fail open, so fried resistors = free
- Code detects if no latch occured, setting you free
- I use 4xLiFePO4 18650 batteries to power everything. Safer than normal lithium, 4 of them also closely match a normal 12V source. This is backup #1 (they should last a bit more than an hour at 1500mAh each)
- Key in ice. This is backup #2

I have learned a lot by lurking on this site before posting. I hope this helps someone!

Cheers
User avatar
BoundInKasugai
***
Posts: 352
Joined: 19 Feb 2011, 06:28

Re: Easy way to know when an electromagnet latches

Post by BoundInKasugai »

Great find, many thanks for sharing!
rmcingle
***
Posts: 224
Joined: 13 Jul 2010, 17:28

Re: Easy way to know when an electromagnet latches

Post by rmcingle »

It is good that you included a failsafe, a maximum time limit.

The drop in current is because "back EMF" is generated when the plate moves freely in the magnetic field. Just like with a motor: with no load, the motor current is small and the motor will turn at the rate where the back EMF cancels out the drive voltage. The freely moving magnet/metal allows for significant back EMF. Load the motor, make it turn slower, and the back EMF is negligible and the current is greater.

So, what I am saying, is that if you held the plate and lowered it slowly you would not create the sharp spike in the current.

Ron
swswl
*
Posts: 13
Joined: 11 Feb 2020, 00:48

Re: Easy way to know when an electromagnet latches

Post by swswl »

rmcingle wrote:The drop in current is because "back EMF" is generated when the plate moves freely in the magnetic field. Just like with a motor: with no load, the motor current is small and the motor will turn at the rate where the back EMF cancels out the drive voltage. The freely moving magnet/metal allows for significant back EMF. Load the motor, make it turn slower, and the back EMF is negligible and the current is greater.
Thanks for the explanation!
rmcingle wrote: So, what I am saying, is that if you held the plate and lowered it slowly you would not create the sharp spike in the current.
Yeah, in several tests it happened only once where I could lower the plate slowly enough to not catch the event, which got me to add the extra safety. Less likely to happen also is the case where the magnet is powered on while the plate is already on it. That would also never result in a detection.
User avatar
Gregovic
****
Posts: 1119
Joined: 26 Mar 2016, 21:31
Location: Netherlands

Re: Easy way to know when an electromagnet latches

Post by Gregovic »

A possibly more accurate way to detect the magnet closing is measuring the inductance of the magnet coil. Fellow forum user Onwrikbaar has made one or two posts about this (See this one with the most information I think)
How may I serve you? *Curtsey*
swswl
*
Posts: 13
Joined: 11 Feb 2020, 00:48

Re: Easy way to know when an electromagnet latches

Post by swswl »

Gregovic wrote:A possibly more accurate way to detect the magnet closing is measuring the inductance of the magnet coil. Fellow forum user Onwrikbaar has made one or two posts about this (See this one with the most information I think)
It’s coming back to me now that I had seen that before. The guy’s a legend.

A little bit too complicated for me to understand (I’m not an electronics guy), but I understand his solution actually determines the state of the magnet, whereas mine only detects a change in the state? If that’s the case, his is definitely better.
User avatar
Gregovic
****
Posts: 1119
Joined: 26 Mar 2016, 21:31
Location: Netherlands

Re: Easy way to know when an electromagnet latches

Post by Gregovic »

swswl wrote:
Gregovic wrote:A possibly more accurate way to detect the magnet closing is measuring the inductance of the magnet coil. Fellow forum user Onwrikbaar has made one or two posts about this (See this one with the most information I think)
It’s coming back to me now that I had seen that before. The guy’s a legend.

A little bit too complicated for me to understand (I’m not an electronics guy), but I understand his solution actually determines the state of the magnet, whereas mine only detects a change in the state? If that’s the case, his is definitely better.
Basically yes. Broadly speaking the inductance of the coil means how much it resists a change in voltage. (Note I'm simplifying things a lot there, but as they say, it's good enough for the girls I go out with)
If the coil is "Open" (there is no plate there) the magnetic field doesn't become very strong and this field in turn doesn't resist the change in voltage. In other words, when you apply a 12 volt battery the actual voltage across the coil matches the battery voltage within a short time.
When the strike plate is there, the magnetic field can take a shortcut through the metal of the plate and the coil is "closed. Because of this the magnetic field in the coil becomes much faster and prevents the voltage from rising very fast. In other words, when you apply the same 12 volt battery the voltage across the coil takes longer to match the voltage of the battery.

So what Onwrikbaar is doing is actually ingeniously simple:
Switch on the coil, and start a timer. Then measure the voltage on the coil and stop the clock when the voltage on the coil matches a threshold (a bit below battery voltage to account for losses in the wires). If the time elapsed is less than 2 milliseconds, the lock plate is not in place. If the time is more than 2 milliseconds and less then 20, the lock is closed. If the time is more than 20ms, something is wrong (the coil is not connected or the battery is dead or something). The wait times (2ms and 20ms) are dependent on the magnet and you might have to experiment a little, but overall you don't even need to understand exactly WHY it works, just that it works in general. Standing on the shoulders of giants and all that.


If you're a mechanical thinker (a bit like me) imagine this "mechanical equivalent". The first thing is the physical equivalents of voltage and current. If we look at a hydraulic system of let's say flowing water, then voltage is equivalent to the pressure of the water (measured in PSI, or Bar or kPA or mmH20) and current is the equivalent of the flow of the water (measured in volume per time ie, L/min or gallons/hour). Now let's suppose you have a pliable bag made of a non stretchable material. So now we want to fill the bag until the pressure (voltage) inside the bag matches the pressure (voltage) supplied by the pump (battery). If it's just the bag, there is basically no resistance so we can very quickly fill the bag at full flow speed and very low pressure until it is full, and then the pressure nearly instantaneously becomes the full pressure the pump is supplying.

Now suppose we put a large weight on top of the bag (this is the equivalent of our strike plate in this example)? First the areas of the bag outside the weight fill with water at the same speed as before. But then all the unobstructed bits are full and the bag has to start lifting the weight before it can fill further. To do this, the pressure inside the bag has to increase before water can flow in again. For a short moment, the flow of the water stops as the pump increases the pressure further and further, until the weight starts to lift and the flow goes back to what it was (since at this new, higher pressure, the flow is no longer restricted by the weight) until the bag is once again full and pressure rises to the full pump pressure . This short stop of flow is the drop in current that you see in your own tests. As you can imagine, it takes time for the pump to build up the pressure to the pressure required to lift the weight before the bag can be fully filled. This delay is the delay that Onwrikbaar is using in his timing. It takes time before the battery can raise the voltage high enough to overcome the increased capacitance of the coil+strikeplate compared to just the coil


Perhaps that is all clear as mud, in which case feel free to forget it all in a heartbeat. I enjoyed typing it out as a mental exercise anyway (call it an occupational mental deficiency if you will :mrgreen: )
How may I serve you? *Curtsey*
swswl
*
Posts: 13
Joined: 11 Feb 2020, 00:48

Re: Easy way to know when an electromagnet latches

Post by swswl »

Gregovic wrote:
Basically yes. Broadly speaking the inductance of the coil means how much it resists a change in voltage. (Note I'm simplifying things a lot there, but as they say, it's good enough for the girls I go out with)
Haha, a fan of Ave I see!

Inductance resists a change in current, not voltage I think. That still perfectly matches your explanation though. I.e. because the current is allowed through more quickly when the plate is on, the voltage doesn’t have as much time to spike when the power is turned on. Am I getting this right?

Cheers
User avatar
Gregovic
****
Posts: 1119
Joined: 26 Mar 2016, 21:31
Location: Netherlands

Re: Easy way to know when an electromagnet latches

Post by Gregovic »

swswl wrote:
Gregovic wrote:
Basically yes. Broadly speaking the inductance of the coil means how much it resists a change in voltage. (Note I'm simplifying things a lot there, but as they say, it's good enough for the girls I go out with)
Haha, a fan of Ave I see!

Inductance resists a change in current, not voltage I think. That still perfectly matches your explanation though. I.e. because the current is allowed through more quickly when the plate is on, the voltage doesn’t have as much time to spike when the power is turned on. Am I getting this right?

Cheers
You are absolutely correct :oops:
How may I serve you? *Curtsey*
rmcingle
***
Posts: 224
Joined: 13 Jul 2010, 17:28

Re: Easy way to know when an electromagnet latches

Post by rmcingle »

Gregovic wrote:A possibly more accurate way to detect the magnet closing is measuring the inductance of the magnet coil.

There is still a failure mechanism if the plate if not properly aligned.

If there was a mechanical guide that assured that the plate always met the magnet with the proper alignment then it could work well enough. But if there was no guarantee of alignment you would need to make sure that you could detect even a small portion of the plate in place.

Or just rely on the failsafe timeout.

R.Mc.
User avatar
Audrey_CD
***
Posts: 224
Joined: 04 Mar 2009, 22:46
Location: United Kingdom

Re: Easy way to know when an electromagnet latches

Post by Audrey_CD »

I take a simpler approach. I have two contacts that the strike plate shorts when it is in the right position. When the short is detected, power is applied to the magnet. A second check on the contacts is made to ensure the strike plate didn't move.
You can even put the magnet's power through the contacts to be a self latching mechanism.
Post Reply