Model Railway Turntable - in a spin so much Im dizzy

Firstly, my apologies for using incorrect language etc. I am very new to all this and am trying to learn extremely quickly. I am extremely appreciative of any help that can be offered and thank you for reading on.

I have been trying to create a sketch to control a T-Gauge 1/450 scale model railway turntable with simple clockwise and anti clockwise buttons to control direction. I then desire motor travel to the next pre-stored location/step count in said direction.
I have been going around and around in circles for a week or more (pun intended) that im getting dizzy.
Can i please reach out for some guidance/advice, especially as to whether this project is feasible or not.

I have read so many online pages, watched tutorials, followed youtube videos, etc. I have even attempted to use Chat GPT as one video recommended it. All to no success.

I then spent time building and wiring a tester board with sensors, switches, buttons, pots, LEDS etc all built in so as to simplify wiring and ensure it was not the wiring at fault. I am satisfied following extensive testing that these are all now wired correctly.

I have tried learning about .cpp and .h files to try and break the code down but I think im getting more and more confused. I even attempted to follow some tutorials to enable me to create these but im starting to believe my entire approach is wrong. Please help.

I am trying to create the program/sketch to meet my brief as below:

i need it to control a stepper motor 28byj- 48 using an uln2003 driver board to be used as a model rail turntable. - I do not require anything too substantial due to the size if the model.

i need to be able to store 12 saved locations. 6 locations and then the same 6 plus a half turn so the train can be turned around to face the opposite way.
I also require one additional ‘home’ location set at 150 steps clockwise from zero. This should remain locked. This ensures there will always be one saved location from zero.

i require the last known position to be stored to the EEPROM for next power up.
if power is lost before saving the current location then upon power up the zero routine should be automatically initiated.
Upon power up, and if the last position is known, the motor should turn a quarter turn clockwise, followed by a quarter turn counter clockwise, to show the user that the system is ready and calibrated.
If the last position is unknown the find Zero Routine will activate automatically.

the zero routine should also be able to be activated manually by holding both directional buttons down for 5 seconds.
The Zero Routine should allow for the LM393 IR Optocoupler to determine the zero when the beam is broken. - i can use an alternative, a micro switch, a KY-024 hall effect sensor, etc., as per advice and guidance but i am very limited on space due to the size of model.
When active this motor should turn counter clockwise until the beam is broken. This beam break is zero location. Whilst this zero routine is active all 4 leds should be lit constantly.
Once zero is located the motor should turn clockwise to home and then flash all 4 leds twice to show the user the system is calibrated and ready.
The system will then revert back to normal operating mode and await a user input.

Normal operating mode should act as follows.
i would like to be able to control direction of travel with a push button for clockwise and a different push button for counter clockwise travel.
when a button is pressed the motor will move to the next saved position in the desired direction of travel.
whilst travelling the corresponding directional led will light to show which direction the motor is in motion to the user.
if the directional button is held pressed for 3 or more seconds the motor will complete a half turn in the desired direction. the corresponding directional led will flash until the desired location is met.
Whilst a travel order is being carried out a button press in the same direction of travel will be ignored until the destination is reached. If the direction button for the opposite direction is pressed then this will pause all movement. The motor will then await a directional input, and then move to the next saved location in that direction.

i need to be able to activate a ‘customise saved location mode’.
In this mode the zero and home locations will be ignored for the user and only zero will be used to determine the steps required to store the desired locations.
i need to be able to access this ‘customise saved location mode’ by using a rocker switch. Whilst this switch is on the mode is active.
When first turned on the motor will immediately find zero in a clockwise direction and then hold position, awaiting user input. At this point the set position led should light and remain lit whilst in this mode.
By turning the switch off the mode is exited and anything unsaved is lost. The motor will then return to home.
During this mode if a directional button is pressed, then the motor will turn to the next saved location in that direction, if it exists. This will assist with big steps. If no saved position exists then the motor will remain stationary.
In this mode the ky-040 rotary encoder will be used to control precision steps manually to allow for fine tuning.
The KY-040 button will then be pressed for 5 seconds to save the current position.
This will also save a position exactly a half turn from the set position.
To confirm a successfully set saved location both directional leds will light and the set position led will flash 5 times. During this time, the motor will move clockwise 100 steps, then 100 steps counter clockwise.

i need to be able to activate a ‘delete saved location mode’.
In this mode the zero and home locations will be ignored for the user and only zero will be used to determine the steps required to the desired locations.
i need to be able to access this ‘delete saved location mode’ by using a rocker switch. Whilst this switch is on the mode is active.
When first turned on the motor will immediately find zero counter clockwise and then hold position, awaiting user input. At this point the delete led should light and remain lit whilst in this mode.
By turning the switch off the mode is exited and anything unsaved is lost. The motor will then return to home.
During this mode if a directional button is pressed, then the motor will turn to the next saved location in that direction, if it exists. This will assist with big steps. If no saved position exists then the motor will remain stationary.
In this mode the KY-040 button will then be pressed for 5 seconds to delete the current position. The rotary dial will remain inactive throughout this mode.
This will also delete the position exactly a half turn from the selected position.
To confirm a successfully deleted saved location both directional leds will light and the delete led will flash 5 times.
Whilst in this mode the user needs to hold the rotary encoder button and the reset button together for 10 seconds to complete a ‘full reset’, thereby deleting all user defined saved locations. To confirm the full reset all leds will flash 10 times and the system will then exit the mode and begin the find zero routine.

i require an led to show that power is going to the system and that the motor is awaiting its next input. This led should blink when an action is being received, but otherwise should remain in a solid state.

To date i have been using the following pin definitions, however, if need be these can be changed if the Arduino Uno R3 is better allocated another way.

// Pin definitions
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define SENSOR_DO_PIN 6
#define POWER_LED 7
#define DIR_0_LED 8
#define DIR_1_LED 9
#define DIR_0_BUTTON 10
#define DIR_1_BUTTON 11
#define CUSTOMISE_SAVED_LOCATION_MODE_SWITCH 12
#define DELETE_SAVED_LOCATION_SWITCH 13
#define ENC_SW A0
#define ENC_DT A1
#define ENC_CLK A2
#define DELETE_LED A3
#define SET_POS_LED A4

Thank you for reading.

Am i asking too much of the Arduino Uno? would a different version work? can anyone recommend where i could locate the code/libraries etc that would work for me?

Many thanks.

Notridge13

As my name tells, I'm a both a model railroader and a veteran railroad railroader.
Your massive text needs to be cut down considerably. Please read and use the topic: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

Please post formatted code, in code tags, schematics and links to datasheets of devices not being every day circuits.

1 Like
  • Always, always, always, start with a schematic and share it with us.

I entertained myself by reading this as you are the quintessential example the railroad industry prefers. I will keep that thought.

The 28BYJ-48 is a 2048 step per revolution motor, 2048 / 12 = 170 1/3 steps. You can't move one third of a step. Watch out for that.

my apologies i did read the guidance notes and i thought i had covered everything.

I didn't upload a schematic because I thought my detailed pin allocations covered this. the only other wiring is from vcc to +v and GND to Ground etc.

i have checked all connections and each item several times on the tester board i have created. using very basic sketches for each item and the serial monitor for correct feedback. My tester board just has pins for the connections so i can run breadboard pins to the desired connections. on each component. The components i am using on my tester board are the exact same as the ones stated in the description of what i am trying to create.
i understood the components to be bog standard, run of the mill components. I thought naming the driver board and parts etc would suffice rather than requiring a full information sheet. my apologies here.
As to switches LEDs etc they are just your basic electronic components easily purchased from your local electronics store. nothing special.
I did forget to mention i am currently powering it using the usb port on my laptop direct to the Arduino and then all my components are connected to the 5v power pin via a breadboard to allow for multiple connections. the GND connections are all connected in the same way via one of the GND pins on the Arduino.

Thank you JCA34F, i will keep it in mind. At the moment i would settle for the circuit working as intended. I can physically relocate the tracks etc to allow for what can and cant be done with specific addressed once i can see it running and test it.
I have purposely avoided finalising the exact location of the rails so as to avoid any complications. But tbh im fried from all the research and searches ive been doing.

My apologies again for anything im doing wrong/overlooking. The software side of things is new to me and that includes the use of forums. I have been seaching the arduino.cc forum a lot but after not finding what i was after i decided to try and create an account and reach out for support.
I did make sure i read manuals and searched google etc before doing so though.

Another caution about 28BYJ is the large backlash in the 64 : 1 geartrain, I've seen more than 12 steps of freeplay when reversing.
Sounds like you are doing things right, good luck.

1 Like

Sorry but neither Google Translate, nor my dictionary, manages that word. Let me point out that I have no history in the railroad industry. Railroads has been an interest from the pram up to now, driving veteran trains.
Yes, I have been nailing tracks on a veteran railroad, 50 years ago.

That's not a good idea. Words never show what schematics show. Often connections not made play a roll.

Hopefully helpers keen on reading novells will help You out.

Of, relating to, or having the nature of a quintessence;

quintessential

avgörande ? :slightly_smiling_face:

I think part of the problem here is that you have made the project more complex than it needed to be, reducing your chances of success.

I would have suggested a DC motor with gearbox and h-bridge driver (not l293/8), and an optical sensor.

The sensor would be on the circumference of the turntable and would sense dark/white markings on the edge of the turntable. I assume the tracks don't enter the turntable at equal angles around the circle, so finding "home" can be done with timing, since one of the gaps between tracks with be longer than any other.

1 Like

The approach with a stepper is the correct way for realizing a turntable. There are many solution to be found on the internet.

@notridge13 : Obviously you are a friend of long explanations. But as already mentioned, pictures and schematics are much clearer than a lot of words :wink:
You could start with the MobaTools library. It contains classes for steppers and buttons and was originally written with the model railroad in mind. There are a lot of examples with steppers which you could use as a starting point (finding 'zero' and going to fixed positions related to a button press ). Changing an example from bipolar to unipoar stepper is fairly easy, because the only difference is when creating the stepper instance an when assigning the pins.
But be aware that your project isn't really a beginners project. I think you have to learn a lot and the advice of @JCA34F about the backlash of the gear is important.

1 Like

Why is it correct?

Why is my suggestion incorrect?

One model railroader to another.
The problem with your post lies in the nature of this forum. Helpers are keen to get on with exploring solutions to posted problems- not drawing pictures. Drawing a schematic from your wall of text is well known to be fraught with problems, both in missing information and in the vagaries of the spoken language. Additionally, only a small subset of those willing to help are 'rails'. You'd be surprised at how much easier it is for us to read between the lines of your post.
Hence, you must present us with a schematic, to eliminate those issues and misunderstandings. That is an accepted method of technical communication worldwide.

1 Like

A stepper is primarily a positioning device. Once the reference position is known, it is easy to move it exactly to different known positions. And that is what is needed with a turntable.
Exact positioning is much more difficult with a DC motor.

Hi, @notridge13
Welcome to t he forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

As ever, trying to do the whole thing ‘Big Bang’ style is hard to make, hard to debug.

Try to make your code do minimal things to validate your hardware and work up from there.

1 Like

People continually try to force fit a stepper motor to directly drive some project that require fractional steps. They never seem to understand the cog belts and pulleys can be driven by the same stepper and give the necessary fractional step control.

1 Like

I have drawn a rough diagram as requested. Hopefully it all makes sense. Any issues let me know. I will also post some photos of the parts used.



2 Likes