Thursday, 20 June 2019

How to Drive a Solenoid from a Microcontroller

I haven't posted anything in a long time...I just haven't been motivated enough to do so.  I suppose it could be because I'm busy but its more likely I've just lacked the energy to do anything.

I recently designed a simple FET driver circuit for energising a solenoid.  It wasn't particularly special in my opinion but I thought it might be useful to share the experience so here goes:

I had to drive a solenoid based circuit for an electronic lock from a microcontroller.  As a solenoid is essentially an inductive load I would need a transistor based circuit to control the supply voltage from a 3.3 Vdc signal generated by an STM32 microcontroller.  In order to actuate the solenoid a short pulse (less than a second) was required from a 12 Vdc or 24 Vdc supply.

A solenoid is an electro-magnetic actuator which creates a concentrated magnetic field.  Here are a couple of links about solenoids and how they work:

A simple switching circuit is all that is required to energise the solenoid coil and actuate it.  Electronic switching can be performed in many ways - a relay switch in itself is a form of electro-magnetic actuator.  In my case I decided to use a Field Effect Transistor or FET.  I've blogged about field effect transistors in the past:

The same principles apply.  Recently I read an excellent blog post by James Lewis - 'The Bald Engineer' on some of the misconceptions about using field effect transistors...I recommend looking at James's website.  He writes some excellent articles and makes some great video content on electronics engineering.

As I had to drive the field effect transistor directly from a microcontroller pin at 3.3 Vdc I needed what is known as a logic level FET - That is a device that will allow current to flow between the drain and the source pins when the gate pin is supplied with logic level voltages, logic level voltages are normally classed as 1.8 Vdc, 3.3 Vdc and 5 Vdc.  In the case for the microcontroller I am using - an STM32F439 a logic 1 (high) is 3.3 Vdc.  Therefore I needed a FET which had a VGS on threshold that was at or below 3.3 Vdc.  I chose to use the IRLZ44 as I had some of those components and I also had a spice model for them so I can simulate the circuit.

From some brief measurements of my solenoid I found that it had a series resistance of about 33 ohms and I guessed it's inductance at 3 Henrys....I didn't have my LCR meter to hand to measure it....I might dig that out later to see what the inductance actually is.

Here is the datasheet for the IRLZ44:

I'm using the T0220 version for my circuit:

Image result for IRLz44

The critical parameters I was interested in are:

VDSS = 55 Vdc - The maximum voltage allowed on the drain pin with respect to the source pin.

RDS(on) = 0.022 Ω - The resistance of the drain with respect to the source pin when the transistor is on.

ID = 47 Amps - The maximum current allowed to flow from the drain to the source.

VGS On Threshold = 2.0 Vdc - The voltage at which the FET turns OFF - Thanks James, I always wrong about this too!  This means in order to turn the FET on (cause conduction between the drain and source pins we need to surpass this this voltage at the gate pin.  If the voltage at the gate pin drops below this voltage the transistor will stop conducting from the drain pin to the source pin.  The parameter name is a little confusing but hey ho...

So what does this all mean.  It means that I can turn the transistor on using an output pin from a
3.3 Vdc microcontroller and that will allow conduction through the drain pin to the source pin which will allow me to control the voltage supplied to the solenoid.

Here is the circuit:

Simple Solenoid Driver Using a Logic Level FET
Here is the circuit's operation.  A 3.3 Vdc pulse will be supplied from the microcontroller.  This pulse is applied to the gate of the IRLZ44 FET through a 270 Ω resistor (R2).  The resistor R2 is present to prevent a large in-rush current being presented to the gate pin of the FET.  It isn't always necessary if one is only applying long slow voltage pulses to the gate.  I always add a resistor as I like to be certain there will not be any in rush current issues.  The 10 kΩ resistor (R1) is a pull down resistor.  It ensures that the gate pin of the IRLZ44 when not driven from the microcontroller is in a known state - low.  It prevents the FET from spuriously switching on or off.  The drain of the FET is connected to the solenoid (modelled in this case by the 33 Ω resistor and the 3 H inductor.  The schottkey diode (D1) is present to prevent the back-emf generated by the inductive load switching on and off damaging the FET.  D1 is sometimes called a 'free wheel diode' or the 'flyback' diode....More on this later.
The voltage source VCC and switch S1 are not really present but a way of simulating what the output from the microcontroller will be. If S1 is closed for a short period the signal presented to the gate pin will be similar to the output pulse from the microcontroller.  This pulse voltage will cause the FET to become active allowing current to flow from the drain to the source which in turn will allow voltage to flow through the solenoid causing it to actuate (create a magnetic field which pulls the pin into the coil).

If we were to simulate the operation of the circuit and observe the input and the output with an oscilloscope connected as shown above this is what would be seen:

The red trace is the the solenoid operation and the blue trace is the input from the microcontroller - modelled in this case by the switch S1 and the 3.3 Vdc voltage supply.

So our circuit simulates will and will work.  The FET operates properly from a 3.3 Vdc supply and can go active within a milli-second.  More than adequate for our purpose!

The only thing left I wanted to discuss was the flyback diode's needed as when an inductive load has voltage and current applied it converts the electrical energy into magnetic energy creating a concentrated magnetic field.  When an inductive load is has the voltage and current removed the magnetic field collapses and any left over energy is passed back to the drive circuit as a large voltage spike.  This spike is often well over 100 Volts for a short period - more than enough to damage the Field Effect Transistor.  Therefore the flyback diode is applied to prevent this damaging voltage spike passing to the FET.

To illustrate the point here is a circuit with a switch added to the flyback diode.  As the switch is open the diode is not connected.  When the magnetic field collapses this is what is passed back to the FET:

Here is the oscilloscope trace:

If the flyback diode was not present in the circuit the FET would fail fairly quickly...

I'll add some photos of the circuit I built shortly to show it in operation.  It works perfectly for my application.  Take care people - Langster!

Here is a quick video of the circuit on a bread board.  It was driving an electronic lock using 5 Vdc as the gate voltage: