I have a model train set which runs on 0-12v based on the position of a rotary encoder. I would like to have somewhat granular, digital control over the voltage on the track.

So far, I'd implemented one of those bog standard eBay's finest voltage controllers, which has a potentiometer on the input, thus can't be controlled from a Raspberry Pi.

I then moved on to the L298N (see thread in Context), but this produces a few problems - namely a "lumpy" DC output, producing resonance in the motor windings, as well as very rough running. I'm now contemplating adding a smoothing circuit to this, but need to understand the electronics behind it to do so.

Another option I'm looking at is the possibility to use a digital potentiometer in conjunction with one of the earlier mentioned motor controllers, which would theoretically give as smooth running as I had previously on the analog input system, with the addition of digital control.

I've considered an ADC, however these are generally more audio-focused, and in my experience tend to cap out at 5v. If this is the right solution, I could do with some direction in terms of a chip or part number for a board that would work.

I'm currently not sure if I'm trying to reinvent the wheel. I suspect this problem has been encountered before, be that for controlling lighting, audio, motors, or any other auxiliary device from a Pi.


I've recently created this thread questioning how to make this work with an L298N.

  • L298N can do PWM, that is the way to go.
    – tlfong01
    Commented Jun 22, 2022 at 2:22
  • 1
    @tlfong01 The L298N is giving me issues with PWM, the question details a requirement for higher frequency, or a way to manipulate the L298N into not buzzing (e.g output smoothing).
    – XtrmJosh
    Commented Jun 23, 2022 at 13:02
  • Just a quick reply: (1) L298N PWM range should be very roughly around 5kHz and 1.5kHz. Outside this range at low frequency (say, < 1KHz), the motor might be shaking. (2) I think "smoothing" using a capacitor for PWM signal is not practical, and most likely won't work. (3) Using ADC/DAC etc, is as you said, is "reinventing the wheel", and I won't recommend it. (4) "the position of a rotary encoder" seems problematic, because a rotary encoder is usually to check speed. Perhaps you can give us more info, eg a link to your model train set, on that.
    – tlfong01
    Commented Jun 23, 2022 at 14:21
  • You might also like to try this US$3 testing tools: DC 12V-40V 32V 10A PWM Motor Speed ​​Controller Speed ​​Regulator With Button Switch Voltage Regulator 400W Board Module - AliExpress US$3.00 fr.aliexpress.com/item/….
    – tlfong01
    Commented Jun 24, 2022 at 13:31

3 Answers 3


It is hard to answer your question because I do not know enough about your train engines and track setup. Since I have built several over the years (last one 20 years ago) I will take a SWAG. The PWM worked great (about 22Khz with the better engines, such as the Athearn (not sure if they are around anymore). I also destroyed the cheaper ones, it only took a few days and they buzzed like crazy. I ran it with a DC bias on the track (about 6 - 8 VDC) and the PWM was to about 15 volts. The good engines ran very nicely and pulled several cars at very low speeds and no buzzing. I implemented it as an analog design but going to digital should work.

Consider using a DAC to support the DC bias and using a MOSFET for PWMing the high side. The BTS7960 (Bridge, great for reversing) would do a great job for you and the losses will be minimal. It will also do the PWMing if you want. Last look they were in the $8.00 US range. There are a lot of options open to you. The more you get into it the better you will be able to tell us exactly what you are doing. Try putting a schematic together and do not worry we are to help you.

  • My intention overall here is to completely avoid this whole DCC thing - given it costs upwards of £30 a loco to add DCC, not to mention the controller cost, that's not a route I want to go down, so I'll be staying very much analog with regards to the modelling world. I've drawn up schematics and even ordered PCBs from JLCPCB before, in this instance it's just a case of not really knowing what exists - I'll take a look at the BTS7960, but I think at the minute I'm liable to end up with a digital potentiometer controlling one of my old DC motor speed controller boards. I've some learning to do!
    – XtrmJosh
    Commented Jun 23, 2022 at 12:59

If I understand your question correctly, I think you will need two components/modules:

  1. A DAC that can be controlled by software in your RPi. It seems they are readily available.

  2. A "series-pass" type adjustable regulator (similar to an LM317) whose output voltage can be set using the DAC output. You can use this online "selector" to find the exact part you need, and TI also provides support through their support forum.

  • As mentioned in the original post, I can't find any DACs (although I won't claim to have searched extensively) capable of running 12v - the first half dozen I found were all capped at logic levels - around 5v. Your suggestion of an LM317 could be very helpful, however, as it's made me aware that there are digital potentiometers available. One of those in conjunction with a DIY H Bridge and the old DC motor controllers would comfortably give me the control I require.
    – XtrmJosh
    Commented Jun 23, 2022 at 12:51
  • @XtrmJosh: It may not (probably does not) need to be a 12V DAC... you only need enough output voltage to control the series pass regulator.
    – Seamus
    Commented Jun 23, 2022 at 13:55
  • My apologies, I didn't realise you said the two components together. Will take a look in more detail shortly, thanks!
    – XtrmJosh
    Commented Jun 23, 2022 at 14:18

Get an ESC (electronic speed control) module. These accept a low-voltage low-frequency PWM signal, or sometimes UART-based digital interfaces such as DSHOT, and produce a proper high-voltage high-frequency PWM signal in the output which will not be audible. Just make sure to get an ESC for DC motors (brushed), not EC motors (brushless). DC motors have two electrical lines, EC motors typically have 3.

ESCs are often rated in terms of "S" instead of voltage, where "S" corresponds to the voltage of a Li-ion cell, 4.2V. For 12V you need an ESC rated at least "3S".

I'm not endorsing any ESC model in particular, but here's an example of what an ESC looks like. On the botton there are power (2 lines) and PWM (3 lines, GND, 5V and signal) connectors, on the top there are lines to connect the motor.

enter image description here

  • 1
    These things seem to be hard to find, at least on Amazon, above a 9.6V rating (presumably S2? and I guess this relates to some RC battery tech). This does look to be somewhat similar to what I've been working with though, in terms of functionality. Interesting that it operates at a higher frequency. I'll keep digging and if I find some, I'll build a test rig out of curiosity. Thanks.
    – XtrmJosh
    Commented Jun 23, 2022 at 13:01
  • @XtrmJosh Again, I'm not trying to recommend a particular product, but the first link I got in Google shopping results was "2/3S Two-way Unidirectional 6Ax2 Brushed ESC" Commented Jun 24, 2022 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.