{"id":1080,"date":"2017-01-19T17:09:26","date_gmt":"2017-01-20T01:09:26","guid":{"rendered":"http:\/\/thenscaler.com\/?p=1080"},"modified":"2017-02-03T13:13:07","modified_gmt":"2017-02-03T21:13:07","slug":"current-sensing-and-occupancy-detection-for-dcc","status":"publish","type":"post","link":"https:\/\/thenscaler.com\/?p=1080","title":{"rendered":"Current Sensing and Occupancy Detection for DCC"},"content":{"rendered":"<p>I&#8217;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&amp;NC. One project has been to further refine block occupancy detection in DCC to achieve 1 mA detection sensitivity\u2014the sensitivity needed to detect rolling stock equipped with a 10k\u03a9 resistor wheel set. High sensitivity occupancy detection will be especially useful in the Red Bluffs yard part of the layout I&#8217;m working on right now<\/p>\n<div id=\"attachment_876\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/08\/track-plan-annotations.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-876\" class=\"size-medium wp-image-876\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/08\/track-plan-annotations-300x196.jpg\" alt=\"\" width=\"300\" height=\"196\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/08\/track-plan-annotations-300x196.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/08\/track-plan-annotations.jpg 531w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-876\" class=\"wp-caption-text\">This part of the L&amp;NC is a Yard\/Staging facility with a roundhouse &amp; turntable, so it has lots of blocks<\/p><\/div>\n<p>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 <a href=\"http:\/\/mrr.trains.com\/~\/link.aspx?_id=F182983D2E6D41B9BEDE343259E87318&amp;_z=z\" target=\"_blank\">December 2016 issue of Model Railroader Magazine<\/a> included an article (<em><strong>Build a signal system with Arduino micro-controllers<\/strong><\/em>) where the author connected the outputs of RRCir-Kits BOD boards to an Arduino Mega, and ran his signals from there.<\/p>\n<p>I&#8217;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?<\/p>\n<h4>Finding Experimental Sensors<\/h4>\n<div id=\"attachment_1071\" style=\"width: 160px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/12\/current-transformer.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1071\" class=\"size-thumbnail wp-image-1071\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/12\/current-transformer-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/12\/current-transformer-150x150.jpg 150w, https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/12\/current-transformer-50x50.jpg 50w, https:\/\/thenscaler.com\/wp-content\/uploads\/2016\/12\/current-transformer.jpg 200w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><p id=\"caption-attachment-1071\" class=\"wp-caption-text\">Current Transformer<\/p><\/div>\n<p>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 &#8220;primary&#8221; 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.<\/p>\n<p>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 <a href=\"http:\/\/amzn.to\/2jt5jJA\" target=\"_blank\"><span id=\"productTitle\" class=\"a-size-large\">Gikfun DIY 5A Range AC Current Transformer Module for Arduino<\/span><\/a>. The <a href=\"http:\/\/amzn.to\/2j7iFrU\" target=\"_blank\">assembled boards were about $7.50 each<\/a>, a bit expensive, but then I found a <a href=\"http:\/\/amzn.to\/2jqbzlX\" target=\"_blank\">5 sensor unassembled kit<\/a> 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.<\/p>\n<div id=\"attachment_1091\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Gikfun-current-transformer-kit.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1091\" class=\"size-medium wp-image-1091\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Gikfun-current-transformer-kit-300x165.jpg\" alt=\"\" width=\"300\" height=\"165\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Gikfun-current-transformer-kit-300x165.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Gikfun-current-transformer-kit-768x422.jpg 768w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Gikfun-current-transformer-kit.jpg 972w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1091\" class=\"wp-caption-text\">Gikfun Current Transformer Kit<\/p><\/div>\n<p>The kit consisted five current transformers, five 82\u03a9 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&#8217;ve seen it referred to as a &#8220;burden&#8221;) on the transformer enabling current flow. No instructions on how to use the sensor were anywhere to be found.<\/p>\n<p>So I went with the <a href=\"https:\/\/thenscaler.com\/?p=604\">methods I already know work with ACS712 sensors<\/a>. 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 <a href=\"http:\/\/amzn.to\/1VuBWnw\" target=\"_blank\">Mayhhew Labs Extended ADC Shield<\/a>, then threw together a test sketch with the current reading utilities I&#8217;d previously used (adapted for the shield).<\/p>\n<div id=\"attachment_1093\" style=\"width: 910px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-Test-setup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1093\" class=\"wp-image-1093 size-full\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-Test-setup.jpg\" width=\"900\" height=\"598\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-Test-setup.jpg 900w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-Test-setup-300x199.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-Test-setup-768x510.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><p id=\"caption-attachment-1093\" class=\"wp-caption-text\">A Simple CT Sensor Test Rig<\/p><\/div>\n<p>I &#8220;let her rip&#8221; and watched the readings go by on the serial monitor.<\/p>\n<p>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.\u00a0 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\u03a9 resistor: an immediate and clear response in the readings told me this was going to work.<\/p>\n<div id=\"attachment_1099\" style=\"width: 910px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/test-results-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1099\" class=\"size-full wp-image-1099\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/test-results-1.jpg\" alt=\"\" width=\"900\" height=\"838\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/test-results-1.jpg 900w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/test-results-1-300x279.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/test-results-1-768x715.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><p id=\"caption-attachment-1099\" class=\"wp-caption-text\">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.<\/p><\/div>\n<p>I tried it again, this time connecting the CT sensor to A0 and ground on my UNO, and got different but workable results. Amazing!<\/p>\n<div id=\"attachment_1104\" style=\"width: 910px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-UNO-Test-Results.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1104\" class=\"size-full wp-image-1104\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-UNO-Test-Results.jpg\" alt=\"\" width=\"900\" height=\"838\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-UNO-Test-Results.jpg 900w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-UNO-Test-Results-300x279.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Single-Ended-UNO-Test-Results-768x715.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><p id=\"caption-attachment-1104\" class=\"wp-caption-text\">The readings through the UNO&#8217;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&#8217;s (10 bits) makes a difference where absolute accuracy is concerned. Our purposes are much cruder &#8212; we just want to know when a minimum current (1 mA) is flowing. For that purpose direct-to-UNO works fine.<\/p><\/div>\n<div id=\"attachment_1101\" style=\"width: 910px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Testing-with-EMD-F3A.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1101\" class=\"size-full wp-image-1101\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Testing-with-EMD-F3A.jpg\" alt=\"\" width=\"900\" height=\"675\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Testing-with-EMD-F3A.jpg 900w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Testing-with-EMD-F3A-300x225.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Testing-with-EMD-F3A-768x576.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><p id=\"caption-attachment-1101\" class=\"wp-caption-text\">Testing current sensing with and InterMountain EMD F3A<\/p><\/div>\n<h4>Wait! This Shouldn&#8217;t Work!<\/h4>\n<p>You see, I was using the ADC in &#8220;single-ended&#8221;, &#8220;uni-polar&#8221; 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&#8217;t.<\/p>\n<p>I was puzzling as to why,\u00a0apparently, 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).\u00a0 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.<\/p>\n<p>DCC is an 8kHz\u00a0 (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.<\/p>\n<p>Or something along those lines is going on.<\/p>\n<div id=\"attachment_1107\" style=\"width: 910px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Uno-with-Mayhew-Labs-Shield-and-CT.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1107\" class=\"size-full wp-image-1107\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Uno-with-Mayhew-Labs-Shield-and-CT.jpg\" alt=\"\" width=\"900\" height=\"676\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Uno-with-Mayhew-Labs-Shield-and-CT.jpg 900w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Uno-with-Mayhew-Labs-Shield-and-CT-300x225.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/Uno-with-Mayhew-Labs-Shield-and-CT-768x577.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><p id=\"caption-attachment-1107\" class=\"wp-caption-text\">Mayhew Labs Extended ADC Shield on an UNO, reading the test CT sensor in differential mode.<\/p><\/div>\n<p>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\u00b5A in differential mode, the draw of a 20k\u03a9 resistor at 11 volts. Now that is sensitive!<\/p>\n<div id=\"attachment_1105\" style=\"width: 910px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Differential-Mayhew-ADC-Test-Results.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1105\" class=\"size-full wp-image-1105\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Differential-Mayhew-ADC-Test-Results.jpg\" alt=\"\" width=\"900\" height=\"838\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Differential-Mayhew-ADC-Test-Results.jpg 900w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Differential-Mayhew-ADC-Test-Results-300x279.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2017\/01\/CT-Differential-Mayhew-ADC-Test-Results-768x715.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><p id=\"caption-attachment-1105\" class=\"wp-caption-text\">Same test sequence using the Mayhew ADC to read the sensor in differential mode. Notice the increased accuracy, stability and sensitivity.<\/p><\/div>\n<h4>Next Steps<\/h4>\n<p>The <a href=\"http:\/\/amzn.to\/2jqbzlX\" target=\"_blank\">Gikfun 5 sensor<\/a> 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.<\/p>\n<p>For larger installations, the external ADC shield is worth the cost because it is fast and more accurate.<\/p>\n<p>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&#8217;t want to have to use a new microcontroller for every 8 blocks. The module I&#8217;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.<\/p>\n<p>In the next post, I&#8217;ll go into a solution to the problem and install the system.<\/p>\n<hr \/>\n<h4>Code<\/h4>\n<p>My experiment was complicated by the fact that I was testing different ADC&#8217;s and different connection methods. Below is some basic code for testing a CT sensor such as the <a href=\"http:\/\/amzn.to\/2jqbzlX\" target=\"_blank\">Gikfun units<\/a> 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.<\/p>\n<p>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 &#8211; <a href=\"https:\/\/thenscaler.com\/?p=514\" target=\"_blank\">1<\/a> , <a href=\"https:\/\/thenscaler.com\/?p=529\" target=\"_blank\">2<\/a>, <a href=\"https:\/\/thenscaler.com\/?p=604\" target=\"_blank\">3<\/a> and <a href=\"https:\/\/thenscaler.com\/?p=777\" target=\"_blank\">4<\/a>. ADC&#8217;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.<\/p>\n<p>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 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Root_mean_square\" target=\"_blank\">Root Mean Square<\/a> algorithm. This is the generally accepted method for determining A\/C voltage and current from digital samples.<\/p>\n<p>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.<\/p>\n<p>To make the process I&#8217;ve described accurate across an array of sensors, I developed a calibration routine. All ADC&#8217;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 &#8212; this too is noise that has to be filtered.<\/p>\n<p>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 &#8212; I call it ADC Zero &#8212; 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.<\/p>\n<pre>\/\/ CT Sensor test\r\n\/\/ Using UNO built-in ADC to read the sensor\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n#define VERSION \"1.006\"\r\n#define SYS_ID \"CT Sensor Test - Direct to UNO ADC\"\r\nconst int adcpin = 0;\r\n\r\n\/\/ Sampling Parameters\r\nconst unsigned long sampleTime = 2000UL; \r\nconst unsigned long numSamples = 100UL; \r\nconst unsigned long sampleInterval = sampleTime\/numSamples; \r\n\r\n#define SENSITIVITY 5000\r\n#define DETECTION_MULTIPLIER 1.3\r\n#define CALIBRATION_READS 300\r\n\r\n\/\/ variables to hold sensor quiescent readings\r\nfloat aqv; \u00a0\/\/ Average Quiescent Voltage; e.g. ADC Zero\r\nfloat aqc;  \/\/ Average Quiescent Current; \r\n\r\nvoid setup()\r\n{\r\n\u00a0 Serial.begin(9600);\r\n\u00a0 Serial.println(String(F(SYS_ID)) + String(F(\" - SW:\")) + String(F(VERSION)));\r\n\u00a0 Serial.print(\"\\nCalibrating the sensor at pin \");\r\n\u00a0 Serial.println(adcpin);\r\n\u00a0 aqv = determineVQ(adcpin); \r\n\u00a0 Serial.print(\"AQV: \");\r\n\u00a0 Serial.print(aqv * 1000, 4);\r\n\u00a0 Serial.print(\" mV\\t\");\r\n\u00a0 aqc = determineCQ(adcpin, aqv);\r\n\u00a0 Serial.print(\"AQC: \");\r\n\u00a0 Serial.print(aqc * 1000, 4);\r\n\u00a0 Serial.print(\" mA\\t\");\r\n\u00a0 float sense = (aqc * DETECTION_MULTIPLIER) - aqc;\r\n\u00a0 Serial.print(\"Detection Sensitivity: \");\r\n\u00a0 Serial.print(sense * 1000, 3);\r\n\u00a0 Serial.println(\" mA\\n\\n\");\r\n\u00a0 delay(7500);\r\n}\r\n\r\nvoid loop(){\r\n\u00a0 float current = readCurrent(adcpin, aqv);\r\n\u00a0 float delta = abs(aqc - current);\r\n\u00a0 bool occupied = delta &gt; ((aqc * DETECTION_MULTIPLIER) - aqc);\r\n\u00a0 \r\n\u00a0 Serial.print(\"Current Sensed: \");\r\n\u00a0 Serial.print(current * 1000,3);\r\n\u00a0 Serial.print(\" mA\\t\");\r\n\u00a0 \r\n\u00a0 if(occupied){\r\n\u00a0\u00a0\u00a0 Serial.println(\"Occupied\");\r\n\u00a0 } else {\r\n\u00a0\u00a0\u00a0 Serial.println(\"Not occupied\");\r\n\u00a0 }\r\n\u00a0 delay(3000);\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Current Sensor Functions\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nfloat readCurrent(int pin, float adc_zero)\r\n{\r\n\u00a0 float currentAcc = 0;\r\n\u00a0 unsigned int count = 0;\r\n\u00a0 unsigned long prevMicros = micros() - sampleInterval ;\r\n\u00a0 while (count &lt; numSamples)\r\n\u00a0 {\r\n\u00a0\u00a0\u00a0 if (micros() - prevMicros &gt;= sampleInterval)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 float adc_raw = (float) analogRead(pin) - adc_zero; \/\/ sensor reading in volts\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 adc_raw \/= SENSITIVITY; \/\/ convert to amperes\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 currentAcc += (adc_raw * adc_raw); \/\/ sum the squares\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 count++;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 prevMicros += sampleInterval;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\u00a0 \/\/https:\/\/en.wikipedia.org\/wiki\/Root_mean_square\r\n\u00a0 float rms = sqrt((float)currentAcc \/ (float)numSamples);\r\n\u00a0 return rms;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Calibration\r\n\/\/ Track Power must be OFF during calibration\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nfloat determineVQ(int pin) {\r\n\u00a0 float VQ = 0;\r\n\u00a0 \/\/read a large number of samples to stabilize value\r\n\u00a0 for (int i = 0; i &lt; CALIBRATION_READS; i++) {\r\n\u00a0\u00a0\u00a0 VQ += analogRead(pin);\r\n\u00a0\u00a0\u00a0 delayMicroseconds(sampleInterval);\r\n\u00a0 }\r\n\u00a0 VQ \/= CALIBRATION_READS;\r\n\u00a0 return VQ;\r\n}\r\n\r\nfloat determineCQ(int pin, float aqv) {\r\n\u00a0 float CQ = 0;\r\n\u00a0 \/\/ set reps so the total actual analog reads == CALIBRATION_READS\r\n\u00a0 int reps = (CALIBRATION_READS \/ numSamples);\r\n\u00a0 for (int i = 0; i &lt; reps; i++) {\r\n\u00a0\u00a0\u00a0 CQ += readCurrent(pin, aqv);\r\n\u00a0 }\r\n\u00a0 CQ \/= reps;\r\n\u00a0 return CQ;\r\n}<\/pre>\n<hr \/>\n<h4>Adjusting the Sketch<\/h4>\n<p>The SENSITIVITY and DETECTION_MULTIPLIER constants are the main values you can manipulate to adjust the sketch.\u00a0 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).\u00a0 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.<\/p>\n<p>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.<\/p>\n<p>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&#8217;ll need to adjust the variables to get a minimum of false readings.\u00a0 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.\u00a0 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.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&amp;NC. One project has been to further refine block occupancy detection in DCC to achieve 1 mA detection sensitivity\u2014the sensitivity needed to detect rolling stock equipped with a 10k\u03a9 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1101,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,21],"tags":[58,22,47,66,23],"class_list":["post-1080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","category-layout-control","tag-analog-to-digital-conversion","tag-arduino","tag-block-occupancy-detection","tag-current-transformer","tag-dcc"],"_links":{"self":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/1080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1080"}],"version-history":[{"count":25,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1449,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions\/1449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/media\/1101"}],"wp:attachment":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}