# Varying Speed of DC Motor w/ Arduino UNO R3

I am using a regular DC motor. I would like to vary the speed somehow. Is it practical to use an arduino to vary the speed? I assume it has to be done through PWM. But doesn't the arduino output pins only output either 5V or 0V?
I think I can do it just by using a regular voltage source with a potentiometer.

Sure - you send PWM to a transistor, the transistor will turn on & off and allow pulses of current to flow thru the motor.
Potentiometers are generally not rated for higher current to control a motor.
Maybe a little one, until the pot heats up and fails.

Wire the pot with 5V on one outer leg, the middle leg to an analog input pin, and the other outer leg to Gnd.
Wire a PWM pin to the Base pin of an NPN transistor with a 150 ohm current limit resistor, the emitter to Gnd, the collecter to one side of the motor. The other side of the motor to +5, +12, whatever you are using.
Then the code is simple, something like:

``````byte PWM3 = 3; // declare pin for PWM output use
void setup(){
pinMode (PWM3, OUTPUT);  // set pin as an output
}
void loop(){
analogWrite( PWM3, (analogRead(A0)/>>2) );  // read A0 as 10 bit number, shift right to make it an 8 bit number, write out to PWM pin
}
``````

Sure - you send PWM to a transistor, the transistor will turn on & off and allow pulses of current to flow thru the motor.
Potentiometers are generally not rated for higher current to control a motor.
Maybe a little one, until the pot heats up and fails.

Wire the pot with 5V on one outer leg, the middle leg to an analog input pin, and the other outer leg to Gnd.
Wire a PWM pin to the Base pin of an NPN transistor with a 150 ohm current limit resistor, the emitter to Gnd, the collecter to one side of the motor. The other side of the motor to +5, +12, whatever you are using.
Then the code is simple, something like:

``````byte PWM3 = 3; // declare pin for PWM output use
``````

void setup(){
pinMode (PWM3, OUTPUT);  // set pin as an output
}
void loop(){
analogWrite( PWM3, (analogRead(A0)/>>2) );  // read A0 as 10 bit number, shift right to make it an 8 bit number, write out to PWM pin
}

Cool thanks. In the above code, where do you specify the speed ?

You said you wanted the speed based on the pot reading, yes? That's what it does. The 8-bit number is the speed.