Everything on model trains, model railroads, model railways, locomotives, model train layouts, scenery, wiring, DCC and more. Enjoy the world's best hobby... model railroading!

Brand New! Just Released! 15 Background City Buildings To Download and Build

Arduino Coding for a Turntable

Tony models OO and asks:

“Does anyone have a simple Arduino coding for a turntable? I’ve made a simple 3d printed turntable for the model railway I’m building for my grandson and want to control it from an Arduino. It’s a simple one-on one-off system. I intend to use a Hall Effect sensor to align it with the tracks. Ta in advance for your help.”

Add your comments or code to assist Tony below.

3 Responses to Arduino Coding for a Turntable

  • Nigel says:

    There is an absolutely brilliant thread on RMWeb that deals with this very issue. He covers both the motorisation of the turntable using a stepper motor, using the hall sensor for alignment and then a step by step explanation of the arduino code which culminates in controlling the whole thing via DCC. Even if this doesn’t give you exactly the code you need for your particular setup, by the time you’ve gone through it you’ll have such a good understanding that you’ll know exactly how to make the amendments you need.

    You’ll find the thread here:

    https://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project-using-a-arduino-uno/

    Good luck!

  • The N-Scale Nerd says:

    If you’re using the Arduino, Stepper-motor module, I tweaked the code to include a FWD and REV button to manually swing the table. You’ll have the stepper.h file in the Arduino library.
    For your application you’ll have to include an input for your Hall Effect sensor, too.
    The other issue is to find the right shaft-coupler for the turntable. The diameter of the Arduino stepper motor is 5mm.
    Here’s the code…

    //FORWARD/REVERSE Control of Stepper Motor – Rev XX
    //26/3/2020 – Use Rubber coupler to motor. (StepsperRev set to 10)
    //24/4/2020 – Use Lego gears to motor. (StepsperRev set to 64)
    //27/04/2020 – Found a plastic shaft-coupler – Works a treat!
    //26/05/2020 – Change StepsPerRev to 5

    #include

    // Constants used to set pin numbers:
    const int CWbuttonPin = 2; // the number of the Clock-wise pushbutton pin
    const int ACWbuttonPin = 3; // the number of the Anti-Clock-wise pushbutton pin

    const int stepsPerRevolution = 5; // 10 – Gives ~10 degrees;64;200;change this to fit the number of steps per revolution
    // for your motor

    // initialize the stepper library on pins 8 through 11:
    Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); //NOT 8, 9, 10, 11 as shown in examples!!!

    // variables will change:
    int CWbuttonState = 0; // variable for reading the pushbutton status
    int ACWbuttonState = 0; // variable for reading the pushbutton status

    void setup() {
    // put your setup code here, to run once:
    pinMode(CWbuttonPin, INPUT);
    pinMode(ACWbuttonPin, INPUT);

    // set the speed at 200 rpm:
    myStepper.setSpeed(200); //60 initially
    }

    void loop() {
    // Here’s the main code, to run repeatedly:

    //Clockwise Push-button code
    // read the state of the CWpushbutton value:
    CWbuttonState = digitalRead(CWbuttonPin);

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (CWbuttonState == HIGH) {

    myStepper.step(stepsPerRevolution);//Rotate clockwise

    } else {
    CWbuttonState = 0;
    }

    //Anti-Clockwise Push-button code
    ACWbuttonState = digitalRead(ACWbuttonPin);

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (ACWbuttonState == HIGH) {

    myStepper.step(-stepsPerRevolution);//Rotate anti-clockwise

    } else {
    ACWbuttonState = 0;
    }

    }

  • The N-Scale Nerd says:

    Oh..Sorry. That looks terrible!
    Perhaps the file…See attached

    By the way , it was the Arduino, XC-4458, 5V Stepper Motor + Controller

    I was hooking up to a PECO NB-55 (N-guage) table, and I ended up using a length of LEGO Technic shaft glued into the turntable rotor to make the connection possible. I see you’re in OO, so you’ll have to figure out a way to hook your table up.

Leave a Reply

Your email address will not be published. Required fields are marked *

Add a photo or image related to your comment (JPEG only)

SUBMIT YOUR QUESTION

Use Tiny Railroad Micro Controllers

Download Your Free Catalog

N Scale Track Plans

Watch Video

Model Train Help Ebook

Model Train DCC HELP

NEW TO MODEL TRAINS?

FREE Tour Inside Club

Take a FREE tour inside the club.

Scenery Techniques Explained

Scenery & Layout Ideas

Model Railroading Blog Archive

Reader Poll

Sorry, there are no polls available at the moment.