Current Sensing and Occupancy Detection for DCC

I’ve had several projects going on more or less simultaneously over the last 6 weeks in connection with the build-out of the 1st module of the L&NC. One project has been to further refine block occupancy detection in DCC to achieve 1 mA detection sensitivity—the sensitivity needed to detect rolling stock equipped with a 10kΩ resistor wheel set. High sensitivity occupancy detection will be especially useful in the Red Bluffs yard part of the layout I’m working on right now

This part of the L&NC is a Yard/Staging facility with a roundhouse & turntable, so it has lots of blocks

Commercial block occupancy detection systems for DCC typically use current transformers to sense current draw from locomotives or resistor wheelsets. The sensors are usually attached to stand-alone logic boards that turn outputs on or off to signal occupancy detection. Typically, these outputs are used to directly power signals. The December 2016 issue of Model Railroader Magazine included an article (Build a signal system with Arduino micro-controllers) where the author connected the outputs of RRCir-Kits BOD boards to an Arduino Mega, and ran his signals from there.

I’ve been avoiding solutions like that because of the high cost and inflexibility of commercial detection systems. All I need is a way to sense very low current flow; the Arduino can do all the rest of the work.Thinking about the issue further, I wondered why not just use current transformers as sensors that can be directly read by an Arduino?

Finding Experimental Sensors

Current Transformer

The type of current transformer useful in model railroading are designed to allow you to wrap a wire (e.g. track feeders) around it through the central hole making the wire the “primary” coil of the transformer. The secondary coil produces an A/C current, that you can measure, proportional to the A/C current running through the primary wire.

I had seen little about using CT sensors with Arduino until I bumped into a seller on Amazon of boards with current transformers marketing them as Gikfun DIY 5A Range AC Current Transformer Module for Arduino. The assembled boards were about $7.50 each, a bit expensive, but then I found a 5 sensor unassembled kit from the same vendor for just under $9. Less than $2 per sensor is my price point for BOD sensors, so I bought the kit to see what i could do with it.

Gikfun Current Transformer Kit

The kit consisted five current transformers, five 82Ω resistors, connector pins and five 19mm x 19mm pc boards on a break-apart strip, drilled and traced for the parts. The resistor is placed across the transformer leads, placing a load (I’ve seen it referred to as a “burden”) on the transformer enabling current flow. No instructions on how to use the sensor were anywhere to be found.

So I went with the methods I already know work with ACS712 sensors. I set up some feeders from the DCC system to my cleaning track, wrapping one of the feeders around the CT coil three times. I hooked leads from the sensor board to pin 0 and ground of my Mayhhew Labs Extended ADC Shield, then threw together a test sketch with the current reading utilities I’d previously used (adapted for the shield).

A Simple CT Sensor Test Rig

I “let her rip” and watched the readings go by on the serial monitor.

Oh My! Putting a DCC equipped locomotive on the track and turning track power on, I was rewarded with a clear reading even though the decoder was idling and all lights were off.  Bring up the lights and the readings jumped up appropriately. Then, taking the loco off the track, I tried shorting the track with a 10kΩ resistor: an immediate and clear response in the readings told me this was going to work.

Test readings in single ended mode using a Mayhew Labs 14 bit ADC Shield. Everything from a 10k resistor shorting the track, to different stages of locomotive operation are clearly shown by the sensor outputs.

I tried it again, this time connecting the CT sensor to A0 and ground on my UNO, and got different but workable results. Amazing!

The readings through the UNO’s built-in ADC are similar, especially at the low end. But notice that the scaling, which is spot-on correct in the first example, fails at higher current levels here. Evidently, the greater bit depth of the Mayhew Labs Shield (14 bits) vs the built-in ADC’s (10 bits) makes a difference where absolute accuracy is concerned. Our purposes are much cruder — we just want to know when a minimum current (1 mA) is flowing. For that purpose direct-to-UNO works fine.

Testing current sensing with and InterMountain EMD F3A

Wait! This Shouldn’t Work!

You see, I was using the ADC in “single-ended”, “uni-polar” mode so that it would read a single input in the range of 0 to +5 volts; this is the same way Arduino analog inputs are read by the built-in ADC. However, the signal from the CT is an A/C signal that the ADC (in single-ended, uni-polar mode) will not be able to read during the negative voltage part of the cycle. Nevertheless, it works with both the built-in and the external ADC, even though it shouldn’t.

I was puzzling as to why, apparently, I was able to cheat when it occurred to me that the difference from ordinary applications was probably the frequency of the signal. Ordinary A/C is a 60 Hz cycle (60 cycles per second).  That is slow compared to the rate at which an Arduino ADC can sample, so a lot of samples would be at 0 during negative phase of the cycle. This would throw calculations off.

DCC is an 8kHz  (8000 cycles per second) cycle which is very fast. My Mayhew Labs ADC can sample at a maximum rate of 3kHz, which is somewhat faster than the Arduino built-in ADC but still slower than the DCC cycle. So even at the fastest rate every ADC read will span multiple DCC cycles, resulting in the ADC returning a positive reading every time.

Or something along those lines is going on.

Mayhew Labs Extended ADC Shield on an UNO, reading the test CT sensor in differential mode.

For hard core accuracy, a CT should be read in differential, bi-polar (+/- voltage) mode, requiring a 14 or 16 bit external ADC with that capability. In differential mode, each CT lead is attached to an ADC port, then the two ports are compared and their difference reported. This effectively captures the entire A/C cycle. Read in differential mode, CT sensors produce stable, high accuracy readings. During testing with my 14 bit ADC I was able to sense down to near 400µA in differential mode, the draw of a 20kΩ resistor at 11 volts. Now that is sensitive!

Same test sequence using the Mayhew ADC to read the sensor in differential mode. Notice the increased accuracy, stability and sensitivity.

Next Steps

The Gikfun 5 sensor kit is a viable sensor package where you will be watching no more than a few (up to the 6 or 8 analog ports on your Arduino or Mayhew Labs ADC shield) blocks. For small installations, buy these sensors and use the ADC in your favorite Arduino board to read them.

For larger installations, the external ADC shield is worth the cost because it is fast and more accurate.

But what if you are watching more blocks than you have analog ports for? That is a bit of a problem, especially if you don’t want to have to use a new microcontroller for every 8 blocks. The module I’m working with has this problem, because it is a yard zone (with turntable and roundhouse) with 24 blocks, and I want just one Arduino board to listen to them all and run signals for this part of the layout.

In the next post, I’ll go into a solution to the problem and install the system.


My experiment was complicated by the fact that I was testing different ADC’s and different connection methods. Below is some basic code for testing a CT sensor such as the Gikfun units discussed above by directly attaching one lead to an Arduino analog port, and the other lead to ground. This is the simplest way to use CT sensors.

The code is built around current sensing techniques originally developed for ACS712 sensors. For more in-depth discussion of that development process, see these posts – 1 , 2, 3 and 4. ADC’s (analog-to-digital converters) read the voltage produced by a sensor which, in the case both current transformers and ACS712 sensors, is proportional to the current it is measuring.

The challenge with sensing alternating current comes from the fact that at any given moment when a sensor is read, the current could be anywhere in its cycle with a voltage somewhere between -Vmax and +Vmax (the nominal A/C voltage). Since we are sampling a bipolar wave form, it is necessary to take multiple samples spanning multiple cycles, then calculate the current from the samples using the Root Mean Square algorithm. This is the generally accepted method for determining A/C voltage and current from digital samples.

In addition to solving the basic problems of sampling an A/C signal, the RMS algorithm cuts through signal and sampling noise, a significant problem with ACS712 sensors when measuring low current. Since CT sensors are far less noisy, we can get accurate readings with fewer samples; but we still need the RMS method to calculate current flow.

To make the process I’ve described accurate across an array of sensors, I developed a calibration routine. All ADC’s will produce some minimal reading for a sensor even when the sensor is not producing a signal. This is a form of noise that has to be filtered out. Also, some sensors (such as the ACS712) produce a signal in the absence of any current to measure — this too is noise that has to be filtered.

Each ADC port / sensor combination will produce a unique amount of noise. The purpose of the calibration routine is to measure and record the noise level — I call it ADC Zero — for each port/sensor. This is recorded both as raw mV, and as a calculated current in mA. Later, the measured noise is subtracted from each raw reading (in mV) to determine what current (if any) has been detected. Then, when current is detected, I use a multiple of the ADC Zero calculated current to establish an occupancy threshold. This method allows me to overcome sensor differences and manage the sensor system in a uniform way.

// CT Sensor test
// Using UNO built-in ADC to read the sensor

#define VERSION "1.006"
#define SYS_ID "CT Sensor Test - Direct to UNO ADC"
const int adcpin = 0;

// Sampling Parameters
const unsigned long sampleTime = 2000UL; 
const unsigned long numSamples = 100UL; 
const unsigned long sampleInterval = sampleTime/numSamples; 

#define SENSITIVITY 5000

// variables to hold sensor quiescent readings
float aqv;  // Average Quiescent Voltage; e.g. ADC Zero
float aqc;  // Average Quiescent Current; 

void setup()
  Serial.println(String(F(SYS_ID)) + String(F(" - SW:")) + String(F(VERSION)));
  Serial.print("\nCalibrating the sensor at pin ");
  aqv = determineVQ(adcpin); 
  Serial.print("AQV: ");
  Serial.print(aqv * 1000, 4);
  Serial.print(" mV\t");
  aqc = determineCQ(adcpin, aqv);
  Serial.print("AQC: ");
  Serial.print(aqc * 1000, 4);
  Serial.print(" mA\t");
  float sense = (aqc * DETECTION_MULTIPLIER) - aqc;
  Serial.print("Detection Sensitivity: ");
  Serial.print(sense * 1000, 3);
  Serial.println(" mA\n\n");

void loop(){
  float current = readCurrent(adcpin, aqv);
  float delta = abs(aqc - current);
  bool occupied = delta > ((aqc * DETECTION_MULTIPLIER) - aqc);
  Serial.print("Current Sensed: ");
  Serial.print(current * 1000,3);
  Serial.print(" mA\t");
  } else {
    Serial.println("Not occupied");

// Current Sensor Functions
float readCurrent(int pin, float adc_zero)
  float currentAcc = 0;
  unsigned int count = 0;
  unsigned long prevMicros = micros() - sampleInterval ;
  while (count < numSamples)
    if (micros() - prevMicros >= sampleInterval)
      float adc_raw = (float) analogRead(pin) - adc_zero; // sensor reading in volts
      adc_raw /= SENSITIVITY; // convert to amperes
      currentAcc += (adc_raw * adc_raw); // sum the squares
      prevMicros += sampleInterval;
  float rms = sqrt((float)currentAcc / (float)numSamples);
  return rms;

// Calibration
// Track Power must be OFF during calibration

float determineVQ(int pin) {
  float VQ = 0;
  //read a large number of samples to stabilize value
  for (int i = 0; i < CALIBRATION_READS; i++) {
    VQ += analogRead(pin);
  return VQ;

float determineCQ(int pin, float aqv) {
  float CQ = 0;
  // set reps so the total actual analog reads == CALIBRATION_READS
  int reps = (CALIBRATION_READS / numSamples);
  for (int i = 0; i < reps; i++) {
    CQ += readCurrent(pin, aqv);
  CQ /= reps;
  return CQ;

Adjusting the Sketch

The SENSITIVITY and DETECTION_MULTIPLIER constants are the main values you can manipulate to adjust the sketch.  The SENSITIVITY constant defines the conversion from raw mV to sensed current in mA; raise that number to reduce the current reading and decrease dynamic range of the sensor readings (lower it to do the reverse).  The DETECTION_MULTIPLIER determines when a current reading is high enough to mean that a block is occupied. Try different values for these items and see what happens. The interaction between these two constants is the primary determinant of how the sketch performs.

If you have having stability trouble with your readings, try increasing the number of samples taken on each pass (numSamples) and/or changing the sampleTime variable that controls the interval between samples.

Bear in mind that for low current sensing, the 10-bit ADC in an UNO is just barely sensitive enough to work with a CT. For low current sensing you’ll need to adjust the variables to get a minimum of false readings.  The values given in the sketch above are what worked for me with the Gikfun CT coil and a particular UNO to achieve stable detection.  Expect different readings and different variable values for different brands of CT coils, and different Arduino boards. Working with large numbers of CT sensors, I can say that a given Arduino board and a given brand/model CT, using the same value for the load resistor, will generally produce the same readings.


58 thoughts on “Current Sensing and Occupancy Detection for DCC”

  1. I am trying to replicate your detector. Can you share the full arduino code for this project?

  2. If you want more accurate results that will cover “zero-stretching” for DC locomotives, just add a low-power Schottkey bridge rectifier on the output of the CT. That will rectify the square wave and provide a nice stable output for either ADC.

    • I have to admit I gave up on running DC locos with DCC “zero-stretching” because the performance is poor (at least as far as N scale equipment goes). It helps that I don’t have a fleet of older DC locos that are hard to convert to DCC. I suspect quite a few do use DC locos with their DCC systems from time to time so thanks for the suggestion.

  3. About the arduino UNO sensitivity, you did’nt mention the analogReference function using either internal (1.1V) or external (VREF input) parameter.

    Does it give a better detection capability directly from the UNO board?

    • That would have the effect of remapping the range of the ADC, but I don’t think it solves any problems. When using current sensors, the lack of inherent noise means you only need minimal sensitivity to be able to detect activity. The goal is not to get an accurate current reading; the goal is to determine if any current is flowing at all. So I guess the answer is “maybe,” but its probably not worth the effort.

      Best, Rob

  4. Ok, Rob,

    And following this goal, would’nt be more effective to replace the RMS calculation, by a more simple , and faster, check to find an analog value for exemple 2 times higher than the “inoccupied RMS value” triggering an exit of the function. When the block is occupied, this event will happen very quickly as statistivally , the sinus function in this case is almost always above this minimum check value. So the check will last a lot less than 100 calculation cycle?


    • Sure, why not. Like everything, there is more than one way to achieve a given end. The advantage of my method is that it works across an array of hardware. Hardware dependent solutions such as you suggest are fine, but not consistent with my goals.

  5. Here is what the function isOccupied(), which replace the readCurrent one in your project, will be
    the result is directly the block occupation value
    As long as the block is occupied, only a few measures will trigger the exit.
    The worst case (except the empty block will be the 10K resistor as the current level is the smallest and hence the sinus value is not so often above my threshold value)

    Does this method work across an array of hardware ?

    Best, JC

    unsigned int isOccupied(int pin, float adc_zero)
    unsigned int occupied = 0;
    unsigned int count = 0;

    while (count 2 * adc_zero { // 2 is my threshold multiplier
    occupied = 1;
    return occupied;

  6. Excuse me there was an error in the function !!

    unsigned int isOccupied(int pin, float adc_zero)
    unsigned int occupied = 0;
    unsigned int count = 0;

    while (count 2 * adc_zero { // 2 is my threshold multiplier
    occupied = 1;
    return occupied;

  7. I hope this is the right one !!!

    unsigned int isOccupied(int pin, float adc_zero)
    unsigned int occupied = 0;
    unsigned int count = 0;

    while (count 2 2*adc_zero) {
    // 2 is my threshold multiplier
    occupied = 1;
    return occupied;

    • I see where we are missing each other.

      No question you method will work. What it does not do is reject noise. Hardware dependency has to do with the noise a particular hardware combination will produce; when using different hardware combinations, noise levels can vary making it difficult to use a single coding solution.

      All ADCs produce noise. Noise is most evident at detection thresholds, especially at the limit of the bit depth of the ADC.

      All current sensors produce noise. The beauty of CT coils is that their signal is strong and only noisy at detection limits. I hit those limits when trying to detect a 1 ma flow from a single 10k resistor wheel.

      The RMS algorithm is the classical way to deal with bipolar signals and noise they contain. That’s the reason to use it.

      If your layout uses all the same gear (same board models and sensor parts), you could build a unified approach omitting the RMS calculation. If you vary the gear or the environment (boards, ADCs, sensors, sensor density, and so on) it is very difficult to create a consistent detection mechanism that works across the system without noise processing.

      Whether you need noise processing or not is entirely dependent on all your choices and things like electronics density and EMI. You may not need it. I definitely do. Making noise processing the default choice simply reflects my practical experience to date, and it allows me to plug the same detection code into every part of the layout. You may choose differently; and if it works well, all the better.



  8. First, I have now read all your articles (learning curve) on block detection and find it very interesting. I am still a way off completing my OO layout but envisage I will need 40 block detectors, as I intend to use them for auto braking of multiple trains on one circuit. I looked into this a few years ago and settled on making my own diode detection circuits (as opposed to transformer) due to them being cheaper than I could find appropriate coils. The source you have found (including peripheral parts is incredibly cheap!). I have since bit the bullet and started playing with Arduino’s, as I need them to automate animatronics that I am building.

    Car Lift
    All the rest

    The control of signals were to be via relays on each board and would have meant 3 relays per block and a 7 wire interconnect between each block!
    I now see that using a Arduino would make life simpler, as I (imagine) that I can use comms (SPI) to talk between blocks (each block would have a mini pro) and transpose the signals across to my mimic. See bottom of page of
    So it lead to the thought (when I was discussing home grown detector circuits with another member on a forum (NRM) that an Arduino should be capable of doing the detection itself and landed me on your site during my research.
    Second I would like to thank you for sharing your investigations and would hope I can throw in some idea’s (although a complete novice in C++, I am an industrial controls system design engineer)
    Would the Arduino be fast enough to ‘see’ the positive half level of DCC through a voltage divider and thus only sample during that period (i.e. command the time at which the ADC takes its samples?
    Why can you not simply full wave rectify the output of the transformer, thus the input to the Arduino would effectively not be AC? No need for RMS calc.

    • Your animatronics are fun.

      I haven’t thought about trying to just read the positive phase through a voltage divider. Using the built-in ADC, your main problem would be timing. DCC frequency is about 8kHz, while the fastest theoretical sampling rate on a 16 Mhz Arduino is about 9.6 kHz. It would be a struggle to reliably catch the positive cycles without the help of a hyper-fast ADC like the Mayhew Labs unit I use that has a much higher sampling rate.

      Others have suggested rectifying the output of the CT coils — that certainly helps with the built-in ADC, so its worth a try to see if it improves native detection.

      The RMS calculation does consume cycles, so people frequently want to find a way around that. I have found that it pays to use it even though it could be skipped. The reason is that there is still noise in the system from normal operation of trains, dirty track and other electrical hiccups. With CT sensors the RMS calculation serves to desensitize the system so that it does not respond to momentary anomalies. You can reduce the number of samples you use for the calculation significantly, since the output of the CTs is so stable, and regain some cycles that way.


  9. Hello,
    I have been trying to utilise your solution, but without success. I believe we have replicated both your code and circuit exactly. But we calibrate the section, turn on the track power and all we get is ‘occupied’ responses.
    We have tried with and without resistors at the coil, played with the sensitivity detection multiplier etc but do not get the sort of results you have published.
    To be sure could you please send the latest code and also a clear picture of your circuit.
    Your help will be most appreciated.

    • Assuming there isn’t a circuit error, the occupied response should mean that either the calibration is being applied incorrectly so you aren’t establishing a zero point or that there really is a current leak of some sort that the system is detecting. Try this: with all locos and rolling stock removed, turn on track power then start the calibration. If after calibration it now works correctly, you have a current leak somewhere and the calibration effectively masks it out.

      If that does not work, we’ll step through it until we find the problem.

      First and foremost: Are you trying to replicate the test rig — one block with one coil? If you are using the code posted on the site, you should be able to get some serial output from testing that would be helpful in understand what is going on.

      I’ll send you ping you by email you have my address and we can pursue this off-line.


  10. I am not an electrical engineer so excuse my questions.

    1) I read all all 5 of your posts but when I got to the 5th, I was confused. I believe that you stated that the Mayhew Labs Extended ADC Shield is not required. That the block occupancy detection will work with the AC current transformer will function correctly directly connected to the Arduino Uno. Is my understanding correct?
    2) If I am incorrect on #1, it looks like the Mayhew Labs Extended ADC Shield is not available anymore. If a shield is need, what would be an equivalent replacement?
    3) If the block occupancy works correctly, can occupancy detection be used to trigger a servo to throw a turnout that is not correctly aligned when a train approaches the turnout from the frog end. The assumption here is the block detection here is located immediately before the switch requiring a point change. Some devices on the market call this function “Auto Throw.”
    4) Can an Arduino Uno power a DPDT relay to reroute track power like a Tortoise switch machine?
    5) Finally can all of the design functions work with JMRI?
    6) My last question is based on your professional experience. Do you think that a person who is not an electrical engineer should attempt a project as electrically complicated as you have outlined? The reason I have asked is that nearly all the comments your audience has submitted appear to have been made by individuals who have electrical or electronic experience. I would prefer not going down the road of sophistication and have to give up the challenge because I am overwhelmed.

    • In order of your questions:

      1) The Mayhew shield is NOT required. It took me a while to reach that conclusion, but that’s part of the fun! The trick to getting a stable, strong signal is putting the right resistor across the CT terminals. The math is messy, but for a CT coil with 100 windings (type I use, and readily available) the correct resistor value is around 960 ohms, so I use 1k Ohm resistors.
      2) There are quit a few external ADC boards on the market compatible with Arduino. A good place to look would be or ebay. The inexpensive ones are generally a little on the slow side. The beauty of the Mayhew Labs board is its blazing speed.
      3) Absolutely. That’s just a software problem — the simplest approach is to set the turnout position as necessary whenever the adjoining block is occupied. You can also set track polarity based on block occupancy and turnout position (see next answer).
      4) YES BUT: most relay coils draw more power than an Arduino pin can handle. If you look at the data sheet for your chosen relay, you’ll likely find the coils draw more current than the 40 mA Arduino pins can supply. So, what you need is an intermediate switching device that allows a tiny current to control a bigger one — you need a transistor to allow the Arduino to switch the coils. You’ll want to use and NPN transistor on the SINK (ground) side of the coil circuit — a BC547 would do the job. Connect the SOURCE side of the coil directly to your power supply. There’s lots of instructables on the web on this subject. For higher loads or handling multiple sink side circuits, use a transistor array such as a Darlington Array (search this blog for more info on Darlington arrays).
      5) I don’t have any specific advice on working with JMRI because I haven’t done it yet.
      6) I’m not an electrical engineer either, but I can do math and learn fast. Really if you can do basic algebra you can solve all the problems, and there are free calculators all over the web. Mostly you’ll use Ohm’s law (current = voltage / resistance) to determine the resistance necessary to limit current flow to a device so it doesn’t overload (the most common problem you’ll encounter).

      Start with basic Arduino circuit tutorials (get an UNO mounted on a breadboard if you don’t already have one) to bring yourself up to speed on the basics. From that point the layout stuff will seem a lot easier. Even the most complex circuits are really just a collection of small circuits chained together. So start with controlling one turnout and detecting occupancy on the three adjoining blocks; add signals to make it more interesting.

      Then as they say, rinse and repeat. Pretty soon your layout underside will be covered with electronics and you’ll wonder how you managed it.

      It helps that kind of things we are doing with layouts are very safe because its low voltage, low current stuff. The most dangerous thing is your soldering iron.

      Go for it and only give up if you aren’t having fun.

      Best, Rob

  11. Hi,
    I’m just getting into Arduino based detection and I implemented your sketch and it worked as your test showed. I did have a problem with false readings once the locomotive cleared the block. I tried changing sampleTime & numSamples values but it didn’t seem to make a difference. The values for not occupied blocks range from .016 to .248 during my test and are interspersed among the ‘not occupied’ lines.
    I hope you are still monitoring this site.

    • Hi Gordon,
      Noise is a recurring problem with BOD. However, with CT coils noise should be fairly trivial and should not cause false occupancy readings. If not, there are some issues to consider and deal with.

      1. Wiring. First step is to recheck all under-layout wiring related to block feeders and detection. Because DCC is an AC signal, it can cause inductive responses on adjacent feeders. Feeders that cross over or wrap around each other are the most susceptible to inductive responses. Also, multiple CT coils should not be placed in close (less than 1″ clearance) proximity to each other or they will inductively affect each other.

      2. Assuming all is well with wiring, then you may need to change the SENSITIVITY factor, and change the value you use to detect occupancy. The “detection threshold” in the original sketch is often too sensitive. Since this post was written I’ve been working on refining techniques and have moved to defining the occupancy threshold in terms of milliamps. I should have a new post on the subject up soon.

      Here’s what I recommend you try: Make sure you know the voltage your DCC command station/booster puts out (on a Digitrax system, its 15 volts in HO mode — which I use because many N sound Loco’s need HO voltage to function right). Then, using an Ohm’s Law calculator (like this, you would determine that a 10k resistor will pass 1.5 milliamps of current at that voltage. Now, modify the SENSITIVITY factor up or down until your detection code produces a reading of .0015 when a 10k resistor (use a .1% type for accuracy) is placed across the rails. That should be your absolute minimum detection threshold. Try every block: you should get a nearly identical reading every time (10% is probably the limit of acceptable variance).

      With your SENSITIVITY adjusted and your detection threshold set to .0015, noise should fall below that point and become a non-issue. If it does not, look for current leaks.

      I will say that I am tracking a peculiar issue with my Digitrax DCC. If the command station is powered, even if the track is “off-line” and a loco would be dark, I see a surprising amount of low level noise. Unplug the Command station and it all goes away.

      I’d be curious to know if you are seeing anything like that as well.

      Best, R

      • Thank you. I am using a NCE system and will check the output voltage. I’m only using a test track and using an NCE BD20 directly to the Uno R3. As I said I’m not electrical engineering based but at startup the block shows no occupied for a long time. Put the engine on the track and it detects but remove it and the noise starts. I’ll let you know what happens when I can get back to the project, maybe tomorrow.
        Thanks for the reply

        • Hmm. So noise starts when you remove the engine. What happens if you put the loco back on the track? If you shut down then restart your DCC, does the noise come back or does it stay quiet until you next put a loco on the track?

          • The scenario repeats itself when the system is shut off. I hope this isn’t to much data but here is the serial monitor log. The first occupied is when I put the engine on the track, then moved it a bit, removed it and the not occupied appeared, then 4 occupied and 3 not occupied, engine on, engine off and then not back on again. Interesting. I haven’t done your sensitivity adjustment yet but will work on it.
            (It would be nice to know how I can know when you respond since I get no email notice that you have. I have to check.

            CT Sensor Test – Direct to UNO ADC – SW:1.006

            Calibrating the sensor at pin 0
            AQV: 0.0000 mV AQC: 0.0000 mA Detection Sensitivity: 0.000 mA

            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.131 mA Occupied
            Current Sensed: 0.368 mA Occupied
            Current Sensed: 0.199 mA Occupied
            Current Sensed: 0.414 mA Occupied
            Current Sensed: 0.639 mA Occupied
            Current Sensed: 0.407 mA Occupied
            Current Sensed: 0.572 mA Occupied
            Current Sensed: 0.340 mA Occupied
            Current Sensed: 0.514 mA Occupied
            Current Sensed: 0.327 mA Occupied
            Current Sensed: 0.622 mA Occupied
            Current Sensed: 0.552 mA Occupied
            Current Sensed: 0.205 mA Occupied
            Current Sensed: 12.021 mA Occupied
            Current Sensed: 13.055 mA Occupied
            Current Sensed: 16.369 mA Occupied
            Current Sensed: 16.318 mA Occupied
            Current Sensed: 0.418 mA Occupied
            Current Sensed: 0.209 mA Occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.033 mA Occupied
            Current Sensed: 0.147 mA Occupied
            Current Sensed: 0.065 mA Occupied
            Current Sensed: 0.083 mA Occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.472 mA Occupied
            Current Sensed: 0.524 mA Occupied
            Current Sensed: 0.197 mA Occupied
            Current Sensed: 0.658 mA Occupied
            Current Sensed: 0.389 mA Occupied
            Current Sensed: 0.725 mA Occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.114 mA Occupied
            Current Sensed: 0.049 mA Occupied
            Current Sensed: 0.131 mA Occupied
            Current Sensed: 0.049 mA Occupied
            Current Sensed: 0.000 mA Not occupied
            Current Sensed: 0.147 mA Occupied
            Current Sensed: 0.065 mA Occupied
            Current Sensed: 0.065 mA Occupied
            Current Sensed: 0.163 mA Occupied
            Current Sensed: 0.000 mA Not occupied

          • From the readout, the biggest problem is that the detection threshold is way too low, assuming the scaling is correct. There is always some noise so you cannot rely on “0” in that way. While it is curious that the noise picks up after you remove the loco ( I do have thoughts on that ), they are mostly low enough that you should not be registering occupancy.

            I’ll need to know details about what CT coil you are using and how it is connected to your UNO (what pins are you using). I’ll also need to look at your sketch. I’ll ping you by email in a little while and you can reply back with more detailed information and the sketch attached as an INO file.

          • Also, when I change the Sensitivity value the calibrated AQV and AQC values are still zero.

          • That is not the point at all. You are striving to get a reading of 1.5 mA when a .01% 10K Ohms resistor is placed across the rails. The ideal is a consistent reading of .0015 AMPS; then set that as your detection threshold. Since the reading with fluctuate, you’ll actually want to adjust the sensitivity so the reading is slightly higher — .0016 instead of .0015, then make .0015 the detection threshold.

            If you cannot make that adjustment, then the code is incorrect in some way.

          • I thought I posted the Serial Monitor log to show you what’s happening. Guess it didn’t come thru. I’ll do post another.

          • This is a moderated site so postings do not come through without approval. Sometimes I don’t get to the moderation immediately.

  12. Hi Robin,
    Thankfully I stumbled upon your blog again after having lost the URL. I am considering having block detection on my layout and found your posts very interesting. I decided not to consider Hall effect detectors as they will use up some of the voltage. I like the current transformers as they don’t rob the track power. What I would like to know is if you had considered the small current transformer sensors for Arduinos that are readily available on EBay such as this:

    Sorry for the long string. It is a small module with a CT and a variable resistor to adjust sensitivity. It is also called: “5A Range Sensor ZMCT103C Single Phase AC Active Micro Current Transformer Module”. I was wondering if it is sensitive enough to detect N-scale locos or lighted cars. I plan on using a constant lighting unit in my cabooses so that I can detect full freight trains and lighted passenger cars. Not too concerned about detecting every freight car and certainly don’t want to apply resistors to each freight truck.

    Thanks in advance for any advice you can give,

    • Hi Rob, Sorry for the delay.

      The trick with the CT sensors is getting the resistor right. A variable resistor is a good solution. The value I use is 910 ohms. 1k works though it tends to be oversensitive. If you get too much noise with that, a parallel tantalum .1uf cap will calm it down.

      Best, Rob

  13. Hi
    I’m trying to replicate your technique. I have the Gikfun CT’s and have it wired up as you suggest. Sadly though I can’t get it to register and idling loco. Only when it’s moving.

    I tried a couple of other simpler sketches to identify if the analog pin is ever registering any value above 0 – but unless I move the loco, it never read anything at all.

    I have elegoo UNO’s and a MEGA, all the same result. Is there anything I can try or could this be down to the DAC in the Elegoo?

    • Hi.

      I’ve refined my techniques a bit since this post was written. Actually, you are not far off if you can register the loco in motion.

      The issue is sensitivity — and sensitivity is controlled by the resistor across the terminals of the CT. The Gikfun CT comes with a 84 ohm (or so) resistor, which is fine if your minimum detection requirement is around 1/2 amp (conveniently, about what an N or HO loco will draw in motion with some rolling stock behind it).

      To achieve the necessary sensitivity for detecting idling locos or resistor wheels on rolling stock, the resistor has to be a larger value. Basically, the more ohms of resistance you have, the smaller the increment of detectable current.

      There is a math formula for this — the correct value will be between 800 and 1k ohms. I run my N scale layout at HO voltage 15 volts (traditional N voltage of 11 volts just doesn’t support the sound locos I own), and I use the value 910 ohms. Try a 1k, which will be easier to get your hands on — it may be too sensitive, but that is easy to deal with on the software side; for really persistent noisiness caused by over-sensitivity, a .1μf polarized capacitor (tantalum) parallel to the resistor will chill it nicely. Note that with the cap, you have to pay attention to polarity when using the built-in ADC — positive pole must be connected to an analog port.

      Let me know how you do with it.

      Best, Rob

      • okeydokey so I tried with a 1k resistor as I had one handy.

        For now, haven’t altered your example sketch. Now it registers ~8mA when track power is off, and floats around 35 when powered. But it doesn’t register the loco at all, even when at full tilt. Do you think this is this due to the noise? Don’t have a cap handy to test.

        Incidentally, before I got your reply I tried the 84 ohm resistor with a esp8266 board I happened to have and was able to get that to register the idling loco. Do you think that’s down to the ADC?

        • Hm. It does sound like you may be dealing with a faulty adc.

          When the track power is off, the ADC should register 0 consistently so long as the sensor circuit is complete — that is, one leg of the sensor is attached to an analog port, the other is attached to ground. If the ADC reads a port without a complete connection, a random non-zero reading will result.

          So the first goal, is to get 0 on the system with power off. If you get it on the ESP but not the UNO or Mega, then its an ADC or electrical problem. The cap only works if you get 0 when power is off, but when power is on you see noise.

          Another possibility — and probably more likely than the faulty ADC theory — is that you have a problem with the power supply you are using for the UNO or Mega. Try different power supplies or try USB. If USB power is unstable, try changing the cable. This is actually the most common problem I see with trying to implement CT sensors.


          • Thanks for the help. Weirdly, I can’t get the ESP to read 0 under any circumstances. But it still does have enough discrepancy to sense an idle loco if I tweak the sketch.

            I do seem to be getting more sensible results out of the mega now. I find it much easier to get reliable discrimination if i go up to a 2.2 k resistor though. Is there any problem with that? And it’s certainly noisy. Gonna order some of those caps.

          • 2.2k will probably be too high. But give it a try.

            Are you using DCC? Have you tried it with DCC unpowered? You should be able to read zeros when there is no power at all in the track system. If the DCC system is ON but offline, you might still get a signal.

            I’ll try to email some updated detect code to you tomorrow to see if that helps too.

  14. Thanks for visiting. Sorry for the delayed response.

    Try a bigger resistor across the CT. I use 910 ohms. 1k will work though it may be oversensitive. See additional comments here about that issue.


    • I should also have mentioned, that getting all 0’s on calibration with track power off is exactly right; if there were any problem with the sensor connections, you would get random numbers even through track power is off.

  15. what a great thread this is. I have started tinkering with a CT coil with 1k resistors & three loops wrapped round. I have a sketch continuously summing the last 50 readings & displaying it on a small OLED display. This is all on a DCC controlled n-gauge railway.

    The live display is showing me that I can detect locos idling (just) and easily detect moving locos, but I also use ABC braking, and once I activate a braking module I detect nothing….

    • Hmm. I don’t know about ABC braking. Usually a DCC braking function would just put out a series of commands causing the locomotive speed to step down; current continues to flow but at steadily reducing level. So I have to infer that the ABC braking system is cutting off normal current flow and redirecting current through a different feed that does not pass through the coil. If that’s the case, you just need another coil to pick up that current flow.

      If not, I’m curious about what’s going on.


      • as far as I understand it the braking module reduces the amplitude of the signal of the signal to one rail, making the wave asymmetric, which is the one i’m monitoring. Basically the loco decoder detects the asymmetric nature of the wave & slows the loco to a halt. the circuit is a loop of 5 diodes wired in a loop. the dcc feed is attached at the join between two of the diodes, the track feed exits the circuit on the other side of one of the adjacent diodes. to disable the braking module, I just short the two wires at which point the decoder detects that the wave is symmetrical again & the loco starts to accelerate. I’ve also tried a sound fitted loco & still no detection – with the sound on.

        • I see; I found the wiki:, and here

          So basically, the Asymmetric DC generator reduces the amplitude by 2.8 volts. That should cause current draw to drop, but not disappear. The only thing I can think of is the location of the CT Sensor. No detection has to simply mean no current is flowing through the wire. If the sensor is on the power source side of the diode array, rather than the rail side, it ought to be detecting the current flow through the diode and the voltage will be full rail power…a stopped locomotive still draws 5 mA, well within the detection range of CT coils.

          How is your sensitivity? Can you detect a 10k resistor across live track?

  16. problem solved – it’s down to my incompetence. There are four wires from my braking module – two to the isolating relay & two for the track feed/output. I stupidly put the coil on one of the relay wires, so the wire was only active when the braking module was being bypassed. Now I can make some real progress with this….

  17. Hi Rob
    Need some help. It looks like “Gikfun DIY 5A Range AC Current Transformer Module for Arduino” is no longer available. Do you have any suggestions. Cost is not an issue.

    • There are a couple of solutions:

      1> The primary manufacturer of the types of CT coils we use is Pulse Electronics. You can get their parts internationally through In the US, is a good source for small parts orders. You want a 100 to 1 Pri/Sec winding ratio; get the “non-invasive” type. Add a burden resistor in the 900 – 1k range (I currently recommend 900 ohms as optimal for N, Z and HO gauge; gauges that run a higher voltages might do best with 1K resistor) and you will have a functional sensor.

      You may also be able to obtain the CT coils on the Chinese market through or other sources. You may also find something like the Gikfun kits there.

      2> I am almost ready to go public with a more comprehensive solution for layout control, including CT Sensor/Block feeder boards that include noise suppression and simplify things considerably. I’m looking for beta testers, so if you are feeling adventurous and want to participate, let me know.

      LCOS (The Layout Control Operating System) is a system of Arduino compatible hardware and embedded software that supports all aspects of model railroad layout control, including turnout, signal, crossing and track power management, plus layout lighting and animated features. LCOS supports multiple control panel options, from fully centralized to distributed (or both at the same time). LCOS is built from LCNs (Layout Control Nodes) that run local layout functions and can be added or subtracted at will. Further, the simplified network interface provides an easy pathway to add unique, fully custom nodes that you create, such as a turntable, transfer table or other unique layout object. Finally, all configuration and maintenance is done through the LCMN (Layout Control Mesh Network); once a node is installed and its peripheral devices connected, you’ll never need to touch it physically again (except to swap CPU’s for an optional major system upgrade) even through you might decide to wipe it and reconfigure it from scratch. Just connect your computer to the master node via USB and the configuration tool does all the work.

      More info coming soon. Rob

      • I may be interested in being one of your beta testers. Being a tester would be a huge learning path for me, the question would you be interested in me?

        I am a retiree from General Motors IT department. I supported two global applications at GM. I have an under grad degree in Information Management and a Masters in Information Systems. I am 79 years old.

        What I lack is an understanding of Electrical engineering. Also I do not have a working N Scale layout. These negative qualities may disqualify me for one of your beta testers.


        • Actually, you sound like the kind of tester I need. The system is agnostic about scale, so whatever you have will be just fine. The core intent of the system is to make it easy for people who don’t like to program or invent electrical appliances to install and configure a layout control system; essentially a semi-plug and play system that can support all common layout functions but allows for the addition of highly customized additional components.

          I will email you off list with more information.

          Best, Rob

          • Hello Rob,

            I’d like to be a beta tester as well as you and I have been communicating via email quite a few times over the last couple of years and would like to help you out in any way I can. Thanks! – Scott

  18. I think this post, and the discovery of DCC++, were the reasons I went out and bought some N-gauge models a couple of years ago. Never had any success with the current sensors though.

    …because it’s taken me until today to notice the comments about replacing the provided 82Ω resistor with a 1KΩ or 910Ω one!

    Ha, I think I’ll be cooking with gas now 😀

    • Also make sure you use a separate regulated 5v supply for your aRef pin. I found this to make all the difference.

  19. I’ve done a lot of playing & have a working solution on my railway – with a lot of help from the info in this thread I’ve got a Nano operating as a 5-signal decoder with current sensing on the 5 blocks to set the signals to danger & clear them again. The main issue I had was with DCC signals – if I spend too much time reading the analog lines I miss DCC packets. The sketch I’m using now does a few reads at a time, releasing the nano to read almost all the DCC packets – it probably misses about 1 in a thousand now. If I was to start again I think I’d use one Nano to handle the analog inputs & output the results on digital lines, to a ‘master’ Nano that handles the DCC packets & drives the signals.

    My current sketch is here:

    many thanks for all the help

    • I’m not trying to mix handling DCC and other functions on the same board. As you’ve found, its easy to miss packets if you are trying to do other things at the same time. What the situation calls for is an interrupt-based approach to signal handling. You want a circuit or device that monitors the signals and raises an interrupt when there is a packet to handle. You might take a look at DCC++ to see how that system approaches the problem.

      Cheers. R

Leave a Reply

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