3
\$\begingroup\$

How to protect an ATTiny MCU from damage when using a PWM pin to control a MOSFET or a transistor?

I want to connect a PWM pin from an ATTiny MCU to an N-Channel MOSFET in order to fade some LEDs or switch the speed of a motor. Three wires are connected to my board: VIN, GND, and OUTPUT.

Questions:

  1. How can I protect the MOSFET, MCU, and LEDs from short circuit and voltage overload? Ideally, I want to minimize the damage if I accidentally connect any of the wires (VIN, GND, OUTPUT) to each other or to a voltage or current higher than the maximum rating of my components.

  2. Do I need to put a pull-down resistor on the PWM pin? I want to make sure that the MOSFET stays off when the MCU is not connected or powered off, but I am not sure if the PWM pin can float to an undefined voltage level in that case.

\$\endgroup\$
2

1 Answer 1

5
\$\begingroup\$

In this answer, I will explain how to protect your MCU from damage when using a PWM pin to control a MOSFET or a transistor. I will also cover some additional considerations for different types of devices.

Use a resistor between the PWM pin and the Gate

To protect your MCU from excessive current draw or voltage spikes, it's a good idea to use a 33 ohm resistor between your PWM pin and the Gate of the MOSFET or transistor. This resistor will limit the current that flows into the Gate when the PWM pin switches from LOW to HIGH, and also prevent any voltage spikes from the Gate from reaching the MCU.

When you switch the PWM pin from LOW to HIGH, a large current flows into the Gate of the MOSFET or transistor. This can damage your MCU or cause voltage spikes. That is why to prevent this, you need to use a 33 ohm resistor between the PWM pin and the Gate. The resistor will limit the current and protect your MCU.

Use a pull-down resistor between the Gate and GND

While the PWM pin should be either HIGH or LOW, there could be situations where your Gate pin can have nothing as input, for example when the MCU is powered off. In this case, the Gate could float to an undefined voltage level, which could cause the MOSFET or transistor to turn on partially or unpredictably.

To avoid this, you need to use a pull-down resistor between GND and the Gate pin. This resistor will keep the Gate at LOW voltage when the MCU is not connected or powered off. This way, you can ensure that your MOSFET or transistor stays off.

The value of the pull-down resistor depends on how much noise and power consumption you can tolerate. A lower value will make the Gate more resistant to noise but also draw more current from it when it is HIGH. A higher value will save more power but also make the Gate more sensitive to noise. I recommend using a 10k Ω or a 1k Ω resistor for most applications.

Consider the current load of your device

Another factor that you need to consider is the current load of your device. If your device draws more current than the maximum rating of your MOSFET or transistor, you can damage them or cause overheating. Therefore, you should check how much current your device consumes at its highest level (e.g., when all parts of it are turned on at the highest level and speed). You can use a current meter to measure that.

If you are only using your device to control a few LEDs or lamps, you probably don't need to worry about this. Most MOSFETs can handle up to 3 Amps of current, while generic transistors are much more limited (e.g. about 100 milli Amps). However, if you are using your device to control something else, you should check the specifications of your MOSFET or transistor and make sure they can handle the current load. The amount of current that a transisor can handle is specified by collector current in its datasheet.

Now that you know how to protect your MCU from damage, let's see how to apply these principles to a specific case: If you are planning to use a motor, which has a coil inside it, so you need to consider some additional factors.

Use a flyback diode for devices with coils

Some devices have electromagnetic coils inside them, such as solenoids, motors, or relays. These devices can cause trouble for your MOSFET or transistor when you switch them off. This is because coils generate a reverse voltage spike when they lose power. This can damage your MOSFET or transistor by exceeding their negative voltage rating. This is a side effect called inductance.

To solve this problem, you need to use a diode called the flyback diode or the kickback diode. This diode is connected in parallel with the coil device but in reverse direction. This diode allows the reversed current to circulate inside the diode and the device until it dissipates safely. This way, you can protect your MOSFET or transistor from voltage spikes. You can learn more about this diode from Wikipedia.

Dealing with motors, relays, etc

If you are planning to power a motor, it has a coil inside it. As I explained before, a coil device like a motor can generate a reverse voltage spike when you cut off power to it. This spike can damage your MOSFET by exceeding its voltage rating.

To prevent this, you need to use a diode near the motor. The diode should be connected in parallel with the motor but in reverse direction. This way, it will allow the reversed current to circulate inside the diode and the motor until it dissipates safely.

The closer the diode is to the motor, the better, as this will reduce the length of the wires that can act as antennas and pick up noise. If possible, try to put the diode on the motor itself or on its terminals. If you can't do that, at least use it on your PCB along with the MOSFET.

The type of diode you need depends on the voltage and current ratings of your motor. I would say a 1N4007 could suffice here, as it can handle up to 1000V and 1A of current. Just remember to put it backwards in the circuit (Anode goes to the Drain pin of the MOSFET and Cathode goes to the VIN of your motor). Unlike resistors, diodes must be used in the correct direction.

Use a capacitor to stabilize the 5V input

Another thing you need to consider is the stability of the 5V input of your ATTiny. When you switch on or off a high current device like a motor, you can create voltage fluctuations or dips on your power supply.

This can affect the performance or even damage your MCU. For example, if the MCU doesn't receive enough power, or the power is not "clean", it can face hangs or program crashes, or skip some instructions in the code and thus not work reliably. In other cases, it could self-reset (e.g. by not receiving power, or triggering the watch dog timers (WDT) set by the user.

To avoid this, you need to use a capacitor to stabilize the 5V input of your ATTiny. A capacitor is a device that can store and release electric charge. It acts like a small battery that can smooth out voltage variations and provide extra current when needed.

You should connect the capacitor between the 5V and GND pins of your ATTiny, as close as possible to the MCU. The value of the capacitor depends on the current draw and frequency of your motor. A general rule of thumb is to use a 220 μF cap for every 1A of current to regulate the 5V input of your ATTiny.

At the same time, you need to use a smaller capacitor (e.g., 0.1 μF or 100 nF) in parallel with the larger one to filter out high-frequency noise. This smaller capacitor needs to be as close as to the MCU as physically possible. The 100 nF capacitor is used as a decoupling capacitor near the MCU to provide a low impedance path for high-frequency currents drawn from the MCU.

Use a resettable fuse for short circuit protection

The last thing you need to consider is the protection against short circuits. A short circuit is a condition where two points of different voltage are connected by a low resistance path. This can cause a very high current to flow through the circuit, which can damage your components or cause fire.

To prevent this, you have two options:

  • Limit the current with a high wattage resistor
  • Use a resettable fuse (PTC)

However, limiting the current with a resistor would not be a good idea in this case. This is because it would reduce the power available for your motor and affect its performance. It would also waste energy as heat and increase the size of your circuit.

Therefore, I recommend using a resettable fuse to protect your circuit from short circuits. A resettable fuse is a device that blocks too much current from flowing until you remove the load and break the circuit. It works by increasing its resistance when it heats up due to the high current.

A resettable fuse is also known as a PTC (positive temperature coefficient) device. It is made of a polymer material that changes its structure when heated. When it is cold, it has a low resistance and allows current to flow. When it is hot, it has a high resistance and stops current from flowing.

You should connect the resettable fuse in series with your power supply, before the MOSFET and the motor. The value of the resettable fuse depends on the maximum current that you want to allow in your circuit. You should choose a resettable fuse that has a hold current higher than your normal operating current and a trip current lower than your maximum allowable current.

The reduced current also prevents overheating or fire.

The discussion of resettable fuses goes beyond the scope of this answer, so I suggest you look it up on Google for more details.

I hope this answer has helped you understand how to protect your MCU from damage when using a PWM pin to control a MOSFET or a transistor. Have fun playing with electronics!

\$\endgroup\$
2
  • \$\begingroup\$ I am wondering whether a freewheeling diode is needed when switching with a N-MOSFET in the ground leg of a motor. Should not the body diode of the MOSFET be sufficient? \$\endgroup\$
    – ARF
    Commented Feb 25, 2017 at 14:45
  • \$\begingroup\$ I personally don't know if it's sufficient for all N channel varieties, but the extra protection here doesn't hurt, specially for low-current MOSFETs ;) \$\endgroup\$ Commented Feb 25, 2017 at 16:44

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