CT Occupied sensor with Arduino and JMRI

Today my CT (current transformer DL-CT1005A) has turned up which I purchased from ebay. I have an n gauge layout and have tried the ACS712 current sensors but have failed to get them to work with such low current draw, so I got this CT to try out.
Below is the connection I have made from the CT to the Arduino and the power for the track is wrapped 5 times around the CT. I have not included any further electronics as I only want to see if the track has a train on it. I am not interested in the exact current being pulled and want this to be as simple as possible. Therefore, the CT is connected directly to the Arduino GND and A1 pins.

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

#define CMRI_ADDR 1

#define DE_PIN 2
#define LED 13

Auto485 bus(DE_PIN);
CMRI cmri(CMRI_ADDR, 24, 48, bus); // defaults to a SMINI with address 0. SMINI = 24 inputs, 48 outputs

int ct = A1;
int ctReading [100];
int ctAverage;

void setup() {
 // Serial.begin(9600);
  pinMode (LED, OUTPUT);

void loop() {
  for (int i=0; i<100; i++)
      ctReading[i] = analogRead(ct);
      ctAverage = ctAverage + ctReading[i];
  int reading = ctAverage / 100; 
  //Serial.print ("reading = ");
  //Serial.println (reading);
  if (reading > 16)
    digitalWrite(LED, HIGH);
    cmri.set_bit(0, 1);
    //Serial.println ("Occupied");
    digitalWrite (LED, LOW);
    cmri.set_bit(0, 0);
   ctAverage = 0;
   delay (5);

The Arduino code is very basic. It takes a reading of the CT 100 times and then works out the average of this reading. I did this because I was getting a lot reading of zero and between high 20 to mid 30 for occupied track

Initially I had the serial communication working which has been commented out in the above code. This allowed me to see what the average reading from the CT was with the train on and off the track. I found with the train on the track I was getting anything from 27 to 36 and when off the track I was getting 1 to 5 reading. With a 10K ohm resistor across the track to simulate a coach with an occupied resistor fitted to the wheel I was getting a reading of 19 to 24. With these figures I could add the IF statement to the code that would show the track occupied if the readings where greater than 16 and not occupied if the reading was below 16. Within the IF statement I would send a bit of 1 for occupied and 0 for unoccupied to the relevant address for this part of the track. For this test it would be bit 0 of node 1 address.

With this upload to my Arduino it was time to start up PanelPro. When up and running I opened the sensor table so I could add this new sensor. The address would be 1001 and I gave it a name O1. Once created I tested it out and it worked very well, with the train or resistor on the track I would get a active in the table and with the items off the track I would get inactive.

Next, I opened a layout click on Panels > New Panel > Layout Editor. I wanted to set a piece of track that would change colour with the train occupying the track. In the editor I placed two turnouts on the layout by selecting RH check box at the top and then holding down shift and clicking on the layout. These where added so I could connect a track between then, they do not do anything else. I then clicked on the track segment check box at the top and whilst holding down the shift key click on the first turnout and drew a line to the next turnout. I now had the track and needed to assign my new sensor to this as a occupied sensor.

To do this I right clicked on the track and selected edit. You must give the block a name in this window before you can edit the block. Once I gave it a name I clicked on Create/Edit Block button to bring up the block editing window. Clicking on the sensor tab allows me to select the sensor O1 that I set up, once this was selected and applied it was all ready for testing.

Now when the track was occupied the layout track would turn RED and when unoccupied it would turn back to black. It all worked great. Next step will be to connect multiple CT sensors and see if this can work across a larger selection of track.

Leave a Reply