Introduction: Controlling Speed of DC Motors Using Arduino


in this instruction i want to show you how to controlling speed of dc motor with arduino .

for more information about pwm wave you can read this page :


ok... Let's start with no waste of time.

Step 1: Hardware Requirements

1- arduino uno

2-dc motor

3-L293 IC drive

4-external Power supply ( i used sealed lead-acid battery 6v)

5-potentiometer 100k ohm

6-jumping wire (male and female)

Step 2: Wiring and Connections

Before proceeding further it is recommended that you read and understand the pin diagram of L293D

Pin diagram of L293D

L293D Connections to arduino:

input1===>digital pin 11(PWM PIN)

input2===>digital pin 4

enable1===>digital pin 9

GND===>to battery(Negative polarity)

output1===>to motor

output2===>to motor

vcc1===> to battery(Positive polarity)

vcc2===> to battery(Positive polarity)


potentiometer===>analog pin A1


we must use of external Power supply because Arduino can not Provide current requirements for motor.

Step 3: Pwm Frequency Control - Arduino Uno

there are 6 PWM channels available . the istruction to produce pwm output is analogWrite(pin,Duty), where pin must be 5,6,9,10,11,or 3, and Duty is the duty cycle, entered as 0-255 corresponding to 0-100%. The default PWM frequency is 490 Hz. To change the frequency an additional instruction is required. The PWM frequency is controlled by three internal timers: timer0, timer1, and timer2. The instruction to change the PWM frequency is

" TCCRnB = (TCCRnB & 0xF8) | i " where i is chosen depending on which timer is associated with the PWM pin whose frequency you want to change. Note that the PWM pins are associated in pairs with the same timer. Thus if you change the PWM frequency for pin 9, you will get the same frequency for PWM on pin 10. However the duty cycles can be different on the two pins. The table below summarizes the options available.

Example frequency change instruction:

TCCR2B = (TCCR2B & 0xF8) | 2
(sets PWM pins 3 and 11 for 3906 Hz)

Example program:

void setup()


pinMode(3,OUTPUT); //make pin 3 an output

TCCR2B=(TCCR2B&0xF8) | 2; //set PWM frequency to 3906 Hz for pin 3 (and 11)


void loop()


analogWrite(3,127); //do 50% PWM on pin 3 at the frequency set in TCCR2B


In general you should avoid changing the PWM frequency on pins 5 and 6 since they use timer0,which controls the delay and milli functions. These functions will return incorrect results if the frequency of timer0 is changed from the default. The factor 0xF8 is a mask so that you only affect the bits for the frequency in the TCCRnB register when OR-ing with the | operator.

Step 4: the Code

you can download the code of this instruction in below :

also you can contact to me with my email address :
ashkan. khajeh

""good luck""