JMRI Arduino Turnout setup

In this blog I will be setting up a turnout on my layout which will be controlled by an Arduino using JMRI PanelPro.

The first thing I will need to do is setup the servo with an Arduino sketch so that I know the values to put into the servo throw command and the servo close command. Below is a simple sketch that will read the keyboard input with the Arduino Serial monitor and if the key input is "t" the servo will throw the turnout and if the input is "c" it will close the turnout. I can then adjust the servoWrite commands by 1 each time until the turnout has been moved enough to make a good connection with the running rails and not derail the train.

Below is the setup code for the Arduino which I run with the servo connected to one of my turnouts on my layout. Once I got the values for throw and close I could place these into the Arduino code for panelPro as shown in the previous blog.

#include <Servo.h>;
Servo myservo;

void setup(){
   myservo.attach(9);
   myservo.write(2);
   Serial.begin(9600);
}

void loop (){
   char inChar = (char)Serial.read();
   if (inChar == 't'){
      myservo.write(10);
      myservo.write(9);
      Serial.println ("Turnout Thrown");
   }
   if (inChar == 'c'){
      myservo.write(2);
      Serial.println ("Turnout Closed");
   }
}

You may have noticed I have two myservo.write commands for the turnout throw. This was due to the constant buzzing then trowing the turnout. myservo.write(10) is needed to get the points to throw over, I then back this off by 1 with myservo.write(9) which stops the buzzing but keeps the points in a good position to not derail the train.

Below is the Arduino code with the new values for panelPro JMRI to use.

#include <CMRI.h>
#include <Auto485.h>
#include <Servo.h>

#define CMRI_ADDR 1

#define DE_PIN 2
int turnout1 = 0;

Auto485 bus(DE_PIN); // Arduino pin 2 -> MAX485 DE and RE pins
CMRI cmri(CMRI_ADDR, 24, 48, bus); // defaults to a SMINI with address 0. SMINI = 24 inputs, 48 outputs
Servo turnOut1;

void setup() {
  turnOut1.attach(9);
  turnOut1.write(2);
  bus.begin(9600);
}

void loop() {

   cmri.process();
   turnout1 = (cmri.get_bit(47));
   if (turnout1 == 1){
      turnOut1.write(10);
      turnOut1.write(9);
   }
   else{
      turnOut1.write(2);
   }
}

1 Comment

  1. Darren Williams says: Reply

    Thank you Motorhomer for your videos I am using you processes on my layout and I look forward to any more ideas you have working with the Arduino!

Leave a Reply