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.


18 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.


Leave a Reply

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