How to control DC motor using PWM from Arduino | applied electronics engineering

Latest

How to control DC motor using PWM from Arduino

By Applied Electronics - Thursday, January 19, 2017 No Comments
In this arduino tutorial we show How to control DC motor using PWM from Arduino. This requires the use of the analogWrite function. PWM pulses from arduino can emulate analog signal to control external devices such as DC motors or brightness of a light bulb or LED.

A similar tutorial on PWM and analogWrite function is How to use Arduino PWM to control signal strength.

Shown below is the circuit schematic of how you can control DC motor using PWM from Arduino using single transistor, diode and capacitor.

How to control DC motor using PWM from Arduino

The DC motor is connected to the PWM pin via transistor interface. That is the PWM pulses are applied to the base of a transistor which amplifies current which appears at the collector to which the DC motor is connected. The transistor used is 2N3904 NPN transistor but 2N2222 transistor can also be used. The emitter is just grounded at the same potential as arduino.

A capacitor and a 1N4001 diode is placed across the DC motor. The electrolytic capacitor is of value 1uF which is used to filter noises and spikes created by the motor. The diode is there to reserve current flowing into the DC battery.

As you can see a separate DC voltage battery of 9V has been used here. This separate battery is used along with a transistor because the motor requires more current than the arduino can supply.

The arduino program code for controlling the DC motor is below.

const int mot = 6;

void setup(){
    pinMode(mot, OUTPUT);
  }

void loop(){
    for (int k=0; k <256; k++)
    {
      analogWrite(mot,k);
      delay(10);
      }

      delay(1000);

    for(int k=255; k >= 0; k--)
    {
      analogWrite(mot,k);
      delay(10);
      }
      delay(1000);
  }

In the code, mot label is assigned to pin 6 which is one of PWM pin of arduino. We then set up the mot as output using the setup function. In the main loop, we write PWM pulses using the analogWrite function. This function accepts two arguments- one is the pin and the other is the value. Since arduino microcontroller is 8 bit you can write 8 bit word to the PWM pin. Thus the value can be from 0 to 256 in binary. Using the first loop we increase the speed of the motor and using the second for loop we decrease the motor speed.

See the following video which illustrates the DC motor control using Arduino in Proteus professional.


   See also Arduino free books:

- Download Arduino Workshop: A Hands-On Introduction with 65 Projects pdf free
- Download Make: Basic Arduino Projects: 26 Experiments with Microcontrollers and Electronics pdf free book

No Comment to " How to control DC motor using PWM from Arduino "