Turntable controlled by JMRI, CMRI, Arduino Stepper

This post will be a demonstration on my DIY build of a turntable which will be controlled with a stepper motor and an Arduino. The DCC interface control for this will be JMRI using the CMRI library.  For me to get this to work I first had to find a way to control a turntable on the JMRI panel which will put out some sort of communication to the Arduino. One way to do this was to have a light for each position of the turntable and these will be assigned to a bit on the cmri script within the arduino code. Then if an address bit was to change an IF statement would control the stepper motor and position the turntable into the correct place.  Sensors on the track could be placed to safeguard against the turntable moving when a train is still in transit across the table junctions.

First I made the turnout using MDF, routering out the outer circle which will be fixed in place then the inner part was trimmed to approx the width I needed which will take the track. This could also easily be made up from one of the plastic turntable kits available. The stepper motor needed a plate to secure it to the base and was recessed in to the base.

For the wiring I used a L298 H-bridge rated at 12v as my stepper motor was 12v version. This was wired up to pins 22, 23, 24, 25 of my Mega Arduino as shown below

So now all is connected up next will be to upload the Arduino code. This is shown below and explained on the YouTube video

#define CMRI_ADDR 1
#define DE_PIN 2 
Auto485 bus(DE_PIN);
Stepper myStepper(200, 22, 23, 24, 25);
CMRI cmri(CMRI_ADDR, 24, 48, bus); 

int positioning[4];
int currentlocation = 0;
int steps;
int location1 = 0;
int location2 = 10;
int location3 = 30;
int location4 = 100;

void setup() {
    Serial.println("Setup complete");

void loop(){
    positioning[1] = (cmri.get_bit(7)); // 0 steps 
    positioning[2] = (cmri.get_bit(8));  // 10 steps
    positioning[3] = (cmri.get_bit(9)); // 30 steps 
    positioning[4] = (cmri.get_bit(10)); // 100 steps
    if (positioning[1] == 1 && currentlocation != 0){
      Serial.println("Location_1 steps 0");
      int steps = location1 - currentlocation;
      currentlocation = 0;
    if (positioning[2] == 1 && currentlocation != 10){
      Serial.println("Location_2 steps 10");
      int steps = location2 - currentlocation;
      currentlocation = 10;

    if (positioning[3] == 1 && currentlocation != 30){
      //Serial.print("Location deg 30 selected");
      int steps = location3 - currentlocation;
      currentlocation = 30;
    if (positioning[4] == 1 && currentlocation != 100){
      int steps = location4 - currentlocation;
      currentlocation = 100;

Now for the JMRI setup. First create 4 sensors with internal addresses not the CMRI. Give then a username I used turnout_s1 and so on

Then setup 4 lights with the addresses one digit on from the Arduino code bit numbers the same as was done for the turnout setup, so the first bit is 7 and the node is 1 so the address on JMRI will be 1008


Each light needs to be edited to have a sensor from the sensor table turn the light on/off. So edit the first light and use the add control button to select sensor turntable_s1 as active. This will mean when this sensor is active the light will be on and a 1 bit will be sent to the address associated with this to the Arduino to control the stepper motor

Finally to have a visual aid of this open a lay out. Put a turntable on the layout, edit this turntable and ensure the DCC tick box is not selected. Then add each sensor to the layout and position them by each turntable branch. When clicking on the sensor the arduino will now turn the turntable to that position. You must turnoff each sensor as with two on at the same time this will rotate between one and other continuously.


The video below shows this setup and working as a proto type part 1. i will be looking at seeing if this cab be coded better and start building up the turntable model to have trains come on/off it over the next few months so will be posting updates soon.



2 Responses

  1. Nicholas Merton says:

    Love what you are doing. I also am a novice (@66), and making a turntable, but as I don’t have much space under the board have decided to create a worm drive out of Lego, and turn that with the stepper motor. My question is: I have been told I need to reverse the current before letting the engine swing 360deg. I have seen an article on how to do this using an Arduino, but how does one both control the rotation and the swapping of the current using just one device?

    • steve says:

      Hello Nicolas

      I have not got around to doing this part. I had major problems with interferance on the servo lines from the DCC signal which meant pulling all the layout apart and have not got around to getting it sorted, might switch to motor points control. With the weather getting better I spend more time now in the garden.
      Looking at the problem this link is good, https://www.brian-lambert.co.uk/DCC_Page_2.html
      go down to where it says DCC & Turntables look at the sequence drawing and how the ring changes the polarity. Now one easy way would be to use copper tape and make some pickups. This would mean no programming and no relay control.
      If you are to use relays when when the arduino gets to a specific step location as shown in there diagram. A relay can operate to change over the polarity, One issue with this having no pick ups the cables will get twisted.

Leave a Reply

%d bloggers like this: