Author Options:

Can pulse width modulation pins on arduino noticeably affect the strength of an electromagnet? Answered

I am currently designing a project in which two electromagnets cause a small metal object to levitate between them, and am trying to create an interactive interface in which gestures on a multi-touch trackpad cause the object to levitate higher or lower. My question is, is it possible to use an arduino's pulse width modulation to significantly alter the strength of an electromagnet in order to move an object at a noticeably varying distance from the electromagnet?



Best Answer 5 years ago

The arduino pins only safely source/sink 40 milliamps or so which is probably insufficient current for your electromagnets. It is very easy to add a transistor and a 1kOhm resistor to beef up the current capabilities of the Arduino.

By the way, if you want speed (I assume you do), you may have to not use the PWM pins and directly set the pins high and low in a tight loop, and do not use digitalWrite but directly write to the pins.

Best Wishes

Thanks for mentioning that, I was unaware of the exact current limitations of the arduino pins; 40 milliamps doesn't seem enough to move anything at an appreciable rate for a levitation project meant as a display, if it could even manage to power an electromagnet enough to turn on in the first place. Transistors/resistors will definitely need to be used for that.

Again, thanks. I didn't even consider rapid high/low switching. Strange, as I was the one to come up with that as a PWM substitute in my arduino class... Doing that and then having user input determine the speed at which it switches should work well. What do you mean by "do not use digitalWrite but directly write to the pins"? Sorry if this is an incredibly basic topic that I should know already, but I was unaware that there was another way to write to the pins other than digital/analogWrite.

Here is the port manipulation link which describes the PROs and the CONs and the How-to: http://www.arduino.cc/en/Reference/PortManipulation

I realize now I didn't explain that part very well. The 'digitalWrite' function call is implemented by a routine which does quite a bit of stuff under the hood, like making sure the pin is an output pin, etc. This makes it 'idiot proof' but makes it a rather slow function. The arduino pins can be set at a lower level by writing to a predefined memory location. (Sorry, I can't recall the pin names at this moment.) If you directly set or clear the pins you can change their state MUCH faster than by using the digitalWrite call. Same with reading the position of the object you are levitating (I don't know how you plan to do this, though).

If you are doing this 'very seriously' then you'd probably need to take into account control theory and analyze your system and develop root locus and bode plots, etc. etc. which are engineering terms which say 'under what conditions is this system stable?'. Since there is some lag between when you sense the position of the object being levitated and when you change the magnetic field to compensate to keep the object afloat, there can be situations where you change the field and overshoot a little and cause oscillations where your field changes actually amplify the oscillation (i.e. it becomes unstable) and you end up shooting the object into the air like a bullet. :-) Do take videos of your attempts! Changing the mass of the object and the field strength and the lag can all affect the system stability points. Makes for a very fun project!

Best Wishes,


5 years ago

Why would you need two electromagnets ?

Let earth gravity work for you ( at least it is linear )



I considered it, but figured that having two electromagnets or at least one electromagnet and one permanent magnet would allow more stability-- the pull from both sides would help to prevent the object from being knocked out of balance and falling out of the side of the system if the system is bumped slightly. I may do just one to lower the cost and because it'd be simpler than balancing two magnets, but I'm not sure yet.

also, remember that electromagnetic field strength diminishes VERY rapidly ( I seem to recall that it is to the 3rd or 4th power. This might make the control of the levitation a tricky business.

Inverse square law...

If you half the distance,  the "Pull"  increases  by a factor of four..


I would think that the mass of the object would tend to eliminate any need for rapid changes to the current supplied to the electromagnet. . You probably could get away with PWM and a transistor/resistor. At least try it before abandoning it.

Since you're doing dynamic balancing to keep the object levitated (you can't do static levitation), you don't need a large change. Even a relatively small change in one of the currents will shift the equilibrium point enough to notice.