Introduction: Controlling Speed of DC Motors Using Arduino

Picture of 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

Picture of  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

Picture of 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

Picture of 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""


Eric Brouwer (author)2016-11-22

Thanks for sharing.

Please note that their are different versions of the L293 available.

If you use the L293D, this circuit is perfect.

If you use the L293, additional diodes need to be added across the motor.

AshkanK1 (author)Eric Brouwer2016-11-22

Thanks for this point ,in this instruction I used L293D

stechi (author)2016-11-22

Interesting, this relates to something I have been thinking about. It would be good to get a constant speed motor control by sensing the back emf from the motor (which corresponds to speed) in between pulses, and using this to modify the pulse width so that the b.emf and therefore the speed is made constant. I am not clever enough to do this myself!

Eric Brouwer (author)stechi2016-11-22

The back-EMF generated by the motor must be absorbed by fly-back diodes to prevent damage to the driving transistors. Depending on the driver and motor size, the back-EMF will also cause havoc with the other electronics if not controlled.

I am not sure if you will still be able to measure the back-EMF with the diodes in place for your application. But it will be interesting to see what you intend to do.

Have a look at one of my H-Bridge designs.

Swansong (author)2016-11-22

Good instructable :) I'd love to see a video of it in practice!

AshkanK1 (author)Swansong2016-11-22

thank you , sure :)