← Projects
electronics intermediate 16 days ago ◑ 34 views ◯ 5 min read

Build a Magnetic Levitation (Maglev) Display: Hall Sensor Feedback and PID Coil Control

Build time: 1-2 weekends
Tools needed: Soldering iron and basic hand tools, a multimeter, a laptop for firmware (Arduino IDE or PlatformIO), a 3D printer for the coil bobbin and stand, a bench power supply useful for initial testing
magnetic levitationmaglevhall sensorPID controlelectromagnetdisplayfeedback controlESP32
Parts List

A magnetic levitation display suspends a small magnetic object in midair, with no visible support, by actively balancing the pull of an electromagnet against gravity hundreds of times a second. Unlike a magnetic encoder or a passive magnet arrangement, this is inherently unstable physics — Earnshaw's theorem rules out passive, static magnetic levitation of a free object — so the whole trick is a closed-loop feedback system: a Hall effect sensor continuously measures how far the floating object has drifted from its target height, and a PID control loop adjusts current through the electromagnet many times a second to pull it back, fast enough that the object appears to hang motionless.

This is a genuinely good small-electronics project for understanding closed-loop control in a visually obvious way — unlike tuning a PID loop for, say, a 3D printer's hotend (where you can only really judge it by a temperature graph), a mistuned maglev loop visibly oscillates, drifts, or drops the object, and a well-tuned one holds it rock steady. It also makes a genuinely striking desk display once it's dialed in.

The Physics, Briefly

The electromagnet sits above the floating magnet and pulls it upward against gravity. Left alone, this is unstable in both directions: if the floating magnet drifts even slightly closer to the coil, the magnetic pull increases (since field strength falls off steeply with distance), pulling it in the rest of the way; if it drifts slightly farther away, the pull weakens and gravity wins, and it falls. The control loop's entire job is to counteract this instability continuously — reduce coil current the instant the object gets too close, increase it the instant it drifts too far, fast enough that the correction happens before the instability can run away.

Sensing Position

A linear Hall effect sensor mounted directly below the electromagnet, in line with the floating magnet, outputs an analog voltage proportional to the magnetic field strength at that point — which correlates directly with the floating magnet's distance from the coil. This is the measurement the whole control loop depends on, so sensor placement and calibration matter more than almost anything else in the build:

Coil and Driver

Wind or buy a coil sized to produce enough magnetic force at your intended levitation height for your chosen floating magnet's mass — as a starting point, a coil of several hundred turns of 28-30 AWG magnet wire around a ferromagnetic core, driven at 12V through a logic-level MOSFET, comfortably levitates a small (a few grams) neodymium magnet at 15-25mm gap. Drive the coil with PWM from the microcontroller through the MOSFET, with a flyback diode across the coil to clamp the voltage spike generated every time the PWM signal switches the coil current off — skipping the flyback diode is the single most common way to kill the MOSFET in this kind of build.

The PID Loop

TermEffect in this systemTuning note
Proportional (P)Main restoring force — how hard the loop corrects based on current position errorToo low and the object sags or falls; too high and it oscillates violently
Integral (I)Corrects steady-state offset — compensates for the fact that the "zero error" coil current isn't actually zero, it's whatever current balances gravityKeep this term modest; too much integral gain causes slow, growing oscillation
Derivative (D)Damping — responds to how fast the object is moving, not just where it isUsually the term that takes a system from "unstable oscillation" to "settles quickly" — tune this in alongside P rather than as an afterthought

Start with P-only control and increase the gain until the object levitates but oscillates visibly, then add derivative gain to damp that oscillation out, and finally add a small amount of integral gain to eliminate any remaining steady sag. Run the control loop as fast as your microcontroller and PWM hardware allow — at least 1kHz, ideally several kHz — since a slow loop simply can't react before the object has already drifted too far.

Mechanical Build

  1. 3D print a bobbin to wind the coil on, with a mounting flange to fix it to the stand directly above the sensor and the intended floating position.
  2. Mount the Hall sensor on-axis directly beneath the coil, ideally embedded in a small 3D-printed holder that keeps it perfectly centered and prevents it from shifting after calibration.
  3. Build or print a simple arch or bracket stand that holds the coil assembly a fixed, known distance above a base — leave this adjustable (a slotted mount) for your first calibration pass, then lock it down once tuned.
  4. House the driver electronics in the base, away from the coil's magnetic field, since a nearby ferromagnetic enclosure or stray field can distort your Hall sensor readings.

Troubleshooting

SymptomLikely cause
Object won't catch/levitate at allCoil isn't strong enough for the magnet's mass at your target gap, or sensor calibration is off enough that the loop is chasing the wrong setpoint
Object oscillates and eventually fallsInsufficient derivative gain, or control loop running too slowly to keep up
Object settles but noticeably lower than the intended heightIntegral gain too low, or setpoint miscalibrated against the actual sensor-to-height relationship
Coil or MOSFET runs hotCheck for a missing or backwards flyback diode, and confirm PWM frequency isn't so high that switching losses dominate

Once a single-point levitation is stable, this platform extends nicely into a small levitating display — a tiny printed object, a miniature globe, or a light, 3D-printed platform riding the floating magnet to carry something decorative, as long as you stay within the coil's lift capacity for the added weight.