All About Pulse Width Modulation

Pulse Width Modulation (PWM) is a very useful technique to control and modulate a device electronically. An example would be controlling the speed of an electric motor. One way to do this is through the use of a potentiometer to limit the current going to the motor by shunting some of the voltage to ground. The problem with this is the current induced heat that builds up in the potentiometer. PWM solves this problem by sending short pulses of current to the motor. By changing the width of the pulses we can make the motor go faster (longer pulse) or slower (shorter pulse). There are a few other ways to do this with electronic components such as a 555 timer or with switching transistors. A very simple way is to use a micro controller to send the pulses to a MOSFET transistor which then acts as a switch to deliver a separate current to the motor.

In this week’s video I’ve set up a simple demonstration using a breadboard to hold a few components, a small DC motor and an Arduino Leonardo micro controller. I connected the output from the MOSFET to my analog Tektronix 453 oscilloscope so you can see the square wave pulses as they change width. You can use the parts list, wiring diagram and code below to set up this experiment yourself.

Parts list:

1 – N-channel MOSFET transistor (used to switch negative voltage)
1 – 100K resistor
1 – 1N4001 rectifier diode
1 – Small DC motor
1 – Breadboard and some hookup wire
1 – Arduino compatible micro controller

About the circuit:

The MOSFET has three pins; Gate, Drain, Source. When a voltage is applied to the GATE it triggers the MOSFET to turn on. The SOURCE pin of an N-channel MOSFET gets connected to the negative side of the supply voltage. The DRAIN pin is what delivers the negative voltage to the device being controlled. A 100K ohm pull down resistor is usually connected between the Gate and ground to insure that the MOSFET turns off completely when no voltage is present at the GATE. A rectifier diode is connected between the positive voltage and the DRAIN to protect the MOSFET from motor induced back current. The potentiometer is connected to the micro controller and the code reads the resistance value to determine the pulse width that is sent to the MOSFET gate pin.

Here’s a picture of the setup I used in the video.
pwm breadboard actual  layout

Wiring Diagram:
pwm breadboard layout


/* Simple code to experiment with Pulse Width Modulation by Dino Segovis.
This code reads the variable resistor on pin 3 and stores it as a value.
The value is then converted to another value between 0 and 255 which is output on pin 9.


int motorPin = 9; // motor connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value

void setup()
pinMode(motorPin, OUTPUT); // sets the pin as output

void loop()
val = analogRead(analogPin); // read the input pin
analogWrite(motorPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255

This week’s video… keep on hackin!

About Dino

Self taught electronics and hardware hacker.
This entry was posted in Weekly Hacks. Bookmark the permalink.