Building 1-9v Train Trafo with Potentiometer

Im trying to build an Trafo for my Lego Trains because my old one is broken and it would be super to build it by myself. I already have some Ideas.

What I want is that I can control the dc motor with the potentiometer. One thing where I'm not sure is how to find the right potentiometer (I would need one that has an middle where its 0 and a left and a right side. Like it is in some remotes for RC cars). I want the two sides so that I can change the way how it rotates. The coding would be pretty simple if the potentiometer is 0=5 than use the first transistors and if its 5=10 than use the sencond transistors. Hope you understand me :sweat_smile:

Basically I don't know how to do it with the transistors, which ones I need and if npn even the right one.

Also the Train is on an metall rail that provides him with power. The dc motor is an 9v motor and as the battery I would use the arduiono 9v power adapter.

Almost any (linear) potentiometer should do. (Audio pots are not linear.) 10K is a common-standard value but a 1K or 100K should work too.

Look at the [u]Analog Read Serial Example[/u]. The 10-bit analog-to-digital converter reads 0-1023, so with the pot at the mid-point you should read about 512.

Also, look at the and [u]Read Analog Voltage Example[/u]. It shows you how to use a ratio to convert the 0-1023 reading into something useful.

Also look at the [u]Fade Example[/u]. (Fading an LED is similar to controlling motor speed.)

The 8-bit PWM "analog" outputs are scaled 0-255. So for example, with your 9V power supply you'd read about 512 with the pot at mid-point, divide by 4 and write 128 PWM for a 50% duty cycle (4.5V average).

Note that the Arduino doesn't have a digital-to-analog converter, so no true-analog outputs. It uses [u]PWM[/u], which is actually better for motor control because it's more efficient and puts less strain on your transistor/MOSFET.

[u]Here is a MOSFET driver circuit[/u].

Does the voltage need to reverse? If so, you need to get (or build) an H-Bridge motor driver.

Im trying to build an Trafo

A transformer? Transformers are for AC, not DC. (Some people call a DC power supply a transformer, but technically that's wrong... It has a transformer inside, plus some other components.)