Home▸Electromagnetism▸Magnetic Levitation (2D) — PID-Controlled Electromagnet & Ball

Magnetic Levitation (2D) — PID-Controlled Electromagnet & Ball

2D electromagnetic-suspension lab: a coil pulls a ferromagnetic ball with F = k·I²/x², a PID controller reads the gap and drives the coil current to hold it at setpoint, and you can nudge or drag the ball to see when active levitation goes unstable.

Electromagnetism2DModerate60 FPS📱 Mobile-adapted⇄ 3D version
2d-magnetic-levitation-electromagnetism ↗ Open standalone

The 3D version of this simulation is a decorative shared-template scene reused across a family of unrelated "labs" — a generic percentage-slider dashboard with no levitation formula behind it. This 2D companion builds the real mechanic the title promises instead: a ferromagnetic ball on a vertical track, an electromagnet whose pull follows the inverse-square law F = k·I²/x², and a PID feedback loop that reads the gap and drives the coil current to hold the ball at a setpoint — the same open-loop-unstable, closed-loop-stabilized system used to teach magnetic-levitation control in real labs. Nudge the ball, drag it by hand, overload it with a heavier load, or detune the gains until it oscillates or drops, and watch the live gap/current/force readout and error strip-chart respond in real time.

⚙ Under the hood

2D PID-controlled electromagnetic levitation lab: F = k·I²/x² coil force, a proportional-derivative controller on the measured gap, draggable/nudgeable ball, and a live gap/current/force/stability readout with an error strip-chart.

magnetic levitationelectromagnetpid controlfeedback stabilityinverse-square force

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)