Magnetic Levitation Simulator (PID) Back
Electromagnetics & Control Engineering

Magnetic Levitation Simulator (PID Control)

Real-time magnetic levitation simulation. Experience passive levitation instability and PID-controlled active stabilization. From Earnshaw's theorem to maglev train principles.

Levitation Mode
System Parameters
Object mass m (kg)
kg
Target gap d₀ (mm)
mm
Force constant K
N/mm²
PID Gains
Proportional gain Kp
Integral gain Ki
Derivative gain Kd
Disturbance
External force F_ext (N)
N
Live Readings
Results
Current Gap (mm)
Coil Current (A)
Error (mm)
Status
Magnetic Levitation Animation
Gap d(t) vs Time
Coil Current i(t) vs Time
Theory & Key Formulas
$F_{mag}= K \cdot \dfrac{i^2}{d^2}$

$m\ddot{d}= mg - F_{mag}+ F_{ext}$

PID: $u = K_p e + K_i\!\int\!e\,dt + K_d\dot{e}$

What is Magnetic Levitation Control?

🙋
What exactly is the "passive instability" mentioned in the simulator description? Why can't the object just float stably on its own?
🎓
Basically, it's a consequence of Earnshaw's theorem. For static magnetic fields, there's no stable equilibrium point for a magnet in free space—it's either attracted or repelled. In our simulator, the electromagnet attracts the steel ball upward. If the ball gets too close, the force spikes; too far, and gravity wins. It's like trying to balance a pencil on its tip. Try setting all PID gains to zero in the controls; you'll see the ball immediately crash or fall.
🙋
Wait, really? So how does the PID controller fix this? What do the P, I, and D terms actually do in this magnetic case?
🎓
In practice, it's a constant, high-speed correction. The controller reads the gap error $e = d_0 - d$ from the sensor. The Proportional (P) term ($K_p e$) applies a force proportional to the error—like a spring pulling it back to position. The Derivative (D) term ($K_d \dot{e}$) damps the velocity, preventing overshoot and oscillation. The Integral (I) term ($K_i \int e\,dt$) corrects for steady-state errors, like if the ball's weight wasn't perfectly compensated. Try moving the Kp slider first; you'll see oscillation. Then add Kd to calm it down.
🙋
That makes sense! So what's the role of the "Force constant K" and the "External force F_ext" parameters? Are they just for theory?
🎓
Great question! They're crucial for realism. The Force constant K defines how strong your electromagnet is. A higher $K$ means a smaller current can lift the same mass, but the system can also become more sensitive and harder to control. The External force F_ext lets you simulate real-world disturbances. For instance, imagine giving the levitating object a gentle poke. Try setting a positive F_ext for a second—you'll see the controller struggle to reject this disturbance, which is exactly what engineers test for.

Physical Model & Key Equations

The core physics is Newton's second law applied to the levitating object. The net force determines its acceleration. The magnetic force is governed by the electromagnet's characteristics and the instantaneous current.

$$ m\ddot{d}= mg - F_{mag}+ F_{ext}$$

Where:
$m$: Mass of the levitating object (kg).
$\ddot{d}$: Second derivative of the gap (acceleration).
$g$: Gravitational acceleration (≈ 9.81 m/s²).
$F_{mag}$: Upward magnetic force.
$F_{ext}$: Any additional external force (e.g., a push).

The magnetic force is not constant; it depends on the coil current $i$ and the gap $d$ in a specific, non-linear way. Simultaneously, a PID controller calculates the required control current based on the error between the target and actual gap.

$$ F_{mag}= K \cdot \frac{i^2}{d^2}\quad \text{and}\quad u = K_p e + K_i\int e\,dt + K_d\dot{e} $$

Where:
$K$: Force constant of the electromagnet (N·m²/A²).
$i$: Coil current (A).
$d$: Instantaneous gap (m).
$u$ : Controller output (which sets $i$).
$e = d_0 - d$: Gap error.
The squaring of current in the force law is key to the instability.

Frequently Asked Questions

The PID gains may be insufficient. First, gradually increase the proportional gain (Kp) to strengthen the attractive force, then use the derivative gain (Kd) to suppress oscillations, and if necessary, adjust the integral gain (Ki) to correct steady-state error. A good starting point is to try initial values around Kp=500, Kd=50, and Ki=10.
Earnshaw's theorem states that 'stable levitation is impossible using only static magnetic fields.' In this simulator, you can observe that an object becomes unstable with passive levitation using only permanent magnets, and you can experience that stable levitation is only achieved through active adjustment of the electromagnet current using PID control.
When an external force is applied, the levitation gap fluctuates, but the PID control immediately adjusts the electromagnet's attractive force to restore the original position. This is the same principle by which maglev trains maintain stable levitation against wind pressure and vibrations during operation, allowing you to experience the responsiveness of the control.
K is a constant that determines the strength of the electromagnet. If it is too small, the attractive force will be insufficient and levitation will fail; if it is too large, the control becomes unstable. A typical appropriate range is around K = 0.001 to 0.01, and it should be adjusted according to the object mass and target gap.

Real-World Applications

Maglev Trains: Electromagnetic Suspension (EMS) systems, like the one modeled here, are used in some maglev trains (e.g., Transrapid in Shanghai). Electromagnets in the train's undercarriage attract it upwards to a guide rail, and a sophisticated version of this PID control maintains a stable 10mm gap at speeds over 400 km/h.

Contactless Bearings in Turbomachinery: High-speed centrifuges, compressors, and flywheel energy storage systems use magnetic bearings. Levitating the rotor eliminates mechanical friction and wear, allowing for higher speeds, lower maintenance, and operation in vacuum chambers.

Vibration Isolation Platforms: Precision manufacturing (e.g., semiconductor lithography) and scientific experiments (e.g., gravitational wave detection) require ultra-stable platforms. Active magnetic levitation can isolate sensitive equipment from ground vibrations more effectively than passive springs.

Product Demonstrators and Haptic Interfaces: The compelling visual of a floating object makes maglev a popular educational tool. Furthermore, the principle is used in advanced haptic interfaces, where magnetic force can provide touch feedback to a user's hand in mid-air, enabling interaction with 3D virtual objects.

Common Misconceptions and Points to Note

First, the idea that "simply increasing the P-gain makes it stabilize faster" is a major misconception. While raising Kp does make the system react more quickly to error, going too far causes severe oscillation (hunting), as you saw in the simulator. This means the system is in an "over-controlled" state. In a real device, this can cause coils to overheat or the controller to oscillate, leading to failure. A good rule of thumb is that the value just before oscillation subsides is the first step toward an "optimal gain."

Next, there's a tendency to think "the larger the D-gain, the better it suppresses vibration," but this is also a pitfall. The derivative term looks at the "rate of change" of the error, so it can drastically amplify sensor noise (tiny measurement errors). For example, if there's 0.01mm of noise in the gap measurement, a large Kd can turn that into huge disturbances in the control signal. In practice, "incomplete differentiation" is often used as a countermeasure against noise.

Finally, the mindset of "since the I-gain can eliminate steady-state error, let's set it high from the start." This might be the most dangerous one. The integral term continuously accumulates past errors. So, for instance, after a sudden external force is applied, even after the error is corrected, the accumulated value (integral windup) remains, causing significant overshoot in the control. Implementing an "anti-windup" measure is essential. In the simulator, if you increase Ki and then suddenly apply and release an external force, you should be able to experience this phenomenon firsthand.

How to Use

  1. Set ball mass (0.01–0.5 kg) using sMass slider; heavier objects require stronger electromagnetic force
  2. Adjust initial air gap (5–50 mm) with sGap; smaller gaps increase magnetic force nonlinearly
  3. Configure electromagnetic stiffness (sK, typical 0.5–2.0 N/mm) to define coil responsiveness
  4. Tune PID proportional gain (sKp, range 0.1–10) until coil current stabilizes the gap at setpoint
  5. Monitor Current Gap (mm), Coil Current (A), and Error (mm) in real-time; Status indicator shows stable or unstable

Worked Example

Steel ball (0.015 kg) levitating in electromagnet: initial gap = 15 mm, K = 1.2 N/mm, Kp = 2.5. Gravity pulls ball downward at 0.147 N. With Kp too low (0.8), coil current oscillates 0.6–2.1 A and gap drifts ±3 mm (unstable per Earnshaw). Increasing Kp to 2.5 produces steady coil current of 1.4 A maintaining gap at 15.2 mm ± 0.2 mm. Further increase to Kp = 5.0 causes overshoot; gap cycles 14–16 mm with 0.8 A ripple.

Practical Notes

  1. Earnshaw's theorem proves passive magnetic levitation is unstable; active PID feedback is mandatory—adjust Kp incrementally (±0.2) to avoid limit-cycle oscillation
  2. Air gap inversely affects force: halving gap from 20 mm to 10 mm quadruples attraction; start with conservative Kp values for large gaps
  3. Coil saturation occurs above 3 A for typical ferromagnetic cores; if current maxes out, reduce mass or increase K
  4. Sensor noise tolerance: Error readings ±0.5 mm are normal; excessively high Kp amplifies noise and destabilizes control