You're reading ...

Train detection with a reed switch and a magnet

With a digital model railway, train detection (block signaling) is an important function. Most often current sensing is used. It is quite reliable and you don’t have any (possibly ugly and always non-prototypical) sensors in view. The drawback is that it probably also is the most expensive method.

I like to try out some different sensing methods. Just for fun … and also because I like to use the Arduino for S88 sensor feedback, which can reduce cost by a factor of 5 or more!

This video covers the use of a reed switch and a magnet glued under the loc.

Optical detection has been discussed earlier, see this blog post.



About RudyB



13 thoughts on “Train detection with a reed switch and a magnet

  1. Hi Rudy, I’ve just started on Arduino and S88 with Ecos. In your video you mentioned it is possible to latch the reed switch detection via the S88 Arduino sensors. Could you please provide some more information on how this could be done. I need a little help to start on the reed detection and latching. Also, how would one reset the latch when the train moves out of the block. Regards, Vic


    Posted by Vic Dhawan | October 30, 2019, 17:33
    • I’m sorry Vic, it is too long ago that I made this video, I really can’t remember what I may have meant with ‘latching the reed switch detection’. The S88 bus just sends all the signals to the Command Station and if there is any latching to be done it is done in there, or in the PC that is connected to the Command Station.


      Posted by RudyB | October 31, 2019, 09:09
  2. Op welke aansluitingen sluit je het reed contact aan?


    Posted by Robbin28 | September 14, 2017, 17:54
    • Een Arduino Uno wordt gebruikt als S88 unit. Als je de code download, de link staat op de software pagina, dan zie je in de S88 software welke pin nummers gebruikt worden. De andere kant van de reeds ligt aan de 0V van de voeding, de nputs schakelen dus naar nul.


      Posted by RudyB | September 14, 2017, 18:02
  3. Hi Rudy, I need some help in connecting the Arduino uno directly to the laptop, for the use of occunpancy sensors. I have a test track where i see the Arduino uno see a loco going over the reed switch But I cannot get traincontroller to see it. I use a Sprog III for train and turnout control and have all connected via a usb hub, the laptop is running windows 7
    Would be great to see if i can get it to work
    Stuart (from Australia)


    Posted by stuart marshall | March 12, 2017, 09:46
    • From what I understand, Rudy configured the Arduino to connect to his ECoS command station using the S88 bus that is available on some command stations. So your Sprog 3 would need a S88 bus which looks like it doesn’t.


      Posted by diehard | March 15, 2017, 15:51
    • Yes, ‘diehard’ is correct. The Arduino’s read the sensors and transmit the sensor data via the S88 bus. Can’t connect that directly to a PC, you will need a device that has an S88 interface. Sprog does not have that. There are S88/Loconet/USB solutions … will have to Google some to find those.


      Posted by RudyB | March 15, 2017, 17:02
  4. Please disregard the post I made in the IR explaination 😉

    Hi Ruud,

    Very neat approach. I am just on a project that requires direction sensitive sensors. I order to do this, I thought it could be done by reeds located on each side of the track (in front of each other). So one would be on the left and the other on the right of the track.
    The loco would have two magnets located on each side. Each magnet would be placed a way such that only the reed on the corresponding side would be closed. The two magnets would also be placed at a distance from each other so that they close the reed at a different moment.
    The signal generated by the two reeds would then be connected to a NAND, XOR or whatever in a way that two output signals would be available at the Arduino.

    Any suggestion or hint ?



    Posted by Jacques Hoste | January 28, 2017, 20:50
  5. Hello Rudy
    I have been testing reed switching using your S88 program with the aduino uno
    with your optocoupler system with the ECoS all working well
    i need a little help with a problem
    If i use 2 reed switches with a servo : 1 reed for barrier up:1 reed for barrier down
    along a small section of track
    when i set the train running passing reed1 (Boom down )pass reed2 (Boom up) Great works
    send the train opposite way reed2 through to reed1 this will not work
    I been trying to only use 1 reed switch where i can run the train over the reed( Boom down)
    on contact with reed then Boom up when no contact which works
    So could help with a timer in your software so the (Boom up)
    will only move up after a set number of seconds
    that i can alter to suit the size of trains and waggons
    I If you think this is a silly idea and you can show me a way that I can get the Booms to work both direction on the track
    thanks for your support
    Roger Newton


    Posted by Roger Newton | September 21, 2015, 13:59
    • Hi Roger. If I understand well, the crossing is on a single track? A timer may at first seem like a solution, but it is not fail safe, it should also work well when trains drive slow.

      Do you use PC software to control the layout, and the crossing? If so, which software? Of it is possible to use a condition in your software then someting like this should work:
      Sensor 1 triggered and boom is open then close boom.
      Sensor 2 triggered and boom is closed then open boom.
      And vice versa the other way around.

      If no PC control is used, it is possible to create Arduino software that handles a crossing autonomously, not even needed to go via S88. The reeds connect dorectly to this Arduino. The software can take care of the above and can even control a few alternating flashing leds.


      Posted by RudyB | September 21, 2015, 16:38
      • Thanks Rudy
        Yes i do use RocRail for controlling trains
        I only tested the S88 interface with RocRail with the reed switches and that worked
        thanks to your great software
        I was testing the S88 reed switches and opto coupler and servo only with the ECoS
        I will try the full test with S88 Reed and the opto coupler and servo using RocRail
        this week and i let you know how i get on
        Thanks again for your support


        Posted by Roger Newton | September 22, 2015, 08:49
  6. Thank you for all the information on your blog.
    I will try you reed switch detection on my layout asap !!


    Posted by cardon samuel | November 29, 2014, 09:41
  7. Why not hide the reed switch by installing it in the same direction as the railway ties between 2 ties ?


    Posted by John Zajdler | October 12, 2014, 03:28

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Archive of all posts

%d bloggers like this: