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.


Code

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
#define DETECTION_MULTIPLIER 1.3
#define CALIBRATION_READS 300

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

void setup()
{
  Serial.begin(9600);
  Serial.println(String(F(SYS_ID)) + String(F(" - SW:")) + String(F(VERSION)));
  Serial.print("\nCalibrating the sensor at pin ");
  Serial.println(adcpin);
  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");
  delay(7500);
}

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");
  
  if(occupied){
    Serial.println("Occupied");
  } else {
    Serial.println("Not occupied");
  }
  delay(3000);
}

//////////////////////////////////////////
// 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
      count++;
      prevMicros += sampleInterval;
    }
  }
  //https://en.wikipedia.org/wiki/Root_mean_square
  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);
    delayMicroseconds(sampleInterval);
  }
  VQ /= CALIBRATION_READS;
  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.

 

31 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?

    Best,
    Jean-Claude

    • 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;
    break;
    }
    count++;
    }
    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;
    break;
    }
    count++;
    }
    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;
    break;
    }
    count++;
    }
    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.

      Best,

      Rob

  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 https://www.youtube.com/watch?v=074HhSkIdjc&t=3s
    All the rest https://www.youtube.com/channel/UCsbxIqSw3SrqESd80nk11lg

    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 http://www.elotion.co.uk/myelectrics.html.
    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.

      Best,
      Robin

  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.

      R.

  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 Adafruit.com 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.
    Thanks
    Gordon

    • 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 http://www.ohmslawcalculator.com/ohms-law-calculator), 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.
            Thanks

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

Leave a Reply

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