The „Magnetic Levitation“ project is truly fascinating and has the potential to amaze everyone. It allows objects to levitate seemingly out of thin air. In order to implement this project, you need to have a basic knowledge of electronics as well as experience working with an Arduino. But even if you don’t have any experience in this field, it shouldn’t be a problem. In this article I will detail the entire building process, electronics and programming.
This project is a great opportunity to learn simple electronic circuits and take your first steps with an Arduino. It is particularly well suited for school classes because it can be done comparatively quickly and the learning curve is steep. Pupils can thus better understand the force of electric magnetic fields and see what a big impact even the smallest changes in the program code can have. Precise tuning of the electromagnet is critical, as even minor adjustments can have a significant impact on its function. It is necessary to wind a coil and solder a small circuit board. It is also possible to build the project completely without 3D printing. But it just looks visually nicer.
In summary, this project is perfect for school classes.
3D printing of the Magnetic Levitation
3D printing should work without problems with any material. The parts were all designed to be printable without a support structure. I printed the parts with my self designed and built 3D printer. This one did a good job for many years until it was replaced by a replaced by a Voron 2.4. It is also possible to build the project without 3D printed parts. To do this, all components are mounted on a tripod or similar.
Winding the electromagnet
The coil for the electromagnet is made of 0.5mm thick lacquered copper wire. The highest precision is required when winding the coil, as all windings should be close and evenly adjacent to each other. A mistake in winding cannot be undone. The precision of the winding directly affects the efficiency of the electromagnet. Personally, I have managed this task with a drill. The exact number of windings is not critical. The point is to completely wrap the coil with wire, and then fine-tuning is done using the program code.
The assembly of the Magnetic Levitation
A Hall sensor is used to detect the distance of the floating object. It is important to make sure that the Hall sensor used provides a linear voltage output, as some Hall sensors only have a digital output and are not suitable for this project. I used the low-cost KY-024 Hall sensor, which is Arduino-compatible and operates at 5V. This sensor comes on a small board that already has all the necessary components built in.
In order to put the sensor in the right position, namely just below the electromagnet, it is necessary to remove the sensor from the board and solder it to longer cables. Then the coil and the cables are attached to their intended place.
How can the magnetic levitation work?
A Hall sensor uses the Hall effect, which occurs when an electric current flows through a semiconductor and the semiconductor enters a magnetic field. This situation generates a measurable Hall voltage that is directly proportional to the strength of the magnetic field. The Hall sensor detects this voltage and converts it into an electrical signal. This allows magnetic field detection without direct contact and is of great importance in various applications.
When a magnet approaches the Hall sensor, the measured voltage increases, which is interpreted by the Arduino. Therefore, the control of the electromagnet tries to generate the exact magnetic field strength required to keep the levitated object stable.
Suppose the electromagnet has a certain „strength“, and the levitating object comes closer. The Hall sensor detects this and sends a signal to the Arduino. The Arduino then adjusts the electromagnet strength down, causing the object to move away again. This causes the measured signal to decrease, and the electromagnet becomes „stronger“ again. This process repeats thousands of times per second, allowing the object to levitate. A certain analog value is now set by means of a potentiometer which the control tries to hit exactly.
The logic of the program code
Two point controller
My first attempt to levitate objects was realized with a simple two point controller. If the magnet gets closer to the hall sensor, the coil is switched off. Vice versa, when the magnet moves away from the sensor, the coil is switched on. This works quite well for a few seconds. The problem is that this system does not detect the change in speed. The magnet is only switched on completely or switched off completely. So there are no intermediate steps. With time, the system oscillates so much that the object falls down. So a logic is needed that determines the change of the distance over time and counteracts it with exactly the right „magnet strength“.
I did not have to reinvent this logic. It exists in the form of a PID controller.
A PID controller is a form of feedback control used to precisely control the output variable of a system. It continuously compares the measured quantity to a desired value and adjusts the control of the system to minimize the difference.
This means that the measured analog signal is continuously checked for a „P-value“ (Proportional component), an „I-value“ (Integral component) and a „D-value“ (Differential component). The result of these values results in an exactly correct strength of the electromagnetic field of the magnet.
The PID control is quite complex, so I will only cover it in outline here.
P (Proportional): This component responds proportionally to the current error magnitude (difference between measured and desired magnitude). It ensures that the output of the controller is adjusted in relation to the current error value.
I (Integral): This component handles the cumulative effect of the error over time. It corrects slow, steady errors and ensures that the controller’s output eventually reaches the desired value.
D (Differential): This component responds to the rate of change of the error over time. It helps detect rapid changes in the error and can help protect the system from overshoot or unstable responses.
The D-value, or differential component, helps improve the stability of the control loop by accounting for the rate of change in the error and adjusting the output control accordingly. This can help the system respond more quickly to changes and prevent unwanted oscillations.
Precise tuning of the P, I, and D values is critical to achieve an optimum levitation condition. Adjusting these values can be a bit tedious as the smallest changes can make a big difference. Once the correct values are found, however, they will work for any object you want to levitate.
You can find the complete Arduino code here.
The construction of the electronics requires only a few components and should be feasible by anyone interested. As microcontroller I used an Arduino Nano. This needs an operating voltage between 7 and 12V. In order not to load the Arduino with the full 12V and not to overload the internal voltage regulator, I installed a voltage regulator of the type 7806. This reduces the input voltage from 12V to 6V, and the Arduino still works fine.
The Hall sensor is connected to one of the analog inputs of the Arduino, in my case this was A0. Of course, this assignment has to be adjusted accordingly in the program code.
To set the „set point“, that is the point that the control should keep, I added a potentiometer. The exact resistance value of the potentiometer is not critical. I used a 10k potentiometer, but a 100k potentiometer should also work.
Since the Arduino cannot supply the necessary currents for the coil directly, it is necessary to add an amplifier stage with a transistor. For this I used a BDW93C transistor connected to a PWM output of the Arduino via a 1k Ohm resistor.
It is important to fuse the coil with a diode. This is necessary because high voltage spikes can occur when switching off inductors like coils. These voltage peaks are short-circuited by the diode and thus safely dissipated.
You can find the complete schematic here.
And that’s all the witchcraft. Basically, it’s not rocket science. Here are a few more impressions: