Magnetic levitation in a DIY context is the active suspension of a ferromagnetic object using a feedback-controlled electromagnet that continuously adjusts its pull to counteract gravity. When you introduce levitation into a circuit, it changes a static magnetic design into a high-speed, closed-loop control system requiring microsecond sensor sampling and PWM-driven power stages. You cannot simply arrange permanent magnets to achieve stable levitation; you must build a dynamic system that reacts to physical disturbances in real time.
The Core Physics: Why Passive Levitation Fails
If you have ever tried to balance two neodymium magnets with like poles facing each other, you experienced the physical limitation that dictates all magnetic levitation DIY projects. This limitation is defined by Earnshaw's Theorem, which proves that no static arrangement of permanent magnets can maintain a stable equilibrium in all three spatial axes. If you constrain the magnets laterally with a physical guide, they will slide or flip to find a lower energy state.
Sizing the Electromagnet and Power Stage
Before writing any firmware, you must size the magnetic circuit and the power electronics. Let us look at a worked numeric example for suspending a standard M8 steel bolt.
- Calculate the Required Force: A standard M8 steel bolt weighs approximately 50 grams (0.05 kg). The force of gravity acting on it is F = mg = 0.05 kg × 9.81 m/s² = 0.49 N. Your electromagnet must be capable of pulling significantly more than 0.49 N to allow the controller room to correct for disturbances.
- Design the Coil: We wind 800 turns of 26 AWG enameled copper wire around a 10mm soft iron core. This yields a coil resistance of roughly 12 ohms.
- Size the Power Stage: At a 12V supply, the maximum steady-state current is 1A (12W). However, the inductive kickback when the MOSFET switches off will generate high voltage spikes. We select an IRLZ44N logic-level N-channel MOSFET rated for 55V and 47A, giving us massive thermal headroom.
- Add Flyback Protection: A 1N4007 diode is placed in reverse bias across the coil terminals. Without this, the inductive spike will instantly punch through the MOSFET's drain-source junction and destroy your microcontroller.
On the bench, this specific 800-turn coil pulling the 50g M8 bolt requires roughly 400mA to maintain a stable 15mm air gap. Because the coil is rated for 1A, the PID controller will operate at a baseline PWM duty cycle of about 40%, leaving 60% overhead to pull the bolt back up if it drops.
Where You Meet This In Practice
While a floating steel bolt makes an excellent desk toy, the exact same closed-loop electromagnetic principles scale up to critical industrial and commercial infrastructure. You meet this technology in practice in several key areas:
- Maglev Transit: Electromagnetic suspension (EMS) trains, like the Transrapid, use active electromagnets to pull the train upward toward a ferromagnetic guideway, maintaining a precise 10mm gap at speeds exceeding 400 km/h.
- Magnetic Bearings: High-speed flywheel energy storage systems and industrial turbomolecular pumps use active magnetic bearings to eliminate mechanical friction, allowing rotors to spin at over 50,000 RPM in a vacuum.
- Vibration Isolation: Precision optical tables and semiconductor lithography machines use electromagnetic actuators to actively cancel out sub-micron floor vibrations, keeping the payload perfectly isolated.
Bench Scenario Walkthrough: Building a Hall-Effect Levitator
Theory is clean; the workbench is not. Here is a real-world scenario walkthrough of building this circuit, including the exact failure mode you are likely to encounter.
The Setup: We use an Arduino Nano reading an SS49E linear Hall-effect sensor mounted flush at the bottom of the iron core. The sensor outputs an analog voltage proportional to the magnetic flux density. The Arduino runs a PID control algorithm at a 1kHz sampling rate, outputting a PWM signal to the gate of the IRLZ44N MOSFET.
The Numbers:
Target gap: 15mm (Sensor reads 2.5V at this distance).
PID Tuning: Kp = 45, Ki = 0.5, Kd = 12.
PWM Frequency: 20 kHz (to keep the coil whine out of human hearing range).
The Outcome: Upon powering the 12V rail and bringing the bolt up to the 15mm gap, the bolt violently oscillates, slamming into the core and then dropping to the table. The MOSFET becomes too hot to touch within 10 seconds.
What Went Wrong:
The derivative term (Kd) in a PID controller calculates the rate of change of the error. The SS49E Hall sensor is highly susceptible to 60Hz mains hum and ADC quantization noise. The derivative term amplified this high-frequency noise, interpreting it as rapid physical movement of the bolt. The Arduino responded by slamming the MOSFET fully on and off at 20 kHz.
Furthermore, at 20 kHz, the 1N4007 flyback diode (which has a slow reverse recovery time) failed to clamp the inductive spikes fast enough, causing massive current ringing that dissipated as heat in the MOSFET.
- Solder a 100nF ceramic capacitor directly across the Hall sensor output pin and ground to filter high-frequency noise before it hits the ADC.
- Drop the PWM frequency to 4 kHz using Arduino timer registers.
- Replace the 1N4007 with a UF4007 ultra-fast recovery diode or a Schottky diode like the 1N5819 to handle the rapid switching edges.
Common Confusions and Troubleshooting Pitfalls
When researching magnetic levitation DIY projects, makers frequently confuse active electromagnetic attraction with passive magnetic repulsion. Many beginners attempt to build a levitator by placing a strong neodymium magnet on the table and trying to float another magnet above it using electromagnets pushing it away. Because of Earnshaw's theorem, lateral instability will always ruin a purely repulsive static setup. Professional repulsive systems (like superconducting maglev) rely on the Meissner effect or diamagnetism, which are entirely different physical phenomena requiring cryogenic cooling or specialized materials like pyrolytic graphite.
Another common pitfall is ignoring the inductive time constant of the electromagnet. An electromagnet is essentially a massive inductor. Current does not rise instantly when the MOSFET turns on; it rises along an exponential curve dictated by $\tau = L/R$. If your coil has too many turns (high inductance), the magnetic field cannot build up fast enough for the PID loop to correct a falling object. If your object drops instantly when you let go, your coil inductance is likely too high for your control loop frequency.
FAQ: Magnetic Levitation DIY
Can I use a standard relay instead of a MOSFET for the electromagnet?
No. A mechanical relay cannot switch fast enough for a PID control loop. A typical relay has an operate time of 5 to 15 milliseconds. A stable levitation loop requires switching adjustments in the microsecond range. You must use a solid-state switch like a logic-level MOSFET or a dedicated motor driver IC.
Why does my Hall sensor read erratic values when the electromagnet turns on?
The Hall sensor is measuring the total magnetic flux in the gap, which includes both the permanent field of the suspended object (if it is magnetized) and the massive, rapidly switching field of your electromagnet. You must physically separate the sensor from the coil's direct flux path, or use a differential Hall sensor setup that cancels out the common-mode electromagnet flux while detecting the gradient caused by the steel object.
What is the best microcontroller for a DIY magnetic levitator?
While an Arduino Uno or Nano works for basic setups, a 32-bit microcontroller like the Teensy 4.0 or an ESP32 is vastly superior. They offer 12-bit or 16-bit ADCs (compared to the Arduino's 10-bit ADC), providing much finer resolution of the air gap, and hardware PWM generators that do not jitter when the main processor is executing complex PID math.






