Powering an electromagnetic lock/strikeplate

Hi all--experienced programmer but noob with arduino and electronics, and I'm hoping someone (anyone!) can help.

I'm trying to use an arduino to control an electromagnetic lock. The setup is in the attached image.

I'm sure I'm being silly but I can't figure this out:

When I connect the lock to either the 5V or 3.3V and a ground, it unlocks as it should.

When I connect it to a pin--nada. Am I missing something? I've tried both analogWrite and digitalWrite (see code below).

Help! And thanks!

#include <Servo.h>

Servo servo1;

const int LED = 13;
const int BUTTON = 7;
//const int LOCK = 0;
int val = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(LED, OUTPUT); 
  pinMode(BUTTON, INPUT);
}

void loop(){

  val = digitalRead(BUTTON); 
  
  if (val == 1) {
    Serial.print(val);
    digitalWrite(LED, HIGH); 
//    neither is working!~
//    analogWrite(9, 255); 
//    digitalWrite(9, HIGH); 

    
  } 
  else {
    Serial.print(val);
    digitalWrite(LED, LOW);
//    analogWrite(9, 0); 
//    digitalWrite(9, LOW);     
    
  }

  
}

arduino_shot.jpg

It is likely the Arduino is incapable of providing enough current to operate the lock, and you may have already damaged your Arduino.
Use a relay or a transistor.

You need something like this:

http://www.ebay.com/itm/5V-2-Channel-Relay-Module-for-Arduino-PIC-ARM-DSP-AVR-Electronic-/140764956257

Arduino -> Transistor -> Relay

Or you could use a power MOSFET or something, but I don't see any reason not to use a relay for this, they are simple. This thing appears to have diode protection and is exactly what you want. One side of the relay is normally open, the other side is normally closed, you apply a high signal to the input and it switches. You get two channels.

Or something like the first schematic on this page:-
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html