I have to control an electromagnet with a current from 0-2 A and I'm stuck in figuring out the right way to do it. I want to use the output of an Arduino (0-5 V). The coils of the magnet have a resistance R1 of 1.25 Ω, so for maximum current a voltage of 2.5 V is needed.

The problem is that the used MOSFET only activates at around 2.5 V and the current in R1 quickly rises nonlinear afterwards to its maximum of 2 A. So the "control" area is less than 1 V and nonlinear. Can somebody help me out with what I'm doing wrong? Do I need to use other components, or maybe a PWM-signal?

enter image description here


2 Answers 2


Instead of driving the MOSFET with a variable voltage, drive it with PWM. Change the solenoid current by varying the duty cycle. Use a 20 kHz frequency so that there won't be audible noise. The inductance of the solenoid will average the current, so you don't need a low pass filter. You do need a diode across the solenoid, though.

  • 0 % duty cycle (fully off) = 0 A
  • 25 % duty cycle = 0.5 A
  • 50 % duty cycle = 1 A
  • 75 % duty cycle = 1.5 A
  • 100 % duty cycle (fully on) = 2 A

PWM control


The usual "linear" way to regulate current in some load is in a closed loop, where negative feedback to an op-amp is perpetually monitoring and adjusting the current through the load:


simulate this circuit – Schematic created using CircuitLab

While this works, it's really inefficient. At full current (2A), the MOSFET is dissipating 5W of power, and will need serious cooling, with a big heat-sink and perhaps even a fan.

The best way to go, in my opinion, is going to be some kind of PWM, which Davide Andrea described in his answer. With PWM, the transistor is always switched completely on or off, and dissipates power only during transitions between those states. Without a control loop to measure current, and adjust the PWM output accordingly, control is hit-and-miss. You rely only on "such-and-such duty cycle corresponds roughly to such-and-such average inductor current", but maybe that's all you need.

However, there's no reason why you can't somehow measure coil current with the ADC on the microcontroller (which I do with resistor Rs in the circuit below), and implement feedback and a control loop in software.

Here's a PWM solution that uses a comparator with hysteresis to keep coil L1 current very close to the set-point determined by the potential at IN:


simulate this circuit

It's really just a relaxation oscillator, using the inductor (your coil) as the timing element, rather than the capacitor.

When the transistor is on, the coil is energised, and the current through it rises slowly. That current is sensed by Rs. When the voltage across Rs reaches some threshold, as detected by comparator U1, the transistor switches off. Coil current continues to flow, this time drawn via D1, and still passing via Rs, and the voltage across Rs falls. The comparator's threshold is slightly lower during this phase, due to positive feedback via R4 to implement hysteresis. When the voltage across Rs falls below this lower threshold, the transistor turns back on, and the process repeats.

The result is a current through the coil which oscillates about some mean value, set by applying a DC potential at IN, between 0V and 5V, corresponding to 0A and 2A respectively.

The fuse is there to prevent Rs from exploding if ever the transistor is permanently switched on, either through some circuit failure or failure of the transistor itself. Rs will dissipate nearly 1W at maximum current, so make it beefy.

Oscillation frequency will depend entirely on the inductance of the coil, so if you build this, you may need to experiment a bit, to make it operate at an appropriate frequency.

I've never built or tested this, so I can't vouch for it at all, but it's worth a try.

Here's the voltage at S, and the current through the coil, when the \$V_{IN}=3V\$:

enter image description here enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.