Current detection - ADC

One way to detect if tracks are occupied, is to measure the current that is flowing once there is a train on the tracks. Depending on the type of train, such current can be anything between a few milli Ampère (mA) till rougly 1 Ampère (A).

A very good approach to measure current is to use the ADC capabilities of the MEGA 328 and similar processors. Such approach is not only cheaper than traditional approaches based on 4 diodes plus an 814 optocoupler, but also allows to adjust the detection sensitivity. In addition, the "loss of track voltage" is limited to the loss of a single diode only, instead of the 1,4V (or more) needed to trigger the 814 opto-coupler.

Since the use of AD conversion to detect occupancy seems to be relatively rare, this page is intended to give some background on how to design such detectors. Interested people should also look at the descriptions of OpenDCC GBM16 and the ArduCAN-GBM Strom.

Figure 1: ATMEGA 328P (28 pin PDIP version)

Hardware

Figure 1 shows that the ADC pins on the MEGA 328 are on Port C. The 28 pin PDIP version, which is generally found on the Arduino UNO, has (only) 6 pins available for ADC. The 32 pin TQFP package, which is used on the Arduino Nano, has 8 ADC pins. The Arduino MEGA 2560 has 16 ADC input pins. The MEGA 16, which is used in most designs described on this site, has 8 ADC pins on Port A.

The ADC converter can be based on an internal reference voltage. Interestingly this reference voltage is 1,1V for the MEGA328, but 2,56V for the MEGA16. On the MEGA2560 it is possible to select between 1,1V or 2,56V. Care should thus be taken if different processors are used.

To use the internal reference, AREF should be decoupled with a 100nF capacitor. An inductor of 10uH, in combination with a 100nF capacitor, can be used to surpress noise on the AVCC pin (note that the Arduino UNO doesn't have such inductor).

The resolution of the ADC converter is 10 bits. Depending on the reference voltage, the resolution is thus between 1,1 and 2,5mV. That should be more than sufficient to detect if tracks are occupied.

Figugure 2: ADC-based current detection

The basic schematics is shown in Figure 2 above. The DCC signal flows through 2 anti-paralel diodes, next to resistor R1. The voltage over these diodes/resistor combination is connected via resistor R2 to the ADC (analog) input pin.

ADC voltage - Resistor R1

As mentioned above, the resolution of the AD conversions will be somewhere between 1,1 and 2,5mV. To reliable decide whether a track is occupied, a value of 100mV will be sufficient. Since the required sensitivity for occupancy detectors will generally be around 1mA, resistor R1 should have a value of 100 Ohm. Thus, if 1mA is flowing, the voltage on the ADC input will be 100mV, which can reliably be detected. With 6mA the voltage becomes 600mV, which is in the range that normal diodes start conducting. With the ESD3D diodes shown if figure 2 the voltage over the diodes becomes 800mV at 1A, and 1V at 3A.

Note: the GBM16 of OpenDCC project uses resistors of "only" 22Ohm. The processor used in that project is the XMEGA128, however, which has 12 bit AD conversion (and 16 ADC input pins). This higher resolution gives more headroom to reliably detect occupancy, and thus allows smaller resistors. Also the ARM procesor used in the Arduino Due and ESP32's have 12 bit AD converters.

Diodes

Although the decoders I've build in the past used EGP30 and BYV-28 diodes, which are through-hole components, the figure shows two ESD3 diodes, which are similar but SMD variants. All these diodes are high-speed 3A types. Other diodes will likely work as well, provided they meet the following requirements:

  • Maximum average forward rectified current: 3A or more (maximum current onder normal operation).

  • Peak forward surge current: at least 50A (diode should survive short-cut of the tracks).

  • Repetitive peak reverse voltage: 40V or more (normal DCC signals are often from +15V to -15V).

Instead of "traditonal" diodes, it is also possible to use Schottky diodes. With Schottky diodes the DCC voltage drop will not be around 0,7V, but around 0,45V. The GBM16 recommends the use of SSA33L, SK 36A or SK 34A, the ArduCan recommends SB130 or SS34 diodes. The smaller DCC voltage drop has as advantage that the impact of the detection circuit on the DCC signal gets smaller, the downside is that the resolution of the AD conversion gets lower. If that is considered to be a problem, the value of R1 may be increaded or a processor with a higer AD resolution may be selected (see also the note above note under ADC voltage).

Resistor R2

Depending on the type of diodes, the voltage over the diodes will vary roughly between -0,7V to +0,7V. This causes two issues: 1) the ADC is only capable to measure positive voltages, and 2) negative input voltages on pins may destroy the processor. The first issue will be discussed later. To solve the second issue, resistor R1 is needed.

According to the specs, the voltage on input pins should not get lower than -0,5V. However, the MEGA has internal clamp diodes to protect the processor, provided that current stays below 1mA. Resistor R2, between the diodes and the ADC pin, is needed to ensure this current remains below 1mA. If the voltage drop over this resistor should be at least 0,2V (the difference between 0,7 and 0,5), the value off this resistor should be 220Ohm or higher. A safe value seems 1 KOhm, higher values will have nagative impact on the quality of the AD conversion.

Figure 3: Power supply

Power supply

As shown in Figure 2, the ADC value is measured against the DCC signal on one of the tracks (Lenz calls the track signals J and K). As a consequence, that signal acts as GND signal for the processor, which means that there is no galvanic isolation between decoder (GND) and track. This can be problematic if there are already other galvanic connections between the decoder and other devices. The decoders described on this website have no galvanic coupling with other devices, since they use the RS-Bus feedback system.

Since galvanic isolation between tracks and processor doesn't exist, an obvious idea is to use the DCC track signal as power supply. Although generally it is better to feed decoders from a separate power supply, in this case it may be acceptable to use the DCC track power, since the decoder requires only 20mA. So, even with 10 decoders (monitoring 80 tracks), the total power consumption remains less than an avarage engine.

The schematics is given in Figure 3. The J-signal is rectified using a single diode of 1A; the type of diode is relatively uncritical. The resistor of 47Ohm is used to limit the inrush current. Capacitor C1 may be relatively small, since it is filled by the (roughly) 10Khz DCC signal (if power comes from a 50Hz signal, the value of the capacitor should be much higher). Depending on the exact output voltage of the central station / booster, the input to the L7805 will be around 15V. The 7805 Linear Voltage Regulator needs to dissipate around 200mW (10V x 20 mA), which means that the bigger 1A version is recommended.

Note about galvanic isolation: The decoders described on this website have galvanic isolation since the design of the RS-Bus feedback system includes opto-couplers. However, several other feedback buses don't have galvanic isolation. In such cases special measures are needed. These measures involve two opto-couplers; one for sending and one for receiving. In addition, some active components, like transistors or opamps, are needed to amplify the feedback signal.

If, for some reason, a dedicated power supply is desired, such supply can be used provided that galvanic isolation is arranged via a DC-DC converter, such as for example the RECOM REM1-series or Murata NME or CRE1 series.