Skip to main content

I'm interested in using the standard isolated rail occupancy detector trick to drive logic level input on an Arduino.

It sounds like the right way to do this is to use a relay (and an optocoupler wouldn't hurt). There have been some great forum threads on this topic (example), but from what I can tell they are more focused on driving LEDs/lamps directly. Is it any different to safely drive a logic level input?

For reference I'm looking at eBay #142155381144. The isolated rail would feed the IN pin (via a resistor, as shown in the linked forum thread), and the Arduino would be connected at the "output" end, essentially directly to the relay.

I think what's confusing me is that these modules are advertised as helping convert from logic level voltage (output from an Arduino) to a higher voltage, where I'm looking to do it the other way around.

Am I making this too complicated? Thanks!

Original Post

Replies sorted oldest to newest

The relay modules can control 10 Amps of current. For driving an Arduino input you need to control, say, 1 mA of current (10,000 times less).  So, yes, you could use a relay module but somewhat overkill.  Of course the multi-channel relay modules are insanely priced at about $1 per relay (and even includes an opto-isolator on each channel).  But you can also get just a multi-channel opto-isolator module which uses less power, smaller foot-print, slightly less expensive per channel.

Untitled

Either method has i's to dot, t's to cross to safely mate the AC track voltage to the DC Arduino logic levels.  Resistors and/or diodes may be required.  And unless all this is old-hat, we should discuss  smoothing of the sensed signal whether done in software or with a capacitor.

Attachments

Images (1)
  • Untitled

You're not wrong on using a relay to isolate track power form the Arduino's inputs, but rather than using a relay module here you would use a relay with a coil voltage that can be driven directly form track voltage, and running the 5v to control the arduino through the relay's contacts.  

There is a simpler and less expensive way, however, to get the same result.  You can use opto-couplers and a 5vDC power supply of some sort.  One of those inexpensive buck converter modules would probably work out well.  

Connect +5v from the power supply (or whatever voltage your chosen opto-coupler requires) to the +/Anode on the opto-coupler.  Connect -/Ground of the power supply to your outside rail of your layout, and connect your isolated rail sections to the -/Cathode of the opto-coupler.  

This will cause the opto-couple to trigger whenever the isolated rail and outer rail are bridged, sending a logic-level signal on the opto-coupler's output to your Arduino.  

I use the cny17-1 opto-coupler at the moment, which I paid about 10 cents a piece for a bag of 100 last year.  At the moment I see them for about $8 shipped for 100pcs. on the big auction site.   These run well on 5v.  the output behaves like an NPN transistor, so they are active low, meaning that when a train is on the isolated section the opto-coupler will provide  a low signal, and a pull up to high will provide a high when the section is open.  

JGL

 Edit:  As stan suggested, you will need some method of de-bouncing the input, whether software or hardware, but there are countless examples to be found by searching the web, or Youtube for 'Arduino debounce' .  there is even an example built into the Arduino IDE package, under Examples-> Digital-> debounce

 

Last edited by JohnGaltLine
JohnGaltLine posted:
...

Connect +5v from the power supply (or whatever voltage your chosen opto-coupler requires) to the +/Anode on the opto-coupler.  Connect -/Ground of the power supply to your outside rail of your layout, and connect your isolated rail sections to the -/Cathode of the opto-coupler.  

This will cause the opto-couple to trigger...

Placing 5V directly across a generic component opto-coupler input will destroy it!    You need a resistor to limit the current as the 2 input pins are connected directly to a low-voltage LED inside the chip. 

If you are comfortable messing with small components, soldering, rolling-your-own, etc., 10 cents is indeed an attractive price for an optocoupler, but here's an unsolicited comment.  You can get optocouplers that have what is effectively an AC-input which really just means it has back-to-back LEDs inside.  If you plan to do more DIY projects in the quirky O-gauge world (with its AC tomfoolery) take a look at something like the LTV-8141 which costs a bit more.  That is, if you are only going to stock one optocoupler in your stash, I suggest one with AC and/or DC input; and of course it will work in your current application.  There are many choices but I suggest the LTV-8141 because a prolific OGR contributor (Gunrunnerjohn) uses and has published a variety of useful train circuits using this part.

 

Stan, you're right about the current limiting, I should probably re-read things when I post at 3 in the morning to make sure something wasn't forgotten.  Luckily folks like you will usually catch the mistakes.  In the example above a 220-330 Ohm resistor should do the trick.  

As for the part I used, it was just the least expensive Opto-coupler I could find at the time I wanted to buy some, and having a sack of 100 I'll be using them for a while yet.  For an actual PCB design I'd look into other parts that may be better suited to a particular application.  I'm also a fan of DIP for hobby electronics.  

With the double LED I can see an advantage in working with reverse voltages, but you still have the zero crossing to deal with.  I feel like I'd still rather use an 8 cent opto-coupler and a 1 cent diode to do the same job as a 40 cent-opto-coupler, when bought in similar quantities.  or even a bridge if you really need a signal on both halves of the wave.  

JGL

I hear you.  If you have a sack of them you can take 2 pieces and anti-parallel the inputs, and parallel the outputs to "make" an AC version. 

Untitled

Note that it's not just to detect both halves of an AC input but also, for example, to detect activity in our bi-directional DC-can motor drive circuits.  This allows you to detect (with optical isolation) activity in either direction as GRJ did in one of his recent motor circuits. 

Attachments

Images (1)
  • Untitled

I used to have a Schottky bridge for my first iteration of that design, and Jon Z. pointed out that I could use an AC opto-isolator. DUH!  Much better choice, and that's what I use now.  As stan points out, you MUST have current limiting for the emitter or you'll have a small puddle of silicon that doesn't do anything useful.

Locomotive Motion Sensor, Rev. 3 Schematic

Attachments

Images (1)
  • Locomotive Motion Sensor, Rev. 3 Schematic

Add Reply

Post
×
×
×
×
Link copied to your clipboard.
×
×