Variable voltage for DC motor

Hello,

I am fairly new here and for school I have to make a DC motor rotate at variable speed by using an arduino. The nominal voltage of the motor is 12V (external battery), so I want to control this battery voltage in order to steer the motorspeed. I also have to make a control loop in order to keep the motor running at the same rotational speed untill I decide to set a new speed. Is it possible to make only with an arduino such a controllable voltage program? Or do I need other electric components like transistors or such?
Any ideas how to make this?

Thanks,
Stan

Yep you need to switch the motor's 12V with a transistor, with pulse width modulation. PWM the base of the transistor so that the collector / emitter switches the 12V off and on at a switching speed which makes the average motor voltage be (say) 4V or 8V or whatever to run the motor at less than full speed.

If I were you I'd get that sorted out, then as a second phase look into the control loop. That will probably need an encoder on the motor so you can sense the actual speed and tweak the PWM to keep it at the right speed.

Start with the tutorials here, particularly this one which uses analogWrite to PWM an LED's brightness. Then have a look at this, to see how to drive a motor: it essentially replaces the LED with a transistor/motor setup. Don't forget the base resistor and the diode.

Thank you, the encoder I had already in mind. Also thanks for the tutorials!

There's also this adafruit tut, although it shows the motor powered off the Arduino which you'll want to change.