I am new to electronics, and I want to do a project where I make a device that levitates an object with the use of an electromagnet, for learning purposes.

Something like this is what I had in mind: https://www.youtube.com/watch?v=BY-HvrxY8Ic

My initial plan is to use a 12V power source, an ultrasonic distance sensor, a PID implementation on an arduino microcontroller for power regulation, and an OP AMP for amplification.

Does this sound like a good setup? Are there any obvious flaws? I see that someone are using a Hall effect sensor instead for distance measure. Why is this?

All the MagLev projects I have found use a Hall effect sensor for distance calculations. How does one separate the magnetic field generated by the electromagnet from that generated by the permanent magnet? And how can I relate the measured magnetic field to the position of the levitating permanent magnet? Will a linear model suffice?

  • 1
    \$\begingroup\$ Ultrasonic sensors are good for relatively large distances, which your electromagnet probably wouldn't handle. And I am not sure that they are able to work with small objects. \$\endgroup\$
    – Eugene Sh.
    Jan 26, 2015 at 15:33
  • 2
    \$\begingroup\$ An optical distance sensor might work better. And use PWM and a MOSFET to drive the magnet, not an op-amp. \$\endgroup\$
    – tomnexus
    Jan 26, 2015 at 15:49
  • \$\begingroup\$ Thanks for quick answers. Why is a MOSFET a better choice? \$\endgroup\$
    – Torben
    Jan 26, 2015 at 15:59
  • \$\begingroup\$ Less parts, greater control of the current and voltage, wider bandwidth for your PWM \$\endgroup\$
    – Eugene Sh.
    Jan 26, 2015 at 16:02
  • \$\begingroup\$ I'd try the hall sensor, it tells you when the permanent magnets are close to the right position. This two coil set-up will be easier (I think) Coils wound in opposition. youtube.com/watch?v=nZL6mLISxeo \$\endgroup\$ Jan 26, 2015 at 16:05

1 Answer 1


The setup you have in mind seems alright apart from the fact I'm not sure what you want to use the Op Amp for. Perhaps you meant a transistor to decouple the Arduino's output from the 12 Volts supply?

A PID on Arduino will be good enough for the application.

Now, about the usage of the Hall sensor my answer becomes lengthy.

If you are going to use magnets as levitating object then you can indeed implement a Hall sensor instead of an ultrasonic one. But as you point out, how do you know if you are reading the right field? Magnetic fields aren't labelled.

I offer you a (little advanced, but quite cool to play with) way to deal with it: disturbance compensation.

  1. Mount the setup, with coils and a way to control their voltage (you can use a linear potentiometer on Arduino)
  2. Find a position where to put the Hall sensor (place it where it's as sensitive as possible to your magnets' field, and the least sensitive to the coil's field)
  3. Without having the magnet in the setup, map the readings of the Hall sensor to the voltage input to the coil. Now you know exactly what magnetic field the coils generate at each voltage setpoint.
  4. Magnetic fields add up linearly, so you can simply subtract the expected 'disturbance' field from the readings of the Hall sensor, and you have your clean reading

Just pay attention to the fact that instantly the voltage might fluctuate much more than the current. The current is low-passed naturally by the coil. So, alternatively, you might either average the voltage readings for the subtraction, or just assume the voltage to be constant because the current won't change much once the magnets are close to the steady-state position (especially in case your magnetic object and your sensor's position won't change)

At last, about the use of Hall sensor and linearity: a linear model should suffice. But it must be a linearized model around your object's height setpoint. Magnetic field are very non-linear (in your case it changes with 3rd power of distance I believe).

Alternatively, you could map the readings given by the Hall sensor to the magnet's position (having the coil turned off). So you can skip the complex modelling of magnetic fields.

For all mappings (look-up tables) on Arduino you should be able the MultiMap function.

Enjoy the nice choice of setup.

  • \$\begingroup\$ I want to build something like that. But I don't know where to start. \$\endgroup\$ May 28, 2017 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.