# Controlling an Electromagnet using a microcontroller

My question stems from this one here: Controlling an electromagnet strengh with arduino

Background

I am working on the classic magnetic levitation control systems problem. I have a 10N electromagnet (150 mH, 40 ohms) that will be used to levitate a steel ball. I will be using an optical time of flight sensor to determine the balls position. I plan on going above and beyond the typical undergrad magnetic levitation problem by allowing the set point (ball height) to be changed on the fly, similar to this video.

I took non-linear control systems course in grad school so I am aware this is a difficult problem. We never went beyond the theory though, so I thought this would be a fun problem to extend my learning.

The Problem

The control system I derived expects that the input variable (the voltage applied to the electromagnet) is a continuously varying DC voltage. I've been looking at using a DAC, but they are relatively expensive and don't seem to be designed for power applications. PWM seems like the way to go, but I am struggling finding a way to model the PWM system; a square wave is not mathematically friendly.

The Question

What is the best solution for the problem: using something like a DAC or PWM? I need to create a fast-acting (< 1ms delay), accurate control signal.

If PWM is the best solution, how would I go about modeling it in a control system?

• I'd use a PWM, but the fact that it's a varying duty-cycle square wave shouldn't be a problem because it's the duty-cycle that you're controlling. The micro's hardware does the timing of turning the pin on & off appropriately for you. – brhans Sep 14 '16 at 21:27
• @brhans I do not have an issue with the implementation of the PWM, rather modeling how a PWM effects the dynamics of the system. I can easily model the behavior in a circuit simulation, but I need some model or approach to design the control system appropriately. – dgreenheck Sep 14 '16 at 21:46
• The PWM frequency should be orders of magnitude above the mechanical response time - so 0-100% duty-cycle of the PWM simply corresponds to 0-100% voltage delivered to your electromagnet... I wouldn't even try to model the details of the PWM itself as part of the control system. – brhans Sep 14 '16 at 22:02
• Supporting existing comments: (1) PWM at much higher than critical rate then filter to DC. This can drive an eg FET driver. OR (2) Send unfiltered PWM to FET and drive inductor with recirculation diode for when FET is off. This now becomes a buck converter. Changing PWM % changes inductor current. You can add a sense resistor and measure current. With due design the sense resistor can have one end grounded allowing ease of I measurement with mcircontroller's ADC. – Russell McMahon Sep 15 '16 at 2:10
• I changed A-----o in your title to "Microcontroller" as this problem is a generic electronic one and specific uC is not overly relevant , and as a small % of people here turn blue, froth at mouth, howl at moon and throw their toys out of their cots when A------s are mentioned it seemed seemly to spare them the anguish, should it be possible. – Russell McMahon Sep 15 '16 at 2:13

Rather than needing to model PWM effects on levitation you can run the PWM at much higher than critical rate then either

• Filter to DC.
This can drive an eg FET driver. OR

• Send unfiltered PWM to FET and drive inductor with recirculation diode for when FET is off.
This now becomes a buck converter. Changing PWM % changes inductor current.
You can add a sense resistor and measure current.
With due design the sense resistor can have one end grounded allowing ease of I measurement with microcontroller's ADC.

eg M1 is a P Channel high side MOSFET. If inductor supply is the same as microcontroller supply (which is not usually the case the gate can be driven at microcontroller voltage levels. If eg mc has 5V supply and inductor has say 12V supply a level-shifting driver is needed. Details on how this can be achieved can be discussed if this is the path taken. D1 allows current "recirculation" when MOSFET is off.
R1 allows current sensing.
Depending on inductor resistance it may be desirable to provide additional loss in the recirculating path to allow faster field decay to improve control response times. Or not.

simulate this circuit – Schematic created using CircuitLab

_______________________________

I see lots of people suggest using a TIP120 for driving a high current load from a uC. But it seems a FET is a much better choice since it allows faster switching and is less power hungry.

TIP120 is less bad than some - 5A 60V Darlington. Min current gain is 1000
BUT C-E saturation at 3A is 2V max @ Ib=12 mA,
and 4V at 5A at Ib = 20 mA.
At 3A that's equivalent to on resistance of R = V/I = 2/3 = 666 milliOhm or
at 5A = 4V / 5A = 800 milliOhm.
Heat = 6W and 20W. FETs with much much much lower Rdson are cheap and available.

Also 12 mA drive is liable to be near upper limit for some uCs and 20 mA worse.
FET drive needs are low at low speeds and if higher speeds needed a 2 transistor gate driver (NO other parts) can be used.

PWM with recirc diode as in other answer looks good.

An N Channel FET is equally good from a power switching point of view.
N Channel speed is no problem - required switching speeds will be well below what can be accomplished with typical devices.

The above P Channel method puts the "switch" high and allows the sense resistor to easily have one side grounded. This makes it simple to use an ADC to read V_Rsense in microcontrollers with ground referenced ADC's and keeps Rsense under Vdd_mc.

An N Channel switch on the "low" side slightly simplifies FET drive but makes Rsense voltage slightly harder to read as Rsense is then usually either floating or attached to V+_solenoid. Usually you then either use a current mirror to "reflect " Isensed to a ground reference point or use a differential amplifier to do the same.
Rsense is then usually at DC level of > Vdd_mc if Vinductor >> Vdd_mc as is often the case.

• Could you please explain why you chose to use a P-channel instead of N-channel MOSFET? Wouldn't using an N-channel eliminate the need for the level-shifter (at the expense of switching speed)? – dgreenheck Sep 15 '16 at 16:12
• @dgreenheck NChannel speed is no problem. P Channel puts switch high and allows sense resistor to easily have one side grounded. This makes it simple to use ADC in microcontroller with ground referenced ADC's and keeps Rsense under Vdd_mc. | N Channel slightly simplifies FET drive but makes Rsense voltage slightly harder to read. Usually you then either use a current mirror to "reflect " Isensed to ground reference or a differential amplifier. Rsense is then usually at DC level of > Vdd_mc if Vinductor >> Vdd_mc as is often the case. – Russell McMahon Sep 15 '16 at 22:10

One point of PWM is that the individual pulses are so short that the thing being driven only sees the average value. This certainly applies in your case too. The individual PWM pulses are irrelevant, only the duty cycle (fraction of on time to total pulse period) matters.

This concept is used a great deal in electronics, so just about every microcontroller out there comes with some hardware that can generate PWM. You don't say how fast your control loop will be, so I'll pic 1 kHz, meaning a control period of 1 ms. That's a "long" time for a modern micro.

Let's say you run the PWM at 25 kHz. That gives you 25 pulses per control iteration, so you should be able to see that individual pulses are invisible. The system will be reacting to the average duty cycle over 10s of pulses at least. Let's say you want at least 10 bit drive level resolution. That means you need a bit over 25 MHz clock into the PWM generator. That's readily available in many micros. There are some dsPICs, for example, that have a PLL built in to effectively clock a special PWM generator at nearly 1 GHz. 12 bit or more PWM resolution is available with a little digging.

The circuit is very simple:

L1 is the electromagnet, which looks to the circuit like a big fat inductor. If the power voltage and the maximum magnet current is low enough, then you can use a FET that can be driven directly from a digital output. Otherwise, use a FET driver (not shown) between the digital output and the FET gate. This will require a separate 12 V or so supply.

The diode is important and non-optional. For the best efficiency and the smoothest current, and fast reverse recovery, it should be a Schottky. It not only keeps the current circulating thru the magnet during the PWM off phase, but it also gives the inductive kickback current a place to go and keeps Q1 from getting fried when getting switched off.

Another point to consider is the non-linear response from PWM duty cycle to steel ball position. Duty cycle to magnet current will be reasonably linear, but magnet current to position will not be. This makes it impossible to tune the control loop to the right response over a range of positions since the same variation in PWM duty cycle at one end of the range produces a much smaller response than at the other end.

A good solution I've used for similar problems is to use a lookup table to roughly linearize the "plant" as seen by the control loop. Measure a bunch of points so that you can relate open loop PWM duty cycle to ball position. The control loop then adjusts the desired ball position, which gets looked up in the table to find the PWM duty cycle that will be written to the hardware that iteration. Piecewise linearly interpolate the table between points. This is a trivial operation compared to the floating point control calculations, which should still be possible in well under 1 ms.

You should assume that a PWM controller adds a time delay to your control loop. Most PWMs are a fixed-frequency, variable duty cycle type. In this case, the pulse width is affected only after the on-going cycle. For example, a PWM period of 100 microseconds will introduce a 100 uS delay to your control loop. Since delays are often deadly to control-loop dynamics, a high PWM frequency is desirable. A fixed pulse-width, variable frequency PWM method can potentially eliminate this delay, but this PWM method is less common.

With PWM, a fixed-voltage supply is used, rather than the variable applied analog voltage in your proposed solution. A fixed-voltage PWM power source puts an upper limit on the electromagnet's rate-of-change of current because of its inductance (Vsupply = L di/dt). A higher supply voltage to the PWM switcher allows you to change electromagnet current more rapidly. This limit applies to any PWM method.

• Thanks for this response. So to account for this during design, I would look at the phase margin of my control system to see if it could tolerate the 100 us delay? Do you have a reference you could provide in regards to this? – dgreenheck Sep 15 '16 at 16:07
• @dgreenheck - I am familiar with Microchip's PWM implementation. In their 16F684 microcontroller data sheet (document DS41202F), section 11.3.2 PWM DUTY CYCLE states that PWM period is updated only after the current cycle completes. I think this delay would be a worst-case assumption for any fixed-frequency PWM implementation. – glen_geek Sep 15 '16 at 17:04