You're reading ...
Hardware, Software

Multiple Arduino’s chained as an S88 occupancy detector interface

In the previous post about using the Arduino as an S88 sensor interface, I only had 1 Arduino available for test, which restricted the test to max 16 inputs and it was not possible to see if ‘chaining’ multiple Arduino’s would work.

In the mean time a new batch of Arduino’s has arrived from Aliexpress. making a test with multiple Arduino’s in ‘chain’ possible. To chain them, all that is needed is to connect the ‘data-out’ of the next Arduino to the ‘data-in’ of the previous Arduino. Also, of course, the S88 Clock and PS pulses and GND have to be connected to all Arduino’s in the chain. The 5V power can come from the Command Station S88 bus, connect it not to the power terminal, but to a pin that reads 5V. First check your CS S88 power line has enough ‘juice’, else use an external supply. Power consumption of an Arduino is less than 30mA.

The software seems to be running well. I did have a strange phenomenon with my ECoS2 though. Every time the Arduino starts up, or is reset, the first 8 bits showed up as ‘1’, even though there were no sensor signals. It went to ‘normal’ only after I changed one of the sensor inputs. After a bit of puzzling and trying out different things, I still could not find a cause and a proper solution. So … what I did is have a little extra ‘tweak’ in the software. One second after startup I let the software change bit 0 of the sensors … which does the trick. Not neat … but it works! 🙂

The software can be downloaded here, free for private, non commercial, use. Disclaimer: So far it has only been tested on test-bench scale. Longer term reliability ‘in the field’ with longer wires and possible distorting signals around is not yet proven. Use at your own risk.

Advertisements

Discussion

43 thoughts on “Multiple Arduino’s chained as an S88 occupancy detector interface

  1. Rudy, I like your hardware/firmware project and found out about it from a printed circuit board design published on SeeedStudio’s web site. While I don’t own any model railway hardware, I have some suggestions for the v03 firmware that may help with “first 8 bits” problem and other intermittent problems with linked controllers mentioned in the comments.

    First, in the setup() function, you probably want to configure all the pin modes first and attach the interrupts as the last step in the function. In the AVR source, I could not find out if interrupts are enabled during the setup() function. If they are, you could enter the clock() ISR and read the pin voltages before the pull-up resistors are enabled.

    Secondly, depending upon exactly when interrupts are enabled, you might receive a number of clock() ISR calls prior to the loop() function being called the first time. Since you initialize data=0xffff, you might be sending those “1” bits prior to your first digitalRead() calls in loop().

    Third, on an Arduino reset, the clock() ISR will most likely trigger multiple times before the first PS() ISR arrives. This is because it is being triggered more frequently than PS and after a reset, you don’t know where you are in the cycle. You should guard against this so that you only start sending data bit0 in the clock() ISR after the first PS pulse has been received.

    Fourth, reading from the downstream Arduino after the hard coded delayMicroseconds(16) looks like a possible problem area with multiple linked boards. What you may consider doing is to create a clockOut pin and attach that to the downstream board’s clock pin. Then have the clock() ISR trigger on CHANGE, test or track the direction of the change, and cascade that clock pulse to the downstream board. After that, you could sample the dataIn pin just prior to sending the falling edge clock signal to the downstream board and you would have given it the maximum time to respond to the previous rising edge signal. Since you are reading the downstream bits one PS cycle before you will transmit them, you can give the downstream boards this extra time to respond.

    Lastly, it is recommended that any variables modified inside an ISR be declared as “volatile” to avoid unpredictable behavior when they are used outside of that ISR. The “clockCounter”, “loopCounter”, and “sensors” variables are modified in the PS() ISR and used elsewhere.

    Liked by 1 person

    Posted by Eric | August 10, 2017, 01:05
  2. Hey Rudy,
    this is awesome work. Unfortunately it seems like your box.net account is out of bandwidth. Is there a possibility to post the code in the blog (or could you email it to me?).
    Thanks,
    Henric

    Like

    Posted by Henric Jentz | September 30, 2016, 05:07
    • Henric, yes, Box gives Me a lot of problems. If you’d go to the Software page (top menu) there you’ll find a link to a zip file that contains all the software of all the blog pages. That link works.

      Like

      Posted by RudyB | September 30, 2016, 06:54
  3. Hy Rudy

    For some reason I can’t download the v3. I don’t get any errors, the download just doesn’t start (maybe it is my browser/settings or anything else). Can you please reupload v3 (on other site)?

    Thanks

    Like

    Posted by Doru | July 15, 2016, 09:51
  4. Hello Rudy

    Great work, I really do not know how you find the time to do all these great things …
    We have been steadily adding the S88’s (5 so far, 7 to go!) using the Mini Pro, however recently the first S88 is now intermittently showing the first 8 bits as occupied, this is the same problem you added the software fix for?
    I note the current version is V3 and all S88’s are flashed to that, have you noticed this on your modules and could it be a power/reset problem as we add more modules?
    We use the older ESU Ecos (not colour) so the transformer is not the adjustable one you have.
    its not a problem to not use the first 8 bits as we have plenty of Mini Pro’s! but if there is intermittent reseting of the units it will disturb the point settings as they are setting routes via the Ecos.

    Like

    Posted by martinkirkby | January 27, 2016, 10:26
    • Yes, those first 8 saying occupied at startup is a strange phenomenon. With Me it works fine now, with that startup delay, but to be honest I don’t know why that works! 🙂 I almost can’t imagine that the power surge of 10 Arduino’s would overload the ECoS S88 power supply. Maybe you can do a current measurement to see how much current is drawn? It is of course always possible to give the Arduino’s their own power supply, as long as the 0V is coneected to the ECoS S88 0V.

      Like

      Posted by RudyB | January 27, 2016, 11:21
      • Hello Rudy

        Thanks for your quick answer, we continued installing more Arduinos and just ignored the first 8 bits.
        But now we are seeing intermittent random triggering!
        The problem shows up as turnouts moving without inputs (via the ESU routes set) and also the occupancy LEDs flicker for a few seconds then they return to solid for the next 30seconds to a minute?
        It looks like interference as it gets worse when a loco is travelling around the track, the Arduinos are located under the stud/probe turnout board and approx 1 meter from the ESU connected this Ethernet cat5, well away from the DCC bus wires.
        My thoughts are to add “denounce” to the code?
        Also add ferrite’s to everything?

        Any suggestions to help would be very appreciated.

        MartinK

        Like

        Posted by martinkirkby | March 22, 2016, 19:55
      • Is the power supply for the turnout servo’s a separate one then the one from the Arduino’s? Just to be sure, I have installed decoupling elco’s over the power supply line for the servo’s close to every cluster of servo’s. The power for the S88 Arduino’s should come from the ECoS S88 bus … not even the ground limes should be connected to the other Arduino’s … is that the case? Other than that, I never noticed that DCC lines as such cause interference. But there are a few issues with certain ESU Lokpilot Sound Decoders, they are known for generating spikes casuing all kinds of troubles … do you drive with Lopi 4 Sound?

        Like

        Posted by RudyB | March 25, 2016, 08:12
      • Hello Rudy

        Thank you for taking the time to suggest a few answers.
        Yes the Servos/controllers have their own separate PSU/12v bus.
        Yes ferret rings on every servo lead as they pick up/generate noise causing unwanted servo movements/twitching.
        The S88-N Arduino’s are only connected to the ESU port.
        There are 2 DCC sound locos (not sure of their decoder type) but I will check/remove them from the layout.
        My other thought are ????
        Should the Arduino interconnecting wires ie. PS/Clock/5V/GND/DATA be twisted pairs as I understand the original flat S88 cable is better replaced with CAT5?
        Also you use UNO and I have MINI PRO’s could this be a source of trouble?
        Should the INPUTS be pulled positive in the circuit until triggered by GND?
        Could the standard Arduino “Debounce” code from their website be implemented for the INPUTS?

        I know more questions than answers! I will set up a chain of S88-N Arduino on the bench for testing, I assume I just remove your commented out lines to get a PRINT output?

        Regards

        MartinK

        Like

        Posted by martinkirkby | March 27, 2016, 15:11
      • Not quite sure how you control servo’s with the Arduino if they are powered with 12V? Do I understand correct that whenyou have the ‘disturbance’ both the Decoder- and the S88 Arduino’s behave strange? That more or less rules out the power, since they are both powered from different sources. They only behave strange when certain trains are driving? Or is it with all trains? Or even when no trains are driving?

        The S88 Arduino’s do not need an external pull up resistor, in the code we take care the internal pull up is used. The inputs must either float or be connected to Vcc, and must be pulled to gnd to detect a train. Do you use current detection, or reed switches, or optical sensors? Debounce is not going to help, in fact you want to see every signal, no matter how short.

        Like

        Posted by RudyB | March 27, 2016, 17:46
      • Hello Rudy

        Each Arduino servo controllers has its own DC-DC step down (set to 5v) for the servos.
        The disturbance of the servos was due to one loco (very old with an open frame motor) this has been fixed with the ferrite rings and some re-routing of wires. Most of the twitching is now under control!
        Sorry to add confusion to my post.
        So I have reviewed our “current sensing” train detection units (they are MERG design from here in the UK) they provide volt free isolation via a relay for output, however one unit is configured to pull its output to ground! (No relay on its PCB) And it’s powered by the common 12v bus that the Servo controllers use!
        I can remove it tomorrow as you have said both systems should be completely separate.
        I hope this is where the problem is as we were having a great experance with your S88-N Arduinos, I am I correct that a contact/input must be made for >50 ms for the ESU to show it as detected on the S88?

        Many thanks again

        MartinK

        Like

        Posted by martinkirkby | March 27, 2016, 18:57
      • OK, some progress at least. The S88 inputs only need to he low a few mocroseconds for the Arduino to detect them. A latch inside the Arduin remembers the state and transmits it to the ECoS, then a new S88 read cycle starts. The inputs this way are decoupled from the s88 read cycle.

        Liked by 1 person

        Posted by RudyB | March 27, 2016, 19:17
      • Hello Rudy sorry for my late reply, update on the problem. If you add more than three units the 16th input shows as occupied/triggered. That is shown on the 4th unit, also the inputs on the 4th unit now respond as if the previous in put was triggered i.e. You trigger input 4 and it shows as input 3 triggered!
        Then if you add a 5th and 6th unit the inputs 16/15 and sometime 14 show as triggered! All with the same -1 input problem.
        Could this be a timing issue as the data stream becomes longer?
        I hope you can fix it as we really need 220 inputs as a minimum.
        Regards
        MartinK

        Like

        Posted by martinkirkby | April 26, 2016, 12:51
      • I’m sorry, but I have no idea how this happens, which also means I have no idea how to solve this.

        Like

        Posted by RudyB | April 26, 2016, 13:30
      • Hello Rudy
        Just thought I would let you know we now have 12 units all working! Yes 190+ inputs for less than £50 wow!
        The fix was to replace the 26swg cat5 1 meter cable with a 24swg cat5 2 meter cable, the random inputs went away.
        There was a couple of dry joints as well but they killed a whole module and were easy to spot.
        We also connected a Ground wire from the Ecos to the last module to form an Earth ring main.
        The only odd thing is that the last module shows input 16 as triggered, but it’s not a problem to not use it.
        Thanks again

        Regards

        MartinK

        Like

        Posted by martinkirkby | May 3, 2016, 17:03
      • That is good news! Yes, the price is unbeatable, that’s the fun of it. There can also be frustration if things won’t work,mbut you did not give up and experimented and found s configuration that worked.

        The Software seems to work ok with multiple modules as it is, which is good news for potential new users.

        Liked by 1 person

        Posted by RudyB | May 3, 2016, 17:10
  5. Hi Rudy
    I have a question related with the sensors such as s88.
    I have a Ecos detector module from ESU with 16 inputs. Its the ESU Feedback module standart E50096 and it costs around 80 euros,
    Although there is a more advanced ecos detector feedback module, also from ESU. Its alomst 200 euros though 😦 an it allows Ecos to detect which locomotive is on a certain block and it holds up to 4 blocks only ( with this info ).
    My question is the following .Im going to use pc software taincontroler 8 , as you have . . Now will i need the most expensive detector module ? or i can use the standart one ? ( as a regular S88 )If so how will the pc software know which loco is on which block if the ECOS it self doesnt know ? I need more of these detector but i dont know which one i must buy.

    Thanks a million
    Joao from Portugal

    Like

    Posted by Joao | December 23, 2015, 23:27
    • Joao, you can use the cheapest Module. You can even use cheaper ones, an Arduino will set you back just €3,0 for 16 inputs and you can use reed contacts with them (not current detection) which are also cheap. Traincontroller knows where the trains are because at the very beginning you allocate your trains to the blocks where they are standing, from there on the software keeps track of who is where. Just have a look at the traincontroller videos on the blog.

      Like

      Posted by RudyB | December 24, 2015, 08:05
      • thank you Rudy.
        So should i just create a blocks ( isolating one of the rails ) and placing the reed switch before or within the block ?I mean if TC know who is where ,than what does the reed switch do for the TC ?

        Like

        Posted by Joao | December 27, 2015, 13:09
      • Place the reed switches where you want your blocks to start. TC follows the trains via the sensors, the reed switches, that’s how it knows which train is where. If you are going to use reed switches as train sensors, there is no need anymore for isolated sections. Isolated sections are only needed if you make use of current detection sensors. If you (already) use those, there is no need for the reed switches anymore.

        Like

        Posted by RudyB | December 27, 2015, 13:49
      • ohh i see.
        Great.
        Thanks a million.

        Like

        Posted by Joao | December 27, 2015, 14:45
  6. I bought and Arduino mega, I think its called and it has 16 i/o connections! I think I paid £5 for it from Aliexpress.com -its a chineese website, but has proved to be inexpensive and reliable, it even came with a usb cable. Delivery times are about 2 weeks. Perhaps I can link two of these?

    I’m awaiting some reed switches and magnets, so when they arrive I’m going to try to get them working with the Arduino mega. Much later I expect to progress from my mobile stations which I use now to a Märklin command station. I like the Ecos, (it has some really neat features and is documented well for the english speaker).but I have mfx locos, so I think I’ll end up with the Märklin CS.

    Like

    Posted by Peter Bradbury | November 17, 2015, 20:45
  7. Hi Rudy,

    I have been reading about using the Arduino Uno for an S88. Currently I have 5 S88 connected in serial. The are LDT RM-88-N from littfinski. If I use the S88-N bus and connect the wires from one LDT to the arduino as you mention do you think that would work. I am not sure if the S88-N clock and latch timings matter and whether they are different for ECOS and S88-N. Although S88-N is a standard.

    So basically I want to use my current S88 and connect further with Arduino S88 and more sensors.

    thx
    Danny

    Like

    Posted by Danny | August 17, 2015, 20:54
    • Hi Danny. I can’t confirm your questions with 100% certainty. The timing of the signals should be similar, and besides, it is not so much about timing, more about the sequence of clocks and PS signales and that should be similar for every S88 implementation. Still, there is just one way to find out, it will cost you just $3,- … buy an Arduino and try 🙂 If it does not work, you’ll still have a noce Arduino to do other fun things with around your railway, like control some lights or servo’s.

      Like

      Posted by RudyB | August 17, 2015, 21:09
  8. HAllo Ruud,
    heb 2 arduinos nu werkend in een chain, als een tierelier!
    Dank voor je werk en het delen ervan

    Like

    Posted by Erik | January 25, 2015, 12:02
  9. unsigned int sensors=0; (regel 21) <<< moet die veranderd worden ?

    void PS() {
    clockCounter=0;
    data=sensors;
    sensors=0; <<< of deze?
    loopCounter++;

    ik kan je misschien de code mailen zodat je kan zien wat ik aangepast heb ?

    mvg sytze

    Like

    Posted by Sytze | January 10, 2015, 19:13
  10. Rudy, klopt idd ik heb een fout begaan.
    ik heb het nu zo staan.
    pinMode(53, INPUT_PULLUP); //sensor 32 if (!digitalRead(53)) {bitSet(sensors,31);}
    clockCounter =(clockCounter +1) % 32;
    maar nog niet werkende.

    Like

    Posted by Sytze | January 10, 2015, 17:32
    • Sytze, ik zit niet achter m’n pc dus ik weet het niet zeker, maar waarschijnlijk is de variabele ‘sensors’ gedeclareerd is als een ‘integer’ (16 bits). Jij hebt ruimte nodig om 32 bits op te slaan. Maak er eens een ‘long’ van (dat is een variabele met 32 bits) … grote kans dat het dan werkt.

      Like

      Posted by rudyb2014 | January 10, 2015, 18:05
  11. het is niet gelukt om 32 ingangen te krijgen.
    pinMode(21, INPUT_PULLUP); //sensor 01 gekopieerd en aangepast tot pinMode(53, INPUT_PULLUP); //sensor 33
    if (!digitalRead(21)){bitSet(sensors,0);} gekopieerd en aangepast tot if (!digitalRead(53)) {bitSet(sensors,32);}
    clockCounter =(clockCounter +1) % 16; gekopieerd en aangepast tot clockCounter =(clockCounter +1) % 32;

    wat zou ik meer moeten doen.
    mvg sytze

    Like

    Posted by Sytze | January 10, 2015, 10:58
  12. ik kan de modulo 16 zo niet vinden in de code of kan het zijn dat het de {clockCounter =(clockCounter +1) % 16;} is.

    mvg sytze

    Like

    Posted by Sytze | January 9, 2015, 19:39
  13. Hallo Rudy,

    mijn dank is groot. ik heb alleen maar mega’s.
    die ‘increment modulo 16′ zat ik aan te twijfelen of dat idd 32 moest worden, maar had dat dus wel goed.
    nogmaals bedankt.

    mvg sytze

    Like

    Posted by Sytze | January 9, 2015, 19:31
  14. Hallo Ruddy,

    is het mogelijk om met een arduino mega2560 2×16 s88 ingangen te creëren ?
    of moet je persé 2 keer een uno koppelen om 32 ingangen te krijgen.
    ik heb hem al geladen in een mega2560 en de digital pinnen genummerd van 21 tot 53 maar alleen de eerste 16 leest hij.
    verder werkt het uitstekend.

    mvg sytze

    Like

    Posted by Sytze | January 9, 2015, 15:57
    • Hoi Sytze. Dat lijkt me geen probleem. Natuurlijk moeten in de software alle 32 gebruikte ingangen geconfigureerd worden. Verder staat er ergens een ‘increment modulo 16’ in de code, dat moet dan modulo 32 worden. Als er meerdere in serie nodig zijn, dan moeten het wel allemaal mega’s met 32 ingangen zijn (vanwege die modulo 32).

      Like

      Posted by rudyb2014 | January 9, 2015, 16:59
  15. Hi Rudy
    As an ESU user with a large layout 100+turnouts (but only slowly moving towards full DCC control/feed back!) I really think you have created brilliant solutions using the Arduino platform. I have tried Servo control using a Mini Pro and your S88 using the UNO, both work and will mean I can get a fully working solution much faster/cheaper than with the “out of the box” products available from the market place.
    Please keep up this great work as its the software, as usual 😉 that makes these great products possible.
    Thank again

    Martin K

    Like

    Posted by Martin | December 4, 2014, 20:31
  16. Hi Rudy
    Been following your site for some time now
    The work you carried out.If it wasn’t for you, I be struggling with my ECoS. I now can use Rocrail with the ECoS
    I now watching your Arduino systems to fit to my system
    I am a N-Gauge railway man myself, and if you can at some point set up a Ardunio with sound
    The N Gauge sound chips are very expensive and hard to fit it would be nice to run a Arduino with reed switch
    when a train that stops at stations it plays sounds around a station background
    Thanks for your great website
    Roger Newton

    Like

    Posted by Roger Newton | December 2, 2014, 10:03
    • Hi Roger. There is a sound library for the Arduino Duo.
      See this link: http://arduino.cc/en/Reference/Audio

      Like

      Posted by rudyb2014 | December 2, 2014, 16:07
      • Thank you Rudy
        I have now 2 Arduino’s Uno
        I managed to get the first one to work following your instructions
        I am having problems with linking the 2 Arduino’s together i can not see very well
        how you wired one to the other as a chain from your breadboard and across the 2 units
        hope you can send cable diagram of linking the 2 together
        thank you
        Roger Newton

        Like

        Posted by Roger Newton | January 3, 2015, 16:43
      • Hi Roger,

        Please make sure you have the v3 version of the software via the link on the software page.

        To connect multiple Arduino’s in a chain, connect the ‘data out’ pin 13 of the next Arduino in the chain to the ‘data in’ pin 12 of the previous one. The first arduino in the chain has its ‘data out’ pin 13 connected to the command station. Of course all Arduino’s need to have the software installed and need to be connected to power, and the ‘clock’ and ‘PS’ signals.

        If this does not work, then alas it can be quite a puzzle to figure put why. Try to add some tests in the software, like blinking the led, or measuring a time between signals, or use serial print. That is mostly how I test software if it does not work first time right (which is the case 99% :).

        Like

        Posted by rudyb2014 | January 3, 2015, 19:23
      • Thanks Rudy
        It was the data line out from pin 12 from the 1st unit to 13 on the 2nd unit
        all sorted now
        thanks again
        Roger Newton

        Like

        Posted by Roger Newton | January 4, 2015, 13:32

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: