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);
}
}