Magnetic Levitation

Hello all,

My professor has tasked me with creating a magnetic levitator using an Arduino, an HC-SR04 sensor, a solenoid, and a MOSFET and whatever else we may need. The thinking is to use the HC-SR04 to measure how high the ball bearing that is being levitated is and then from that reading, regulate the MOSFET to control the voltage inputted into the solenoid so that the ball bearing maintains a constant level. I have been able to create code that measures the distances accurately but am totally confused on everything else. Does anyone have any ideas on Arduino code I could use to control the transistor?

Use a PWM output to control the transistor, as done on motor speed control.

Could you elaborate on that a little more @jremington?