DCC Train AutomationAn independent UK guide to automating a DCC model railway — home

Detection & feedback

How to add infrared and Hall-effect sensors to DCC-EX

Wire cheap infrared or Hall-effect sensors to a DCC-EX command station, define them for JMRI or EXRAIL and use them to trigger automation.

Time
1–2 hours
Difficulty
Medium
Cost
Low cost
Steps
6
Checked
Report a problem

What you’ll need

As an Amazon Associate we earn from qualifying purchases. The Amazon links go to search results, so you can compare what’s available.

Before you start

  • Check the sensor module's supply voltage suits your board before connecting it.

Step by step

  1. Step 1: Choose a sensor type

    DCC-EX's comparison: IR sensors are cheap and need no changes to stock, but room lighting affects them; reflective types hide easily in the trackbed, beam-break types are more reliable but harder to hide. Hall-effect sensors are cheap and ignore light, but need a magnet under the stock and only detect the magnet's position; they can be unreliable at speed.

    Next: step 2
  2. Step 2: Mount the sensor

    Reflective IR: in a hole between the sleepers. Beam-break: emitter one side, receiver the other – DCC-EX notes placing them square to the track can trigger on every wheelset, while angling them (about 45°) helps detect the train as a whole.

    Next: step 3
  3. Step 3: Wire it

    Connect the sensor's VCC and GND to the command station's supply pins and its output to a free digital I/O pin, or to a pin on an I/O expander such as an MCP23017 (DCC-EX).

    Next: step 4
  4. Step 4: Define the sensor for JMRI

    Send <S id vpin pullup> – for an active-low sensor on pin 22: <S 22 22 1>. When triggered DCC-EX reports <Q 22>, and <q 22> when clear. Use 0 for active-high sensors.

    Next: step 5
  5. Step 5: Or use it directly in EXRAIL

    EXRAIL refers to the pin (vpin) directly: AT(22) waits until the sensor is triggered, AFTER(22) waits until it clears. Put a minus sign in front for active-high sensors, e.g. AT(-22).

    Next: step 6
  6. Step 6: Test it

    Wave a hand or a wagon over the sensor and watch the JMRI sensor table or the serial log.

Tips

  • Shield IR sensors from sunlight and spotlights.
  • Fit magnets at the same place on every loco so stopping positions are consistent.

Questions people ask

IR or current detection?

Current detection covers a whole section and suits signalling; IR and Hall sensors detect a point, which suits stopping a train at a platform.

Can I make sensors permanent at start-up?

Yes – DCC-EX explains how to store sensor definitions in its startup configuration.

Sources

Checked 18 October 2026. We wrote this guide in our own words, using:

  1. DCC-EX: which sensor type to use (dcc-ex.com)
  2. DCC-EX: infrared sensors (dcc-ex.com)
  3. DCC-EX: EXRAIL command reference (dcc-ex.com)

Tags: DCC-EX EXRAIL Hall effect IR sensor JMRI sensors

Was this guide helpful?

Report a problem with this guide
What’s the problem?

Please don’t include personal details we don’t need. See our privacy notice.