jremington:
The diode is the last thing I would suspect.
From the page he linked:
D2 -- Make sure it's a fast switching diode like a Schottky. My standard N4007s were too slow and ended up causing instability that took weeks to track down. It looks pointless in this diagram, being wired in reverse, but it's actually used to short out back-EMF from the coil when turned off. Otherwise, you get very short high voltage spikes back into your circuit, potentially blowing the MOSFET or at least giving you false readings and crashes on the Arduino.
Look for diodes that specifically mention (in the datasheet) fast switching speed, or advertise their being suitable for switch mode power supplies or DC-DC converters (that's the dominant application for high speed schottky diodes).
jremington:
The diode is the last thing I would suspect.
More likely, you have not correctly chosen the PID parameters. Look up "PID tuning".
I did the PID tuning more then 10 hours, I did it after reading a lot about it.
Believe me, the last thing I want to blame is a diode, but I did all I could regarding the tuning, it just not stable.
As you can see in the post I've linked to, the guy that did the project had the same problem with my diode, so I guess it is my best shot.
I looked over the code, and it is an absolute mess. Comments like the following suggest that it should just be thrown out completely and start over. On the web, you sometimes do get what you pay for!
//This seems pointless, but is used to deal with overflow in millis()'s output when downcast. If it overflows while in idle mode, it would stick in filter waiting for millis to go all the way back up to the last calculated point.
//This slows down the change in the electromagnet, making the device substantially quieter but taking a lot longer to stabilize (on the order of 30 seconds)
If the code can take a second, (leave alone 30 seconds) to stabilize, I assure you that the diode is not the problem.
For PID to work properly, it is very important that the control loop timing is constant. That is not the case with this code, so it is hopeless.
I see no reference to how the circuit board is laid out
My comment has nothing to do with the circuit board layout; rather the fault clearly lies in the essentially unworkable implementation of the PID algorithm.
The code changes the output 1000 times per second, maybe
In one millisecond, an object at rest falls D = (1/2)at^2 = 0.59.810^-6 = 4.9 micrometers (about .005 millimeters). Therefore it is not necessary for the loop to execute that rapidly, or to switch the magnet that rapidly.
See if you can find a copy of Circuit Cellar #18 (Dec. 1990). Jeff Bachiochi gave complete plans for a perfectly functional magnetic levitation apparatus that was programmed in about a dozen lines of BASIC.
Thank you for your answers.
jremington calculation about speed led me to believe that i can reduce the refresh rate drastically, even in a speed of 1cm per second.
A rate of 100 times per second should be enough because it will move roughly 0.1mm on every cycle.
The connection on the sensor weren't perfect. I still have problems with soldering, there are things that just don't stick to solder.
I printed a serial output every half a second, That destroyed the PID totally and caused a lot of instability.
After fixing both problems and readjusting the PID values, the object floats for 1-2 minutes perfectly, but then it slowly starts to oscillate till it falls.
Any general suggestion regarding the PID ? I've set the
P to 4.7
D to 4.6
I to 0 - pretty useless, couldn't get any improvement by changing it, only increases the oscillation