AVR Atmega Based PID Magnetic Levitator

[Davide] saw our recent post on magnetic levitation and quickly sent in his own project, which has a great explanation of how it works — he’s also included the code to try yourself!

His setup uses an Atmega8 micro-controller which controls a small 12V 50N coil using pulse-width-modulation (PWM). A hall effect sensor (Allegro A1302) mounted inside the coil detects the distance to the magnet and that data is used by a PID controller to automatically adjust the PWM of the coil to keep the magnet in place. The Atmega8 runs at 8Mhz and the hall effect sensor is polled every 1ms to provide an updated value for the PWM. He’s also thrown in an RGB LED that lights up when an object is being levitated!

So why is there a kid with a floating balloon? [Davide] actually built the setup for his friend [Paolo] to display at an art fair called InverART 2013!

After the break check out the circuit diagram and a short demonstration video of the device in action!

Oh yeah, those of you not impressed by magnetic levitation will probably appreciate acoustic levitation.


(click for full size diagram)

33 thoughts on “AVR Atmega Based PID Magnetic Levitator

  1. Just a silly comment: since there’s no friction on the object being levitated by that which is suspending it, if this was put in a complete vacuum, could you spin something and it would spin for infinity?

    I know it’s useless as you wouldn’t be able to harvest any energy from it but it’s just a thought I had…. it’d be like a top on a frictionless surface with no air friction/drag… right?

    Sorry if there’s a glaring hole in my logic, as I’m completely uneducated on the matter and relatively young >.<

    1. Yes it would spin forever because nothing would stop it. Hand a generator on it and it would stop because of drag ofcourse.

      Same as astronauts in IIS would be able to get stuck in the exact middle of a room and never be able to reach the sides if it wasn’t for the airflow through the space station :)

      1. “Same as astronauts in IIS would be able to get stuck in the exact middle of a room and never be able to reach the sides if it wasn’t for the airflow through the space station”

        I suppose that’s another reason to eat your beans with every meal. :P

        1. Um, I don’t think you actually need artificial help there ;-) Would simply not breathing in slowly and blowing out fast (from either end!) do the trick?

          Of course you can then start throwing bits of your spacesuit away to propel yourself in the other direction….

          1. Don’t forget that the ISS is filled with oxygen/nitrogen. All you need to do is to use your hands as wings and fly like a bird. Or swim in the air. It may be slow, but definitely faster than breathing like a crazy person or strip-teasing or use your behind’s gas!

      2. It would be possible to propel yourself by breathing in in one direction and breathing out in the other. It would take a hell of a long time but you would eventually move and hit a wall if you kept it up. Or just undress and throw your clothes in one direction as acceleration mass, just be prepared for the question “why are you naked, and why are your clothes all over the instrument panel?”

    2. Interesting question.

      If the object being levitated was COMPLETELY free from the influence of external forces/fields, and the vacuum was perfect then yes, there’s no reason it couldn’t rotate forever.

      However, the biggest issue I see in the real world is eddy current losses. The rotating object is a piece of moving metal, and it’s present in a magnetic field. Therefore you will have induced eddy currents which will try and slow down the rotation. Even if you ignore the field of the driving coil, you’ve still got Earth’s magnetic field, albeit many times weaker.

      So I would say that it will eventually come to rest, perhaps quicker than you might think!

      1. Interesting reply!

        I’m assuming that even if the object attached to the magnet was, say, plastic, the magnet itself would still cause eddy current issues.

        It would be a fun experiment, however, to see how long you could get something like that to spin!

    1. Since you’re controlling the coil, you know what value it’s at, and can offset the hall effect sensor appropriately.

      Alternately, you can have a sensor on top of the coil and on the bottom of the coil; both will read the same value from the coil but different values from the magnet, so you just subtract the smaller value from the bigger value and viola. :)

      1. Another alternative, since it’s a PWM control for the coil, he could setup his sensor reads to only occur between pulses. This alternative is limited to how quickly the sensor stops reading the magnetic field.

    2. It is, but that’s okay. Because of the superposition principle, the strengths of the magnetic fields simply add up, so you could cancel it out if you know one or the other….
      But it turns out you don’t even need to do that! If you define the target of the PID as the field strength at the hall effect sensor and set the P term of the control loop to a negative gain, the controller will respond to a stronger field at the sensor by lowering the power of the coil, and vice versa.
      What this means is that when the magnet gets closer, the controller will decrease the strength of the coil, letting the magnet drop, and when it gets far away, the controller will compensate by increasing power and pulling it closer. See how you don’t need to “cancel out” the coil’s field to make it work?

      1. Riiiight… except that as you’re detecting the magnet dropping, resulting in a lower signal on the Hall effect sensor, you’re also adjusting the coil itself, which changes the value of the Hall effect sensor too. So you do, in fact, have to cancel out the coil’s field to make it work.

  2. Confused noob here. How is the Hall Effect sensor not thrown off by being in the middle of a magnetic coil? Is the field pulsed off just long enough for the sensor to work or…?

    1. The sensor is affected by both the magnetic coil and the levitated magnet. But since, at any given time, the program knows how strong the magnetic coil is (it’s controlling it, after all) it also knows how much to offset it.

  3. “small 12V 50N coil”
    Anyone know what the N refers to regarding a coil or electromagnet? I am trying to figure out what it can be referring to and am drawing a blank– turns, inductance, resistance, force? Anyone, anyone? Bueller?

    1. I would say no. This system resembles a pendulum at rest. However, flipping it over would make it unstable and prone to falling. The tiniest sideways force would send it downward.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.