# Difficulties in tuning magnetic levitation system on Arduino and MATLAB?

So I am dealing with a project that revolves around magnetically levitating an object below an electromagnet. I created a transfer function of it after making a mathematical model of it, and got this as my result:

Now when I put this on MATLAB's Simulink in order to use its PID functions to tune it, I got exceptionally large values in which I then inserted into the gains as you can see in the figure below. When running these gains, I got a stable result.

However, when I put this code in my Arduino's magnetic levitation code, specifically the PID parameters, it just became a vibration machine that would just vibrate my neodymium magnet to be levitated and that was it. No stability, if i let go then it would stick up.

I'm genuinely confused as to why I have received such different outputs when testing it in real life.

For more information, I got this system by doing a mechanical calculation(newton's second order equation of motion) of the forces of my electromagnet and the neodymium magnet's weight. My electromagnet has a nominal resistance of 5Ohms and Inductance 47mH. I am using a 12V 1A power supply. Schematics of this system are based off of this tutorial: https://www.hackster.io/jsirgado/magnet-levitation-with-arduino-eeeee4

• neodymium magnet's weight. ... what about the force of the magnet's attraction to the electromagnet core? Mar 31, 2022 at 17:48
• It's weak, when the electromagnet is turned off, the neodymium magnet does not fly up to the electromagnet unless it gets turned on. Mar 31, 2022 at 18:04
• maybe the hall-effect sensor is not the linear type Mar 31, 2022 at 18:07
• Yeah it's a ratiometric one fortunately enough, can detect/output a range of values depending on the distance of the magnetic field Mar 31, 2022 at 20:12
• In addition to the mass of the magnet the strength and shape of it will also determine some of the control parameters. Note that even in the video the user needs to make an adjustment when trying to balance the cylindrical shaped magnet.
– Nedd
Mar 31, 2022 at 21:05

I believe your using the wrong equations for your transfer function. The equations that you are referencing to in the above article are for levitating a soft magnetic material (such as steel). If you are using a hard magnetic material (such as a permanent magnet) the equations are going to be different. The reasoning is that the mechanism that generates force is different. The soft magnetic material's force is due to the change in inductance vs change in position. Whereas the permanent magnets flux couples with the coil and the force mechanism is the change in magnetic flux vs the change in position.

In addition, a permanent magnet has the same permeability as air, so the inductance that the coil sees does not change with respect to the position of the magnet. Steel has a permeability around 2000 times that of air. Hence the inductance changes when the object gets closer or further from the coil.

The amount of flux that couples with the coil is going to change with respect to the position of the coil you are using. I would highly recommend modeling your system in FEMM and solving for the flux linkage at several magnet positions.

I will solve out for you so you can see the difference

First perform a power balance between mechanical power and electrical power. $$$$P_m=\frac{Fx}{t}\\ P_e=vi\\ P_m=P_e=\frac{Fx}{t}=vi$$$$ Next apply faradays law.

$$$$v=\frac{d\lambda}{dt}\\$$$$

For a soft magnetic object (steel) total magnetic flux $$\\lambda\$$ is a function of the inductance. The inductance is a function of the distance from the core. $$$$\lambda = L(x)i$$$$

Now put it all together. I believe the chain rule is used here. $$$$P_m=P_e=\frac{Fx}{t}=\frac{d\lambda}{dt}\\$$$$ Break down the derivative into partials. $$$$\frac{Fx}{t}=\frac{i^2}{2}\frac{d(L(x))}{dx}\frac{dx}{dt}$$$$ Finally canceling out the velocity term $$\\frac{x}{t}\$$ gives you the following relationship. $$$$F=\frac{i^2}{2}\frac{d(L(x))}{dx}$$$$

Now look at the units to make sure they work out to be in Newtons $$$$kg*m*s^2=(A^2)(kg*m^2*s^{−2}*A^{−2})(m^{-1})$$$$## Heading ##

For a hard magnet material the total magnetic flux $$\\lambda\$$ is a function of the magnetic flux that couples with the coil. The inductance does not change with position $$\L(x) = 0\$$ so we can leave it out, as it contributes zero torque. $$\\lambda_m\$$ is the B field normal to the coil times the area of the coil (it comes from gauss law)

$$$$\lambda = \lambda_m(x) + L(x)i\\ \lambda = \lambda_m(x) = B*A$$$$

Now put it all together $$$$P_m=P_e=\frac{Fx}{t}=i\frac{d\lambda_m}{dt}$$$$ Break down the derivative into partials. $$$$\frac{Fx}{t}=i\frac{d(\lambda_m(x))}{dx}\frac{dx}{dt}$$$$ Finally canceling out the velocity term $$\\frac{x}{t}\$$ gives you the following relationship. $$$$F=i\frac{d(\lambda_m(x))}{dx}$$$$

Now look at the units to make sure they work out to be in Newtons $$$$kg*m*s^2=(A)(kg*m^2*s^{−2}*A^{−1})(m^{-1})$$$$

In conclusion in order to model $$\\frac{d(\lambda_m(x))}{dx}\$$ you likely need to use finite element analysis. I would recommend you make a simple model using FEMM. But the biggest difference is that you will see that the current is squared in one equation and not in the other equation. This kind of assumption would make a huge difference in your model.

• Wait for real?? I thought they would be applicable for neodymium magnets, wow. Apr 1, 2022 at 11:02
• Crazy, alright fortunately enough I do have steel balls lying around so I can compensate for this. In fact I have few more questions about this, disregarding the fact that I am using a neodymium magnet for this system, is it normal to have such high gain values when tuning the system? Even when I change to a steel ball, I still have relatively the same mass, I still have an input current of 1A and the distance will still be 8mm, and that's just tripping me up. Apr 1, 2022 at 11:14
• Furthermore, in those papers you see that the current values are low, now is that due to them using that low of a current? Or is that the current measured in the inductor? If so then my actual current may be different, yikes. Apr 1, 2022 at 11:16
• I modified my answer. I can't speak about the PID tuning or current. These are going to be highly dependent upon the geometry of your system Apr 2, 2022 at 3:42