# How to Control a MOSFET With Arduino PWM

16,557

10

9

## Introduction: How to Control a MOSFET With Arduino PWM

In this instructable we'll look at how to control the current through a MOSFET using an Arduino PWM (Pulse Width Modulation) output signal.

In this case we'll manipulate the arduino code to give us a variable PWM signal on digital pin 9 of the arduino, and we'll then filter this signal to give us an adjustable DC level which can be applied to the gate of the MOSFET.

This will allow us to control the transistor from an off state with no current flowing to a state where only a few milliamps of current flow or to a state where we have several amps of current flowing through the transistor.

Here I'll set up the PWM so that we have 8192 steps of pulse width variation which give us very fine control over the MOSFET.

## Step 1: Circuit Diagram

The circuit is very straightforward. The PWM signal from pin D9 of the arduino is integrated or filtered by the combination of R1 and C1. The values shown work well an operating frequency of 1.95KHz or 13 bit operation with 8192 steps (2 to the power 13 = 8192).

If you decide to use a different number of steps then you may need to change the R1 and C1 values. For example if you use 256 steps (8 bit operation) the PWM frequency will be 62.45 KHz you will need to use a different C1 value. I found 1000uF worked well for this frequency.

From the practical point of view a PWM setting of 0 means that the DC level on the MOSFET gate will be 0V and the MOSFET will be completely turned off. A PWM setting of 8191 will mean that the DC level on the MOSFET gate will be 5V and the MOSFET will be substantially if not completely turned on.

The resistor R2 is in place just to ensure that the MOSFET turns off when the signal on the gate is removed by pulling the gate to ground.

Provided that the power source is capable of supplying the current dictated by the PWM signal on the MOSFET gate, you can connect it directly to the MOSFET with no series resistor to limit the current. The current will be limited by the MOSFET only and it will dissipate any excess power as heat. Be sure that you provide an an adequate heat sink if using this for higher currents.

## Step 2: Arduino Code

The arduino code is attached. The code is well commented and fairly simple. The block of code on lines 11 to 15 sets up the arduino for fast PWM operation with output on pin D9. To change the PWM level you change the value of compare register OCR1A. To change the number of PWM steps you change the value of ICR1. e.g 255 for 8 bit, 1023 for 10 bit, 8191 for 13 bit operation. Be aware that as you change ICR1 the frequency of operation changes.

The loop just reads the state of two pushbutton switches and increments the OCR1A value up or down. I've preset this value in the setup() to 3240 which is just below the value where the MOSFET starts to turn on. If you use a different transistor or C1 & R1 filter circuit this value will be slightly different for you. Best to start with the preset value at zero the first time you try this just in case!

## Step 3: Test Results

With ICR1 set to 8191 these are the results I obtained varying the current between 0 and 2 AMPS:

OCR1A (PWM SettingCurrent (ma)Gate Voltage (Vdc)
3240 0 ma 0v
3458 10ma 1.949v
4059 100ma 2.274v
4532 200ma 2.552v
4950 500ma 2.786v
5514 1000ma 3.101v
6177 1500ma 3.472v
6927 2000ma 3.895v

## Recommendations

Hello, as with the question about the 490Hz from Arduino which is 10bit I believe, Thats what I was planning as well. Keeping things simple, Im looking to control a dc motor around 6-7volts. and using a mosfet with a pwm signal to the Gate. And varying the PWM signal with a POT as well. The signal looks nice and clean on the scope but havent tried it with the motor yet. My project is semi large. The motor doesnt pull more than 3amps, my mosfets can handle up to 30amp with 5volt at the Gate. Ive never tried PWM a mosfet before and my setup actually will look like your diagram above, as for the control circuit. Just only at 490Hz, Why do you choose higher frequency ranges like that? does it have to do with having a cleaner signal?

Sorry just one more quick question, looking at your code, is the number of pwm steps how you set the 2kHz frequency? I do see that a 2k frequency does smooth it out more and what was more noticeable was actual noise suppression. I tried a little brushless fan and found that at 490 it actually makes noise. I was able to tone it down by adding a 100uF cap, but it still alittle noisy. Those lower frequencies are just at a humans hearing level. I figured Id hear 490Hz but not that much. Thanks again for the code and idea..

https://www.arduino.cc/en/pmwiki.php?n=Tutorial/Se... go to this web link to learn all about changing the timers in Arduino to change frequencies etc. I believe that the default frequency for PWM is 490 Hz so if you just want to use that you can remove the code for the timer changes but you will only have 255 PWM steps available and you will need to analogWrite the value to the pin you are using.

Sir, I am a bit confused. Why must you changed the PWM frequency to 1.95KHz? The PWM frequency from pin 9 is fixed at 490 Hz. Is it possible to use the original frequency of 490 Hz? The duty cycle will be controlled by (variable) potentiometer via to one of the analog pins.

Did you watch the video? It explains this. 1.95KHz works well the R1 C1 filter that converts the PWM signal to DC. 490 Hz might work, or you might have to change the values of R1 and C1 to get a clean DC signal. Just try it with 490 Hz and check if you have a good clean DC signal on the base of the transistor. It will probably work fine.

I am a chemist but wan to learn something about Arduino from very beginning. I found this is very useful in my temperature control project. Since I have very limited background on electronics, I have to get some instruction on this. I understand how this works but need help on selecting the parts since my project uses different power. My power supply is a 20 V 20A DC power supply but I found 20 V is the best voltage. Could you suggest the parts I should use or let me know how should I calculate R2, C1? Thank you very much.

The existing R2, C1, R 1 values should work as they are just a filter for the PWM signal from the Arduino and also to make sure the MOSFET turns off when it should. You should be able to put 20 volts across the mosfet but make sure that you only apply a low. Voltage to the Arduino, I.e use the USB cable from your computer to power the Arduino or an approximate 5 volt supply.