Digitally manipulating speed of DC motor

I am trying to vary the speed of a DC motor WITHOUT the use of a manual potentiometer (twisting a knob).

Is a digital potentiometer the simplest way to do this? If so, does anyone have a digital potentiometers that they recommend for a beginner? If there is an easier way to do this, can you please advise?

My ultimate goal is to vary the speed of the motor based on the quantity of light in a room (using a photoresistor).


Assuming it is a standard brushed DC motor, you can control it using PWM, with a mosfet and flyback diode to drive the motor from an Arduino. See, but connect a 100 ohm resistor between the Arduino pin and the mosfet gate.

Digital pots are for signals, not power.

Thanks for the replies!

I created the circuit as shown from the link. I added in an extra 220 ohm resistor between the Arduino Pin 3 and the mosfet gate. (Didn't have a 100 ohm resistor)

I am still unable to control the speed of the motor. I'm a little confused as to how the mosfet helps us to accomplish the goal of varying the motor's speed. Can someone explain this to me?

Here is my code:

const int motorPin = 3;

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

void loop() {

digitalWrite(motorPin, 100); }

To vary the speed, you need to use analogWrite to write to the pin (not digitalWrite), passing values between 0 (off) and 255 (full speed).

Thank you. Analogwrite worked like a charm.