Build a Magnetic Levitation (Maglev) Display: Hall Sensor Feedback and PID Coil Control
Electromagnet coil or magnet wire for winding your own (28-30 AWG)
Hall effect sensor, linear/analog output (SS49E or A1302)
N52 neodymium magnet for the floating object
- ESP32 or Arduino board for PID control loop
MOSFET module rated for the coil current (logic-level, low Rds-on)
- 12V power supply, 2-3A
Flyback/snubber diode for the coil
3D printing filament for the stand and coil bobbin (PETG recommended for heat tolerance)
Prices and availability as of Nov 22, 6:51 AM ET and subject to change. The price shown on Amazon at the time of purchase applies.
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:
- Mount the sensor as close to the coil's center axis as the coil bobbin design allows, since off-axis placement introduces a non-linear relationship between actual height and sensor reading.
- Characterize the sensor's output against known distances before writing any control code — hold the floating magnet at several measured heights by hand (or a temporary jig) and log the raw sensor reading at each, so you know the real relationship rather than assuming it's linear.
- Filter the raw Hall sensor signal with a light exponential moving average in firmware; the coil's own switching noise otherwise bleeds into the sensor reading and feeds directly into the control loop as false position data.
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
| Term | Effect in this system | Tuning note |
|---|---|---|
| Proportional (P) | Main restoring force — how hard the loop corrects based on current position error | Too 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 gravity | Keep 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 is | Usually 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
- 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.
- 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.
- 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.
- 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
| Symptom | Likely cause |
|---|---|
| Object won't catch/levitate at all | Coil 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 falls | Insufficient derivative gain, or control loop running too slowly to keep up |
| Object settles but noticeably lower than the intended height | Integral gain too low, or setpoint miscalibrated against the actual sensor-to-height relationship |
| Coil or MOSFET runs hot | Check 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.