Using Nano and L298n with rotary encoder to control model train

Hello everyone,
I am trying to build a controller for my little 1:220 model train using an arduino nano, a 12v power supply for the motor and a L298N motor controller with the speed control being a rotary encoder.

I am having trouble figuring out how to include suitable code into the sketch to control ENA on the controller with a PWm signal.

Any suggestions?

Hello and good morning,
did you already ask the WWW too?
I guess there are a lot of tutorial and examples.

Hi, I did do quite a few searches but came up with nothing specific

Don’t make the beginner’s mistake of trying to do too much at once or looking for a compleat solution, work on it in stages.

For a start look for a good library to handle a rotary encoder, one that uses interrupts not polling. Then just print the results of turning the knob. You will see the count increase and decrease but it will also go outside the range of 0 to 255 that you want. So then work on a way that constrains the number to the range you require.

Then you just send the number to the PWM pin. To be sure you do not refresh this too often only set the PWM pin when the number from the count has changed. This is because sending a value to the PWM resets the value and if you bombard it with requests to change you will never see a pulse out of it because it is always resetting.

Hello
Or start with a potentiometer simply.

Hi Grumpy_Mike

Thanks for the guidance. I will check out what libraries there are and go from there. In the meantime, I will probably go with paulpaulson's suggestion to use a good old fashioned pot.

Many Thanks

Hello, yes I think that's a good idea. Don't have any pots right now, will have to order some.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.