Help with creating simple levitating object

Hi I'm trying a create a project where I levitate an object by electromagnet that attached permanently above it.

The levitating object connected to permanent magnet.

I want to measure the distance between my electromagnet and the levitating object by measuring the magnetic field using linear hall sensor (which is close to the electromagnet).

Then to use this measurement to increase or decrease the power of the electromagnet to pull or release the levitating object.

here is a similar (or same) project.

  1. What I don't understand is how can I measure this magnetic field accurately. The field changed not only by the distance of my levitating object, but also from the electromagnet itself.

  2. Another problem I have is when I PWM my electromagnet, it starting to make a buzzing sound.

Any suggestion will be appritated

Google showed : 2,870,000 results (0.74 seconds) , when searching for measuring magnet fields. What did you find?

Paul

Measure the height or distance from the magnet to the object and adjust the current to the magnet accordingly.