# Estimate PWM duty cycle to obtain a given current in a coil driven by a MOSFET?

Hello everyone!

I am trying to control the magnetic field generated by a coil (with the ultimate objective of levitating a magnet).
To do that, I put together a circuit where the Arduino drives an N-channel MOSFET via PWM control, which in turn feeds the coil with some current.

Now, I have been trying to find a relationship between PWM duty cycle and current through the coil (since the magnetic field depends on it), but I'm not getting anywhere and some help would be very appreciated!

My thought process was:

• PWM-driven MOSFET effectively reduces source voltage (7.5 V).
• Steady-state current through coil should be i = V/R, where the resistance of my coil R = 2.4 Ohm.

Thus, simple enough: i_steadystate = Vcc * duty_cycle / R, where Vcc = 7.5 V and duty_cycle = 0...1.

As you can imagine, this didn't work (otherwise I wouldn't be here writing about it)! I'm measuring a voltage definitely lower than what I estimated: for example, for a duty cycle of 20% (digitalWrite(51)), I expected to read 1.5V and instead got 1.1V. If I take the coil out and replace it by a standard resistor instead, I get the right 1.5V on my meter.

So, the coil is effectively reducing my voltage, but I'm not quite sure why and how to take this into account when trying to relate PWM duty cycle and current through the coil.
Any help?

I also tried to run a simulation in Simulink, and I got these plots:

Why is the voltage a triangular wave rather than a square one? And why are the mean voltage and current even higher than before?

Thanks!
Nick

PS little circuit diagram The mosfet has a small capacitor between the gate and source due to the nature of the device. This has to charge and discharge when turning on and off. This may be the cause of the triangular wave.

The voltage reading may because of the type of meter you have. Your meter may be a cheaper type that reads average rather than RMS.

Weedpharma

As you can imagine, this didn’t work (otherwise I wouldn’t be here writing about it)! I’m measuring a voltage definitely lower than what I estimated: for example, for a duty cycle of 20% (digitalWrite(51)), I expected to read 1.5V and instead got 1.1V. If I take the coil out and replace it by a standard resistor instead, I get the right 1.5V on my meter.

You have an RL filter with time constant T = L/R = 0.015 / 2.4 = 6.25 ms
The PWM period = 2 ms

There’s not enough time available for the voltage at the R1/L1 junction to rise 100%.

No, the reason for the voltage being triangular is the resistor - the voltage across the resistor is
proportional to the current.

Lose the resistor, its wasting power, normally the inherent resistance of a wire coil is chosen to
set a desired maximum current. Go to a much higher switching frequency (8 times higher). You'll
get a much smoother current and the system will behave more linearly w.r.t. duty-cycle.

Note that you may have to measure the average current and apply a feedback loop in order to
stabilise things in the real world.

XL = 2π f L

Page 90: RL circuit example
Similar circuit… just use your values. Need to measure DC resistance of coil for R2.

Page 15: Significance of the time constant (T )
Just looking at the first 33% of the first time constant … it looks linear (not much if a curve).

Your existing circuit will need 31 ms for 5T.

Just to test your existing circuit, if you create a 10Hz squarewave signal to replace your PWM, the circuit will have time to completely charge/discharge:

``````digitalWrite(pin, HIGH);
delay(50);
digitalWrite(pin, LOW);
delay(50);
``````

I agree with MarkT on all points made.

MarkT:
No, the reason for the voltage being triangular is the resistor - the voltage across the resistor is
proportional to the current.

Lose the resistor, its wasting power, normally the inherent resistance of a wire coil is chosen to
set a desired maximum current. Go to a much higher switching frequency (8 times higher). You'll
get a much smoother current and the system will behave more linearly w.r.t. duty-cycle.

Note that you may have to measure the average current and apply a feedback loop in order to
stabilise things in the real world.

It may be difficult to lose the resistor as it is the equivalent series resistance of the winding.

Weedpharma

Hi all, thank you very much for the feedback!

weedpharma:
The mosfet has a small capacitor between the gate and source due to the nature of the device. This has to charge and discharge when turning on and off. This may be the cause of the triangular wave.

The plots I have shown come from a Simulink model that doesn't model a full MOSFET (I am modelling it as a switch that I turn on and off with a pulse generator). This means that it can't be due to the capacitor.

weedpharma:
The voltage reading may because of the type of meter you have. Your meter may be a cheaper type that reads average rather than RMS.

Shouldn't average voltage be what I need, since it's a DC circuit?

MarkT:
No, the reason for the voltage being triangular is the resistor - the voltage across the resistor is
proportional to the current.

The resistor I indicated in the diagram, as weedpharma rightly pointed out, represents the inherent resistance of the coil, so I can't take that out!

MarkT:
Go to a much higher switching frequency (8 times higher). You'll
get a much smoother current and the system will behave more linearly w.r.t. duty-cycle.

Right, so right now I am using an Arduino Uno with Timer 2 set to a pre-scaling factor of 8 in PWM Phase Correct mode, giving me 16 MHz / 255 / 2 / 8 = 3.921 kHz. Do you reckon I should go even higher?

Thanks
Nick

It is a very long time since I had to work out the difference between RMS and average so please don't take this as exact.

Look at your voltage graph. There is a section of the waveform under the zero reference. This will reduce the "average" in the same way as the average of a sine wave is zero volts. This will reduce the reading of the area above the reference that you are interested in with a meter that reads average.

The negative is due to back EMF of the coil and is limited to about 0.6v by the diode. When you substitute a resistor, there is no back EMF therefore nothing below the zero and your meter reads higher.

Weedpharma

Looks like for your project, the objective is to create zero magnetic field.
http://amasci.com/maglev/magschem.html

weedpharma:
It may be difficult to lose the resistor as it is the equivalent series resistance of the winding.

Weedpharma

In that case don't confuse yourself by looking at the waveform across the pure inductance
only, since that's not a physically possible measurement. The voltage across the real,
imperfect inductor should be rectangular.

A higher frequency should bring the current ripple down so that its not dominating the
situation - a small triangular wave atop an average DC level is what you should see.

As I am not an electronics engineer, I ask (for my education), what tells us that the wave shown above is not across the real component?

Weedpharma