{"id":777,"date":"2016-04-24T19:45:41","date_gmt":"2016-04-25T02:45:41","guid":{"rendered":"http:\/\/thenscaler.com\/?p=777"},"modified":"2016-04-24T19:45:41","modified_gmt":"2016-04-25T02:45:41","slug":"block-occupancy-detection-for-dc-and-dcc-part-4","status":"publish","type":"post","link":"https:\/\/thenscaler.com\/?p=777","title":{"rendered":"Block Occupancy Detection for DC and DCC, Part 4"},"content":{"rendered":"<p>Through the first three parts of this series (links to <a href=\"https:\/\/thenscaler.com\/?p=514\">part 1<\/a>, <a href=\"https:\/\/thenscaler.com\/?p=529\">part 2<\/a> and <a href=\"https:\/\/thenscaler.com\/?p=604\">part 3<\/a>) I&#8217;ve experimented with and refined to a degree the use of ACS712 current sensors for block occupancy detection. The system works well in DC mode, lighting up whenever a running DC train enters or is powered up in a block; program logic &#8220;remembers&#8221; block states when power is off for control reasons. My DC locos typically start drawing 30 &#8211; 50\u00a0 mA when the lamps come on.<\/p>\n<p>In DCC mode, ultra low current detection becomes an issue and the off-the-shelf ACS712 sensors don&#8217;t meet every need. My BLI DCC\/Sound Locomotive draws enough power to be detected with the current system in idle mode, all sound and lights off. However, some standard DCC decoders draw so little current in idle mode that they are not consistently detectable. Further, constant track power in DCC invites us to find a way to sense any object drawing some minimal increment of current &#8212; such as 1.1 mA for a 10K\u03a9 resistor wheelset on an 11 volt (in N scale) feed. There are a number of obstacles to achieving 1 mA\u00a0 sensitivity with ACS712 sensors.<\/p>\n<h2>The Challenges<\/h2>\n<p><strong>First<\/strong>, there is a question of how low a current the ACS712 can resolve. Theoretically, because the chip responds in a ratiometric way to current input, any amount of current will provoke a proportional output response. In reality, the output of the chip is a little noisy, and the noise masks the low current response.<\/p>\n<p>Allegro MicroSystems <a href=\"http:\/\/www.allegromicro.com\/en\/Products\/Current-Sensor-ICs\/Zero-To-Fifty-Amp-Integrated-Conductor-Sensor-ICs\/ACS712\/ACS712-ACS713-Frequently-Asked-Questions.aspx\" target=\"_blank\">says that the best resolution with the optimum filter capacitor is 20 mA<\/a>. There is some general agreement among internet <em>cognoscenti<\/em> that with some amplification of the signal from the ACS712, greater sensitivity should be possible.<\/p>\n<p><strong>Second<\/strong>, the 10 bit ADC built into most Arduino boards has a maximum resolution of 4.88 mV (5 volts \/ 1024 steps), limiting the minimum detectable current to 26 mA (4.88 mV resolution \/ 185 mV\/A sensitivity). Even the 12 bit ADC found in the Due and Zero boards is not quite close enough, with a maximum resolution of 1.22 mV (5 volts \/ 4096 steps), resulting in minimum detectable current of 6.6 mA.<\/p>\n<h2>A Better Analog-to-Digital Converter<\/h2>\n<p>Of the two problems, this is by far the easier problem to solve by using an external ADC with a 14 or 16 bit resolution. A 14 bit ADC has a maximum resolution of .305 mV, which is a good resolution for consistently detecting current at the 1.6 mA level or higher; a 16 bit ADC would provide 76 \u00b5V resolution and current detection in the mid to high \u00b5A range.\u00a0 For those wanting to detect a single resistor wheelset, 16-bit is the way to go.<\/p>\n<p>For my purposes and goals, I&#8217;m going to see how it goes with 14 bits so I&#8217;m not pushing resolution past the point of usefulness at this stage. My thinking is that 1.6 mA should be sufficient to detect a <em>pair<\/em> of resistor wheels. Two 10k\u03a9 resistors in parallel have a resulting resistance of 5k\u03a9, which at 11 volts will draw 2.2 mA (@16 volts, 3.2 mA). Assuming I can overcome the noise problem to the point where 14 bits of resolution provides accurate current detection, the final step would be to go to 16 bits.<\/p>\n<p>I looked at several ADC products and settled on the <a href=\"http:\/\/mayhewlabs.com\/products\/extended-adc-shield\">Mayhew Labs Extended ADC Shield<\/a>, which is also <a href=\"http:\/\/amzn.to\/1VuBWnw\">available at Amazon<\/a>. I chose this for several reasons: it supports 8 inputs, comes in a shield format and (perhaps most importantly) offers enough bandwidth to be able to sample multiple sensors at a reasonable rate.<\/p>\n<p>Mayhew provides a code library for reading a variety of sensor types through the shield. It is fairly straight forward to use, and the shield is a very good quality piece of gear. I have found other ADC boards, but none with the unique capabilities of this one.<\/p>\n<div style=\"width: 510px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"http:\/\/ecx.images-amazon.com\/images\/I\/51eyKbQ4EjL.jpg\" width=\"500\" height=\"500\" \/><p class=\"wp-caption-text\">Mayhew Labs Extended ADC Shield<\/p><\/div>\n<h2>Sparkfun Low Current Sensor<\/h2>\n<p>Sparkfun sells a <a href=\"https:\/\/www.sparkfun.com\/products\/8883\">breakout board as a low current sensor using the ACS712 <\/a>sensor chip plus an operational amplifier to amplify the signal. This is the only commercial solution of its type I&#8217;m aware of, so I bought one to check out and test. The <a href=\"https:\/\/cdn.sparkfun.com\/datasheets\/Sensors\/Current\/SparkFun_ACS712LowCurrentSensorBoard_v14a.pdf\">schematic for the board can be seen here<\/a>.<\/p>\n<div style=\"width: 610px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/cdn.sparkfun.com\/\/assets\/parts\/2\/1\/5\/6\/08883-1.jpg\" width=\"600\" height=\"600\" \/><p class=\"wp-caption-text\">SparkFun Low Current Sensor Breakout &#8211; ACS712. This image is from an older version; different trimpots are used in the current version.<\/p><\/div>\n<p>Although the circuit is promising, this board is really an experimental item and not suitable for production use on a model railroad. Part of the problem is that it does not provide &#8212; and is not drilled for &#8212; standard screw terminals for attaching the input current source, adding additional challenges to deployment. On the input side (I+ \/ I-) it is drilled for a pair of header pins and two larger holes with contact pads. The control side (VCC, OUT, GND) is drilled for standard headers.<\/p>\n<p>Secondly, because this is an experimental board, it has two trim-pots for configuring the Op-amp.\u00a0 Sounds great, but in practice this setup doesn&#8217;t work well. Add sketchy instructions for setting and using the board and I think most will find this board impractical for large scale use, though an interesting experimental tool.<\/p>\n<h2>Learning By Failing<\/h2>\n<p>So when the board arrived, I soldered on some header pins and hooked up the sensor to an UNO with the Mayhew ADC Shield, then attempted to configure and use the board. Here are the setup instructions in their entirety:<\/p>\n<p style=\"padding-left: 30px;\">&#8220;To calibrate, first set the output offset to the desired level (with zero current on the sense lines, read output with a DVM). Then with a known current input (a 100mA limited supply works well for this), set the output deflection with the gain pot. Sensitivity is then calculated as (Vref &#8211; Vdeflect)\/(current input).&#8221;<\/p>\n<p>The &#8220;output offset&#8221; is (presumably, though not literally stated) adjusted with the vref trim-pot.\u00a0 But what &#8220;level&#8221; is &#8220;desirable&#8221; for this application?\u00a0 The second instruction is even weirder, since you are not going to short out a power supply by directly attaching it to the sensor without a load, and its the load that determines the current flow. I&#8217;m sure they meant to say something like &#8220;supply a load with a known current draw.&#8221;<\/p>\n<p>I tinkered fruitlessly with the trim-pots for hours, testing various theories about how it should work and getting nothing intelligible out of the sensor. The gain pot is straightforward enough, resulting obvious changes to readings as the trim-pot is adjusted. Its the vref adjustment that mystified me and seemed to have no logic. I\u00a0 did notice that when setting it there was a point where the output would suddenly, but only briefly, drop to O. That zero point is so finely specific that it is essentially impossible to set the trim-pot at that level; it always ends up off one way or another. That was a clue, but I did not yet understand.<\/p>\n<h2>Back to School<\/h2>\n<p>I found a <a href=\"http:\/\/www.electronics-tutorials.ws\/opamp\/op-amp-building-blocks.html\">nice tutorial about Op-amp circuits<\/a> and, after a while started to understand the circuit and comprehend the problem. If you don&#8217;t understand Op-amps (who does among those of us not trained in electrical engineering?), check out the tutorial before reading further here.<\/p>\n<p>The Op-amp circuit Sparkfun uses is a &#8220;voltage subtractor,&#8221; the intent of which is to\u00a0 subtract the input voltage from the reference voltage and amplify the difference. This is the right choice\u2014I think\u2014but the implementation is wrong, at least for my purposes.<\/p>\n<h2>Unique ACS712 Properties<\/h2>\n<p>Many sensors that one might use with an Arduino are straight forward linear output devices where the output voltage ranges from 0 to 5volts in proportion to an input (light, temperature, etc.).<\/p>\n<p>What makes the ACS712 different is that it senses both polarity and current of the measured input.\u00a0 The &#8220;quiescent voltage&#8221; we measure when calibrating a sensor is the sensor&#8217;s &#8220;zero&#8221; point: ideally VCC\/2 &#8212; midway between 0 and VCC (nominally 5 volts).\u00a0 When the polarity is one way the measured current is represented by a value above VCC\/2; when the polarity is the opposite, the sensor produces a value under VCC\/2.<\/p>\n<p>That means that we are only interested in the offset between the sensor output and its quiescent value.\u00a0The software calibration routine I&#8217;ve been using seeks to get the most accurate average mid-point value possible to enhance the accuracy of the offset measurement. Amplifying that offset should be the route to gaining sensitivity.<\/p>\n<h2>Shouldn&#8217;t vRef be VCC \/ 2?<\/h2>\n<p>It may well be that the vref adjustment has value when measuring DC current with a fixed polarity. However, I&#8217;ve seen no math or theory in support of that proposition. For measuring DCC current, which is an 8kHz alternating polarity wave form, there is only one correct reference voltage for comparison to the ACS712 output: VCC\/2.\u00a0 So, Sparkfun had the right idea using a trim-pot as a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Voltage_divider\" target=\"_blank\">voltage divider<\/a> circuit to generate the reference voltage, but the trim-pot inhibits setting vref to VCC\/2 because it is too imprecise to set the two resistor legs to exactly the same value.<\/p>\n<h2>What Now?<\/h2>\n<p>The Sparkfun sensor experiment was a bust in that it did not produce a usable result.\u00a0 But it did help me research and think about what it is I need an operational amplifier to do in order to successfully amplify the ACS712 signal. Sometimes we learn more from failure than we do from success.<\/p>\n<p>The next step is the set up an Op-amp with a fixed reference voltage of VCC\/2, then continue experimenting from there.\u00a0 I have the parts. In a few weeks, if time permits (between work and our ailing Beagle, time is tight right now), I should have some sort of result.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Through the first three parts of this series (links to part 1, part 2 and part 3) I&#8217;ve experimented with and refined to a degree the use of ACS712 current sensors for block occupancy detection. The system works well in DC mode, lighting up whenever a running DC train enters or is powered up in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":523,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[46,58,47,44,40,23],"class_list":["post-777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","tag-acs712-sensor","tag-analog-to-digital-conversion","tag-block-occupancy-detection","tag-circuit-design","tag-dc","tag-dcc"],"_links":{"self":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/777","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=777"}],"version-history":[{"count":10,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/777\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/posts\/777\/revisions\/788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/media\/523"}],"wp:attachment":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}