Built-in project · Arduino Uno R3

Electromagnet hold and drop on an Arduino Uno

An Uno holds a steel plate with a 12 V P20/15 electromagnet through a MOSFET on pin 9, three seconds on and two off, with a diode across the coil. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $39. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Switching a coil with a MOSFET
  • Flyback diodes across inductive loads
  • Why force depends on current squared
  • Sharing ground between supplies
  • Timing on and off phases
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A 12 V electromagnet draws about 250 mA, far more than an Uno pin can give. So pin 9 drives the gate of a MOSFET, and the MOSFET carries the coil current from a bench supply. The supply's minus is shared with the Uno's ground.

A diode sits across the coil. When the magnet switches off, its coil kicks back a voltage spike. The diode gives that current a path so the MOSFET is not damaged.

The sketch holds the magnet on for 3 seconds, then off for 2 seconds, and repeats. It prints the state to serial. The magnet holds about 25 N, or 2.5 kg, at its rated current.

Parts list

5 parts, plus the jumper wires and a bench instrument. The parts retail for about $39; on Mokxi they're free, and so are the instruments.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
MOSFET pin SArduino Uno R3 pin GND
Bench power supply pin V-Arduino Uno R3 pin GND
MOSFET pin GArduino Uno R3 pin 9
Diode pin AMOSFET pin D
Holding electromagnet, P20/15 pin 2MOSFET pin D
Holding electromagnet, P20/15 pin 1Diode pin C
Bench power supply pin V+Diode pin C

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Electromagnet: an Uno holds a steel plate with a 12 V P20/15 for three
// seconds and drops it for two, through a MOSFET, with a diode across the coil.
//
//   magnet  one lead to the 12 V supply, the other to the MOSFET's drain
//   MOSFET  IRLZ44N: gate -> pin 9, source -> GND
//   diode   1N4007 across the coil: cathode to 12 V, anode to the drain
//   supply  12 V, its - to GND, shared with the Uno
//
// The coil draws 250 mA, more than ten times what a pin can give, so the pin
// drives the MOSFET's gate and the MOSFET carries the current. It is a coil
// like a relay's or a solenoid's, so it needs the diode: switched off without
// one, its current drives the drain far above the MOSFET's rating.
//
// At its rated current it holds about 25 N, 2.5 kg, on a flat steel plate.
// The force goes as the square of the current: on a 6 V supply it holds a
// quarter of that. Set the magnet's load to 3 kg and the plate drops even
// with the magnet on.
//
// Press Run and watch the plate and the serial monitor.

const int GATE_PIN = 9;
const unsigned long HOLD_MS = 3000;
const unsigned long DROP_MS = 2000;

void setup() {
  Serial.begin(9600);
  pinMode(GATE_PIN, OUTPUT);
  digitalWrite(GATE_PIN, LOW);
  Serial.println("Electromagnet ready");
}

void loop() {
  digitalWrite(GATE_PIN, HIGH);
  Serial.println("magnet on: holding");
  delay(HOLD_MS);
  digitalWrite(GATE_PIN, LOW);
  Serial.println("magnet off: released");
  delay(DROP_MS);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

It opens serial at 9600 and makes GATE_PIN (pin 9) an output. It writes LOW first, so the magnet starts off and nothing is held. Then it prints "Electromagnet ready".

void setup() {
  Serial.begin(9600);
  pinMode(GATE_PIN, OUTPUT);
  digitalWrite(GATE_PIN, LOW);
  Serial.println("Electromagnet ready");
}

loop sketch.ino

It sets the gate HIGH, prints "magnet on: holding" and waits HOLD_MS (3000 ms). Then it sets the gate LOW, prints "magnet off: released" and waits DROP_MS (2000 ms). The plate is held, then dropped, over and over.

void loop() {
  digitalWrite(GATE_PIN, HIGH);
  Serial.println("magnet on: holding");
  delay(HOLD_MS);
  digitalWrite(GATE_PIN, LOW);
  Serial.println("magnet off: released");
  delay(DROP_MS);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The plate never drops This project
    The gate may be stuck high, or the MOSFET wired wrong. Check pin 9 goes only to the gate and the source goes to ground.
  • The plate falls even with the magnet on This project
    Check the supply is 12 V. Force goes as the square of the current, so a lower voltage holds much less, and a heavy load can exceed it.
  • The MOSFET gets damaged or resets happen when switching off This project
    Put the diode across the coil with its cathode toward 12 V and its anode on the MOSFET drain.
  • The load stays partly on or flickers when the pin is not driving, such as at boot or in input mode. MOSFET
    A floating gate picks up noise. Add a pull-down of about 10 k from gate to source on an N-channel part, or pull a P-channel gate up to its source voltage.
  • The N-channel load is always on or never turns on. Source, gate, and drain legs were mixed up. MOSFET
    Use the N-channel as a low-side switch: load to the supply, drain to the load, source to ground. Check your datasheet pinout, since 2N7000 and BS250 differ. Share the grounds.
  • The diode is in backwards, so nothing flows, or a flyback diode conducts all the time. Current flows from the anode to the cathode, the end with the stripe. Diode
    Put the striped end toward the more positive side for a series diode. For flyback across a coil, the stripe goes to the positive supply side of the coil.
  • A diode in series drops about 0.7 V (silicon) and 0.3 V (Schottky), so the output is lower than expected. A 1N4148 is also too small for big currents. Diode
    Count the drop in your voltage budget. Use a 1N4007 or similar for amps, and a Schottky when you need a small drop.
  • The magnet barely holds anything. Holding electromagnet
    Check the rated voltage and give it that much. The holding force needs the full current. It also needs a flat, thick steel plate, because thin or rough or painted surfaces hold less.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.