# Controlling Speed of DC Motors Using Arduino

13,083

32

6

hello

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

PWM _ WAVE

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## 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

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

""good luck""

## Recommendations

• ### Internet of Things Class

22,798 Enrolled

## 6 Discussions

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.

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!