Projects / Magnet-key lock demo

Magnet-key lock demo

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire the sensor

    5V, GND, output to D2 with a 10 kilohm pull-up.

  2. NANO59°

    Wire the servo

    Signal D9, power 5V and GND.

  3. NANO15.6 mA

    Wire the LEDs

    Red D4, green D5, with 220 ohm each.

  4. NANO59°15.7 mA

    Upload the sketch

    Red LED on, servo at the locked angle.

  5. NANO

    Try the magnet

    Move it to the marked face of the sensor.

  6. NANO

    Flip the magnet

    Only one pole works.

  7. NANO

    Check the relock

    It locks after ten seconds.

  8. NANO

    Mount in a box

    Fix the sensor behind the lid where the magnet goes.

In short

How do you build a magnet-key lock demo?

A Hall sensor only unlocks a model box latch when a magnet is held on the right spot with the right pole facing it. A learning demo, not a real lock. You need Arduino Nano, Hall Effect Sensor A3144, SG90 Micro Servo, Resistor 10 kΩ, LED 5 mm red, LED 5 mm green, Resistor 220 Ω × 2, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
9

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
A3144 pin 1 (VCC)Nano 5VSensor power, flat face toward you, legs down, pin 1 on the left
A3144 pin 2 (GND)Nano GNDCommon ground
A3144 pin 3 (OUT)Nano D2Open-collector output, low near the right pole
A3144 pin 3 (OUT)10 kilohm resistor to Nano 5VPull-up so the output reads high without a magnet
Servo signal (orange)Nano D9Latch movement
Servo power (red) and ground (brown)Nano 5V and GNDSmall unloaded servo only
Red LED anode220 ohm to Nano D4Locked indicator, cathode to GND
Green LED anode220 ohm to Nano D5Unlocked indicator, cathode to GND

Code

A Hall sensor changes its output when a magnetic field of the right polarity passes near it, so it can detect a magnet without any contact. The A3144 is a unipolar switch: its output goes low only when the south pole of a magnet faces the marked face of the chip, and the other pole does nothing. That makes the magnet the key: the right magnet, the right way round, in the right place. The output is open-collector, so a 10 kilohm resistor pulls it high when no magnet is present. The sketch confirms the magnet is present for a short time to avoid false triggers, then moves the servo latch to the unlocked position, turns the red LED off and the green LED on. After ten seconds it relocks automatically. A small servo can be run from the 5 volt pin while unloaded, but a stalled servo can pull enough current to reset the board, so keep the latch light. This is a teaching model: anyone with a spare magnet can open it, so never use a design like this to protect anything valuable, and keep strong magnets away from cards, phones and pacemakers.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Servo.h>

const int HALL = 2, SERVO_PIN = 9, LED_RED = 4, LED_GRN = 5;
const int LOCKED_ANGLE = 0, UNLOCKED_ANGLE = 90;
const unsigned long RELOCK_MS = 10000UL;   // relock after ten seconds
const unsigned long CONFIRM_MS = 60;       // magnet must be steady this long

Servo latch;
bool unlocked = false;
unsigned long unlockedAt = 0, seenSince = 0;
bool magnetWas = false;

void setLocked(bool lockIt) {
  unlocked = !lockIt;
  latch.write(lockIt ? LOCKED_ANGLE : UNLOCKED_ANGLE);
  digitalWrite(LED_RED, lockIt);
  digitalWrite(LED_GRN, !lockIt);
  Serial.println(lockIt ? "Locked" : "Unlocked");
}

void setup() {
  pinMode(HALL, INPUT);                     // external 10 kilohm pull-up
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GRN, OUTPUT);
  latch.attach(SERVO_PIN);
  Serial.begin(9600);
  setLocked(true);
}

void loop() {
  bool magnet = (digitalRead(HALL) == LOW); // A3144 pulls low near its marked pole
  if (magnet && !magnetWas) seenSince = millis();
  magnetWas = magnet;

  if (!unlocked && magnet && millis() - seenSince >= CONFIRM_MS) {
    setLocked(false);
    unlockedAt = millis();
  }
  if (unlocked && millis() - unlockedAt >= RELOCK_MS) {
    setLocked(true);
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Locked
Unlocked

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onRed LED on, latch locked
Right pole near the marked faceGreen LED, latch opens
Wrong poleNo change
Wait ten secondsRelocks and the red LED returns
Serial monitorLocked and Unlocked messages

If it does not work

Never unlocks

Try the other pole, and check the 10 kilohm pull-up to 5V.

Always unlocked

A magnet is nearby, or the sensor is wired backwards.

Servo twitches

Power the servo from a separate 5 V supply with a shared ground.

Board resets when the latch moves

The servo is stalled on the box, ease the latch.

Output reads low all the time

Check pin 1 and pin 2 are not swapped.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the magnet-key lock demo?

Arduino Nano, Hall Effect Sensor A3144, SG90 Micro Servo, Resistor 10 kΩ, LED 5 mm red, LED 5 mm green, Resistor 220 Ω × 2, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.

How long does the magnet-key lock demo take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the magnet-key lock demo works?

Power on: Red LED on, latch locked. Right pole near the marked face: Green LED, latch opens. Wrong pole: No change.

Never unlocks: what should I check?

Try the other pole, and check the 10 kilohm pull-up to 5V.

Always unlocked: what should I check?

A magnet is nearby, or the sensor is wired backwards.

Servo twitches: what should I check?

Power the servo from a separate 5 V supply with a shared ground.

Board resets when the latch moves: what should I check?

The servo is stalled on the box, ease the latch.