How to Interface a Microcontroller with a Relay Using a MOSFET

A microcontroller is a low-power logic device. By itself, it is generally incapable of directly driving a relay. A single N-channel MOSFET may be interposed between the microcontroller and relay to increase the available coil current and accommodate relays with higher coil voltages.

In this article, we explore a simple MOSFET-based interface allowing an Arduino Nano Every to control a 12 VDC relay. The design, as shown in Figure 1, also features the CIT J104D2C12VDC.20S relay and the DMN67D8L-7 MOSFET MOSFET.

Note that this article is the high-level introduction to several related articles including:

The related prototyping article describes how to mount the small SOT23-packaged surface mount MOSFETs to the adapter board as shown in Figure 1. The second article expands the topic to encompass energy saving techniques.

Figure 1: Picture of the Arduino Nano Every driving a pair of relays using surface mount MOSFETs. The Digilent Analog Discovery is shown in the background.

What are the microcontroller limitations?

A microcontroller is limited in terms of voltage and current. The featured Arduino Nano Every is a 5 VDC device with a single-pin current capacity of 15 mA as described in the technical specifications.

Recall that a MOSFET is a voltage driven device that requires zero current once the gate capacitance has been charged. Consequently, the voltage is the limitation for our low-frequency relay driving circuitry.

Tech Tip: The Arduino Nano Every is a 5 VDC microcontroller. With respect to driving a MOSFET, this 5 VDC is preferred over related 3.3 VDC microcontrollers. The 5 VDC MOSFET gate drive signal is high enough to drive many MOSFETS into a full-on condition while 3.3 VDC may be insufficient. Careful study of the MOSFET datasheet is required to determine if the 3.3 VDC signal is appropriate. An implication of this tip is that BJT transistors with their 0.7 VDC nominal gate voltage may be more appropriate for the lower voltage microcontrollers.

What are the MOSFET limitations?

For our purposes, there are several key MOSFET characteristics that must be addressed including:

  • on-resistance as a function of gate voltage

  • steady state current

  • maximum voltage of the drain relative to source

  • power dissipation

These parameters must be addressed from a system perspective where we consider the microcontroller’s drive signal as well as the voltage / current requirements of the relay.

Relay requirements

The chosen CIT relay is a Double Pole Double Throw (DPDT) relay with a 12 VDC 200 mW coil. The coil has an approximate resistance of 720 \Omega. When operated with a 12 VDC source, the relay coil will draw approximately 17 mA.

MOSFET suitability

The MOSFET is selected to interface the microcontroller to the relay. Specifically, we need a MOSFET that will accept 5 VDC gate signal with the capability of conducting 17 mA with a source voltage of 12 VDC. For this demonstration, we use the Diodes Incorporated DMN67D8L-7 . This is a small SOT23 packaged device with specifications that exceed our requirements. This MOSFET can switch 230 mA while withstanding a V_{DS} up to 60 volts.

Tech Tip: We need to be careful with MOSFET specifications especially with regards to gate voltage. Recall that a MOSFET is a voltage-controlled device where the electrostatic gate voltage controls the drain to source channel. The magnitude of the gate voltage effectively controls the resistance of this channel with higher gate voltage providing a lower resistance. The 230 mA specification is associated with a 10 VDC V_{GS} voltage. Note that the current drops to 190 mA with a 7 VDC V_{GS} and even further with the 5 VDC V_{GS} in the featured project. While not specified, our 5 VDC operating point is even lower. Consequently, the DMN67D8L-7 is not as overspecified as we may have originally thought when driven by our 5 VDC Arduino.

Review of the MOSFET datasheet

As suggested in the previous Tech Tip, we need to carefully consider the MOSFET operating points especially with regards to V_{GS}. The datasheet curves included in Figure 2 shows that the DMN67D8L-7 MOSFET is acceptable for this application.

  • On the left we see the relationship between I_D and V_{DS} for various V_{GS} levels. The green dot shows that V_{DS} will be very low for our chosen relay current of 17 mA.

  • On the right we see a closely related curve showing the anticipated channel on-resistance as a function of V_{GS}. The Arduino’s 5 VDC is capable of turning on the MOSFET with a channel resistance of approximately 1.8 \Omega. Unfortunately, this operating point is uncomfortably close to the knee of the curve. However, this should be acceptable as the MOSFET channel resistance is two orders of magnitude less than the 720 \Omega relay coil resistance.

We should note that natural variation in power supply voltage and component tolerance could cause an increase in channel resistance. Also note that this design would NOT be viable if a 3.3 VDC microcontroller were used. The resistance would be too high resulting in MOSFET self-heating and even failure to activate the relay.

Figure 2: Operating curves for the DMN67D8L-7 MOSFET showing the anticipated operating conditions. Note that a 5 VDC V_{GS} is sufficient while a 3.3 VDC gate drive would have an unacceptably high resistance.

Schematic

A representative schematic for the MOSFET relay interface is included as Figure 3. The output of the Arduino is represented as a SPDT switch that connects an I/O pin to either ground or to 5 VDC. A current limiting resistor is placed between the Arduino and the MOSFET. Finally, the relay coil is connected between the 12 VDC source and the MOSFET drain. Note that the 5 VDC Arduino and the 12 VDC supply share a common ground.

The 100 kΩ resistor on the output pin of the Arduino is a subtle protection device. Normally, it serves no purpose as the Arduino output pin is digitally connected to either the 5 VDC rail or to ground. However, there are times when the Arduino’s pin may be set to high impedance such as upon device startup. The 100 kΩ resistor performs a pull-down operation. It ensures that the MOSFET will be turned off unless specifically commanded by the Arduino. Note that the resistor is placed on the left side of the 1 kΩ resistor. This is necessary to maximize the gate voltage as a voltage divider would be formed if the 100 kΩ were on the right-hand side of the 1 kΩ resistor.

A common type 1N4148 small signal diode is used as the flyback diode to “catch” the coil voltage when the MOSFET is turned off. At first glance the 1N4148 diode may seem too small for this application. However, the 200 mA current rating is a good match when we consider the relay and MOSFET specifications.

Tech Tip: The importance of the flyback diode to prevent high-voltage spikes cannot be overemphasized. Without this diode the relay’s inductive kick (flyback voltage) will reach several hundred volts as the relay is turned off. This will destroy the MOSFET. I can tell you this from painful experience. I once forgot the diode and had a very embarrassing public failure as the relay cycled twice and then failed in the on position. Click on, click off, click on, and then a fail - permanently on.

Figure 3: Representative schematic for the Arduino microcontroller, MOSFET, and relay. The schematic was built using MultisimLive.

How fast does the relay operate?

The electromechanical relay is a slow device. Many people believe this speed is a function of the mechanical components moving. While this is true, the inductive nature of the coil causes a greater delay. Recall that the time constant of a resistive inductive (RL) circuit is defined as:

\tau = \dfrac{L}{R}

Here R is the coil resistance and L is the coil inductance. For the chosen relay, the resistance is approximately 720 \Omega and the inductance is in the 2 H neighborhood. The resulting time constant is about 3 ms. This is supported by the current waveform (blue) shown in Figure 4. Recall that the current will reach about 64% of final in one time constant.

The actual steady state current as well as the concept of a relay coil time constant is complicated by the motion of the armature. Initially, the magnetic field loops pass through the air gap of the armature. Later as the armature closes, the air gap is eliminated causing a change in apparent coil inductance. The result is the observed dip in the relay coil’s current as seen in Figure 4.

For more information about the behavior a relay’s magnetic field and time for relay closure, please review:

How do we select the MOSFET gate resistance?

Now that we understand the nature of the relay coil, we can select an appropriate resistor for the MOSFET gate. Since we are dealing with a RL time constant measured in milliseconds the value of the resistor is not particularly important.

In a high speed MOSFET based circuit we would concentrate on the quickly charging and discharging the MOSFET gate capacitance. In a high-speed circuit we may select resistors in the 100 \Omega range. This will provide maximum efficiency by quickly transitioning the MOSFET out of the linear operating range thereby providing a crisp on and off signal. The challenging balance is to keep the resistance low for fast switching speed without stressing the microcontroller logic driving the MOSFET gate capacitance.

In the slow circuit featured in this article, the current is dominated by the relay inductance. Nearly any MOSFET gate resistor from 1k to 100 k\Omega range would be significantly faster than the change in drain current. For example, the datasheet presents the MOSFET input capacitance as 22 pF. With a 100 k$\Omega$ gate resistor the calculated time constant is about 3 uS which is 1000 time faster than the coil based RL time constant.

Tech Tip: Seemingly simple circuits are prone to oscillation. The relay drive circuit is no exception. The resulting high-frequency oscillation can cause interference and in extreme situations destroy the MOSFET. Proper placement of the gate resistor is one way to mitigate against this oscillation. Be sure to place the resistor as physically close to the MOSFET as possible. We see this in Figure 1 where the resistor placed as close as possible to the MOSFET gate.

Figure 4: Turn on command (orange) and the resulting relay current waveform (blue). The dip is caused by the changing magnetic properties and the armature closes thereby eliminating the air gap.

Advanced energy saving operation

Now that we have a functional MOSFET based microcontroller to relay interface we can explore advanced operation. We can use a Pulse Width Modulator (PWM) driver to provide energy savings. To keep this article short, the PWM material has been posted here:

Parting thoughts

The MOSFET is a natural match for a 5 VDC microcontroller. It allows the microcontroller to drive devices with higher current demands and voltages outside the microcontrollers 5 VDC rail.

Throughout this article we painted a clear line between the 5 VDC and the 3.3 VDC microcontrollers. This included a caution that MOSFETS such as the featured DMN67D8L-7 are not a good match for 3.3 VDC microcontrollers. Perhaps in the future we will explore MOSFETS with low V_{GS} capabilities. Some of which provide operations down to 1.5 VDC or even 1.2 VDC.

Best Wishes,

APDahlen

Please follow this link for related Arduino education content.

About this author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (partially interwoven with military experience). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing educational articles about electronics and automation.

Highlighted Experience

Dahlen has authored over one hundred educational DigiKey TechForum articles simplify complex concepts for a wide audience. Explore his Arduino microcontroller articles on this Arduino Microcontroller Index page.

Questions

  1. Why do the Arduino microcontroller and 12 VDC power supply share a common ground? Hint: What are the requirements for KVL?

  2. Contrast and compare the presented solution against a solid-state relay.

  3. Search the web to determine the precautions for working with MOSFETs.

  4. Describe inductive kick (flyback voltage) including its origin, potential problem, and mitigation.

  5. Locate the datasheet for the first microcontroller you encounter when searching on the key words “news release microcontroller.” Would the circuit presented in Figure 3 operate with this new microcontroller?

  6. The DMN67D8L-7 MOSFET is rated for 230 mA. Why is this metric inappropriate for the featured application? Hint: Could our Figure 3 relay with its 17 mA coil be replaced with a 200 mA relay?

  7. Calculate the MOSFET power dissipation using 17 mA and the MOSFET on resistance shown in Figure 2. Is this within the capabilities of the DMN67D8L-7 MOSFET. Hint: Did you account for elevated temperatures such as 150 degrees Celcius?

  8. Describe the magnetic air gap in a relay as described in this forum post.

  9. Modify the circuit in Figure 3 to operate at L/2R speed as described in this forum post.

  10. Locate a MOSFET suitable for a 3.3 VDC microcontroller.

Critical thinking questions

  1. What causes the dip in the relay’s current curve as shown in Figure 4?

  2. The dynamic operation of the MOSFET based relay driver is governed by several time constants. Identify the component(s) and subcomponents associated with the delays and provide an estimate for the time constants.

  3. Redesign the circuit to eliminate the galvanic connection between the microcontroller and the relay circuit. Hint: What are the components of an SSR?

  4. What advantage does a MOSFET have over a BJT?

  5. Describe how to detect high frequency oscillation in a relay driver circuit.

2 Likes

This was a very detailed and well written guide, thank you very much.

One thing confused me though - you refer to a “12V source” at a couple points. It took me a while to realise you were referring to a 12V voltage source supply, and not the MOSFET source terminal. Is that correct?

Good catch, @paulpickering678.

In this particular example, the relay (12 VDC coil) is on the high side of the MOSFET. A 12 VDC power supply (“source”) is used for the relay. The gate of the MOSFET is controlled directly form the 5 VDC microcontroller.

As for the MOSFET drain terminal, we will measure 12 VDC when it is turned off and approximately 0 VDC when it is turned on.

The MOSFET “source” terminal and DC power supply (negative terminal) share the same ground.

Sincerely,

Aaron