Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How to drive train models


  • Please log in to reply
59 replies to this topic

#1 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 30 March 2012 - 09:36 PM

Hi to everybody. As a software programmer, I have a very small knowledge (and all theoretical!) about hardware at this level. So, my problem is: a friend of mine has a very large train model set (five groups of rails, 15 trains, 10 meters by 3), all handled in analogical (kilometers of wires! :-)). So I told him I could help, trying to use a netduino to handle all the problems, but now I realize I don't have the necessary experience. So, just to start, I focused the first two problems: - Rail exchange is handled by a 16V AC line: to move the rail in one of two positions, you have to close one of the two circuits (ground is common), and just with an impulse. Which kind of relay/transistor do I have to use? - Traffic lights, rail exchange, other lights: all need wires, and on a 10x3 meters set, there are a lot of them. I'm thinking to some sort of bus (I2C?) to minimize the number of wires, but then, again, which kind of solution may I imagine? Theoretically, I would like to have some sort of I/O controller with for example 16 pins, and being able to connect to it from a distance of 5 meters, or something like that; from there, I would have to use way less wires... Anyone so kind to give me at least the directions where to look for? Thanks in advance to anyone, and excuse my poor English... Andrea

#2 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 31 March 2012 - 07:59 AM

Hi Andrea,

If I understand your project correctly, you are trying to control a lots of ON/OFF devices that use 16V AC, but you are not trying to control the locomotives with a variable power. Some of the devices need a pulse "rail exchange" (point / turnout motors), others need to stay ON for a long time (lights / traffic lights).

Since you are trying to control AC devices, the most straight forward way would be to use some small relays. These will allow you to control AC using the Netduino's DC outputs.

There are two halves of a relay to consider: the coil and the contacts (the switch).
Coil:

Most relay coils can be driven with DC, and this is what we want. There are a wide range of coil voltages available. Since you will need to use a transistor to drive the relay coil (this protects the Netduino), the coil voltage is not too important. What you should do is find out what DC voltages are available on the model railway. For example, there might be 5V DC, 9V DC, 12V DC or 16V DC, etc. Once you know what is available, you can select one voltage and look for relays that have a matching coil voltage.

Contact rating:

Most small relays will be able to control 16V AC. I imagine that the current (Amps) required will not be very large, so again most small relays will be OK. You need to find out how much current the motors and lights use, and check that any relay you select is suitable.

Contact configuration:

Most relays have changeover contacts, these could be useful for controlling devices like the traffic lights. For example, when the relay is ON, the relay could power the Green light, when it is OFF, the relay would power the Red light. For a point motor, you would need two relays one for each direction. You don't need change over contacts for this; if the relay is not changeover, make sure it relay has a Normally Open "N.O." contact.


For the point motors, you only want to deliver a pulse of current. This will either mean having the software pulse the relay ON and then OFF, or building an electronic timer circuit (e.g. using a 555-timer). There is one other possibility, doing a Google search I believe there are some relays that have momentary contacts. I imagine these will be hard to find and possibly expensive - but worth considering.

So, find out how much current each device needs, find out what DC supply voltages are available. Then try and select some suitable relays. Once you know which relays you are using, then you can design the transistor circuit.

I have not done much with SPI and I2C, so I'll let someone else answer for that. Be careful about the startup condition of any device you use. For example, make sure that the relays don't all turn on when the power is first connected to the railway - this might be disastrous.

(Your English is great - I hope I have not used too many strange words!)

Hope this gets you started - Paul

#3 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 31 March 2012 - 10:16 AM

Thanks very much, Paul, this is by far *much* more than what I hoped for. But I still have many questions, and I don't even hope for a further answer from you! :-) First of all, I need to clarify, for what I've seen of the model: rail exchange is done with a three-way pulse button, that closes a 16V AC circuit (this looks very strange to me! why not DC???) in one of two directions: you give an impulse up, for example, and the rail goes right, you give an impulse down, and you get the rail going left. There is an electromagnet calling the rail in the two directions. So the first problem is just this: googling around, I saw many people saying to *NOT* use DC to AC relay control, because of some coil *induction* effect (I don't know what this is, but they say that voltage can go really up for a few moments). Can you confirm that (I suppose because of the very small power) I will not have this kind of problem? The problem with the lights is much simpler: they are all very small LEDs, so the only problem, I think, is to use some sort of bus just to not have that many wires going all around. Actually for each LED there are two wires going back to the central control panel, and I think they are at least one hundred! A first approach could even be to use some other NetDuino in calculated places, to control local groups of leds, but then I have the problem of 'speaking' from a NetDuino to another, even if I think I can find documentation on this. The last problem, that in the first post I didn't mention, is to control train movement. Actually, since there are five distinct groups of binaries, my friend has five power supplies, but of a type I didn't know about. In Italy we have 220V AC, so this is the input, but then there are two outputs: one is the 16V AC to drive rail exchange, and one is the DC current to move trains, that can be altered with a gauge. Rotating this, you can even invert positive and negative, I suppose, because trains on the rails invert direction, and go faster or slower according to the level of rotation: so I suppose that this is some kind of potentiometer that can invert Voltage. My last question, at this point, is: may I think to replace this power supply with 'something' (what can it be?) that I can then control with software with a NetDuino? Right now, if you want to simulate the train going in station, you have to manually slow it with the gauge, but it would be fantastic to have some software algorithm at the press of a button, that slows the train until it stops! Too many problems? All of this looks very interesting, to me, even only to learn NetDuino programming *AND* basic electronics control (I already have bought a couple of books!). I am obviously available to share on this community all the details and the lessons learned! Thanks again. Andrea

#4 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 31 March 2012 - 12:26 PM

Hello Andrea.
Paul gave you an excellent panoramic about how to move in your project.

I'd add something about the "AC" doubt, because a while ago I was wondering your same question.
Most of the railroad model manufacturers chosen to switch from DC (in the past) to AC, essentially because there are few problems of polatiry when you have very large tracks. Moreover, in the past the DC polarity was the only way to drive back and forth a loco, but...what if you have two or more locos on the same track?...Thus the DC became uselessly.
If you check, the whole track is always powered at 16VAC, then every loco embeds a small microcontroller that converts the driving signals to concrete actions, such as the motor speed, direction, and the lights. As for "signals" I guess there are two kinds: infrared and CAN. The latest is "running" over the AC power.

About the (AC) relays for the track exchange (scambio), I am *NOT* sure, but to change direction you probably need just a short pulse, half a second should be enough, I suppose. You might check for that, because if it works, you could dramatically simplify the remote driving by using DC, which is much simpler (and cheaper) than using AC.
Please, also notice that 16V (AC) is the RMS (=Root mean square) of the actual peak of the sine wave. Just try with a normal 9V battery: it's much below the needing, but it should work at least for an experiment. If you have any +12VDC supply, it's better.

Finally, I do *NOT* suggest the I2C. That's because it's an (awesome) bus, but it's not targeted for long wiring, nor for "noisy" environments, such where are motors, AC, etc. If you wish to transfer the data "far" from the central command, the best way would be the CAN bus, but is an overkill.

At this point the question is mandatory: which brand of railroad are you planning to drive?
A couple of months ago I was able to remotely drive a Maerkiln loco using IR and a Netduino.
http://highfieldtale...r-for-netduino/

Ciao.
Biggest fault of Netduino? It runs by electricity.

#5 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 31 March 2012 - 01:48 PM

Hi Andrea,

I had a point motor on a Hornby OO-gauge railway, it was 12V DC, and also had three wires: left, right, common.
It came with a switch that looked like a lever in a signal box. As the lever was pushed from one end to the other it would connect either the left or the right wire as it passed through the middle. This sent a short pulse of current for about half a second (as Mario suggested) to one of the coils in the motor.
Posted Image

Just to clarify, I was suggesting that you use a relay to switch the AC to the point motors, but that you use DC to drive the relay coil. This is done often with Netduinos (via a transistor) and is safe to do.
Posted Image


I agree with Mario, I think you should be able to drive the point motors with DC (but using a lower voltage) - try a 9V battery and see if the track moves with a short pulse of current. If it does you could skip the relays altogether and just use transistors to drive the motors - much simpler.

The warnings you found on the internet are probably because of the risk of leaving the motor coil connected to the current. If you are using DC to drive the point motor and the switch (relay) gets left ON accidentally, a high current will flow in the coil and this could damage it. If you are using AC, then the current will not build up as much in the coil and so it is less likely that the coil will overheat and be damaged.

Remember to consider how you will attach the new circuit to the existing wiring. What happens if the Netduino stops working? If you have chosen relays, you can connect them "across" the existing switches so that your friend can use Netduino or manual control with out changing anything.

So there is a choice to make here - are you happy driving the point motors with DC and saving two relays per point, or would you like to stick with AC? I do like the idea of using less relays, but I also like the idea of having Netduino/manual control.



Since your lights are LEDs, then you don't need relays to drive them. Stefan has a web page about using 74HC595 shift registers to drive outputs. (I am using a single 74HC595 to drive a set of 8 LEDs.)
This would be a reasonable way to drive lots of LEDs (with resistors) in groups of 8 LEDs. BUT, I am not sure how well the SPI will work on a noisy railway circuit.



The last problem is the most fun! Controlling the locomotives.
If we assume your observation is correct, then the locomotives use DC voltage to drive their motors, and if you reverse the polarity, the train goes backwards.
My old Hornby system had a knob that rotated clockwise for forward and anticlockwise for backwards. It was a big variable resistor and a clever switch all in one.
I have another power supply that has a simple knob and forward-backward switch to swap the polarity.

There are lots of ways to drive DC motors, often it is done using "PWM" Pulse Width Modulation. The Netduino has four PWM outputs, it can be configured to turn each output ON for a given pulse length every period. When the pulse is ON, it is used to turn ON a pair of transistors that supply current to the rails. When a short period is used, the pulse length will control the speed of the motor. A separate Netduino output is used to select a different direction by selecting two other transistors.
The transistors come in a single chip e.g. "L298" called a "bridge":
Posted Image

There are shields (just one example) already made up that you could use.

Did you spot the problem? - The Netduino only has 4 PWM outputs, so you might need to use a PWM shield to get enough PWM outputs for your 5 sets of rails.


Lots of questions - and even more answers!

Hope this helps - Paul

#6 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 31 March 2012 - 04:48 PM

Ok! :-) I realize, and I thank you, that you are all very kind. As for now, I think I have really *a lot* to study, and to test. My (little) spare time will go to this project, and I will report the progress here. I would have a lot more questions, but it would not be fair to ask everything, and not try and test, so I'll keep silent, now, and will start with the components I already ordered. Thanks to anyone, I'll let you know everything! Bye Andrea

#7 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 31 March 2012 - 07:58 PM

Good luck!

#8 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 04 April 2012 - 02:48 PM

I'm here again! :-)
After reading books, googling around, I decided to start from the first problem (probably the easy one): controlling a lot of leds or similar devices on a wide surface (10 meters x 4 meters). Since actually there are really a lot of wires and cables, I think that using shift registers chain-linked each other and put in smart positions can save a lot of these cables.
Posted Image
This simple drawing can give the idea, I hope: the shift registers act as local 'pits' where to connect leds to, so I need only a three wires cable going all around, and the local (short) connection to peripherals.
Moreover, I think that netduino cannot power everything, so I'll have to add a power line going all around, and transistors to act as switches to activate the single peripheral, when put on.
It looks like it can become all very clean and simple, if this is a feasible solution...
Before starting testing, I would like some good advice, if possible: thanks in advance, as always!
Andrea

#9 AJB2K3

AJB2K3

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 04 April 2012 - 07:57 PM

Why not just switch to a DCC system? These have been around for decades Each loco need to be fitted with a DCC chip and is controlled by a matching DCC controller. Ready made modules are available and would save a lot of time and programming work.

"Maybe it's because you're a crap programmer"


#10 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 04 April 2012 - 08:37 PM

...maybe because I don't even know what it is... :-) I'm going to learn another thing... googleing right now! :-) Andrea

#11 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 04 April 2012 - 08:39 PM

Oh, no... the beauty of the system is that some of the trains are *really* old, so are most of the railways: so the project is to add control, but leaving all the (physical) model as is... Digital trains are not an option! Thanks anyway for the idea! Andrea

#12 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 04 April 2012 - 09:10 PM

Hi Andrea,

Yes, each 74HC595 would act as a connection point for a set of up to 8 LEDs. You might put each one (or multiples) in a small plastic box with some screw terminals for the LED connections.

In your diagram you show a closed loop. The shift registers would actually be connected in a long chain rather than a loop.

I don't think you will need any transistors if the loads you are driving are LEDs. I am using a 74HC595 with 8 standard LEDs without any issues. Having said that, you might need to use a transistor to drive high current LEDs or some blue/white LEDs that use a higher voltage.

When it comes to wiring it all up, I think you will probably need 6 wires (but you may want to add more):
  • Ground,
  • Power (one of 3.3V / 5V / 9V / etc),
  • reset,
  • serial clock (SPCK),
  • serial data (MOSI),
  • strobe/latch.

The reset is important if you want to ensure the shift register outputs can all be turned off at power on or when the user presses the reset button. If not there might be a few seconds of delay before the Netduino clears them at startup. Alternatively you could use a simple electrical circuit at each shift register "pit" to hold the 74HC595 reset line low as the power is turned on.

The clock, data and latch are the standard signals you must have to use SPI.

You might want to add another serial data line (MISO) to allow future expansion where you read inputs.

There is a big decision to make for selecting the power supply voltage to take to each block.
  • The simple option would be to use the 3.3V output from the Netduino, but this has a limited output current and will not allow any flexibility with higher voltage loads. (You could use this initially to just get started.)
  • Supplying the power from another source will allow you to supply more current to the LEDs - this will be essential given the number you are trying to drive. To do this you need to make sure that the shift registers and the Netduino use the same ground.
  • Using a higher voltage will allow those higher voltage loads if they are necessary - but you will need to provide a 3.3V regulator for each 74HC595 to reduce the logic supply voltage down to 3.3V.
  • You could have a hybrid - take two power wires around the circuit, the first 3.3V from the Netduino for the shift registers, the second a higher voltage (and current) for the LEDs. This would however require that you use a transistor for each output.
You will need to use capacitors on the power lines to reduce electrical noise. Noise on the data lines is a potential problem with this design, a model railway is a noisy environment, and you may have issues with trains causing extra clock edges on the lines.

Even more to think about - but it is all quite straight forward. Start off simple with a test circuit based on Stefan's wiring diagram and try out driving a small set of LEDs.

Paul

#13 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 04 April 2012 - 10:11 PM

Ok, Paul, I must be crazy! :-( Reading and googleing everywhere, I didn't remember that you already gave me the shift register solution: you are very sharp too, since you point out (correctly) that the wires are not surely three, but this is not a problem, since one only cable going round is surely much simpler than things now. I'm not an expert in electronics, so I cannot understand the capacitor thing (to avoid 'rumors'): but with your hint I'll try to find more info about it. I owe you a dinner, so if you ever come to Italy, I'll really be happy to have you in my house! :-) Andrea

#14 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 05 April 2012 - 03:59 AM

Please, forget the SPI way for a so wide surface: it won't work. Any TTL/HCMOS level signal (as is the I/O of the Netduino and related logic) is working without problem (*) for very short wiring, let's say under 20-30 cm. You cannot wire the SPI/I2C for meters along the train plate. A possible solution would be to use several Netduinos (e.g. the Mini), so that to distribute the driving (led, relays) to many little sections, whereas the wiring will be reasonably short. Any of the Netduinos can be connected via Xbee, RS485 or Ethernet (for Plus). The main controller could be another Netduino, or a PC. I don't see any cheaper, yet reliable, solution without using a decentralization of the drivers. The main problem is how to carry the signal from the main controller along the train plate without problems. Cheers (*) "without problem" is a relative concept. Let's say "is easy to avoid problems".
Biggest fault of Netduino? It runs by electricity.

#15 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 05 April 2012 - 05:46 AM

Mario, Thanks for stepping in. I was not sure whether the noise would prevent SPI working. Do you think it might be possible to use RS422/485 drivers to carry the clock, data and strobe? Paul

#16 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 05 April 2012 - 06:55 AM

Wouldn't schimitt trigger buffers along the line keep signal integrity? You can get them for $0.06 from Mouser.

#17 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 05 April 2012 - 07:19 AM

Mario,

Thanks for stepping in. I was not sure whether the noise would prevent SPI working.

Do you think it might be possible to use RS422/485 drivers to carry the clock, data and strobe?

Paul


Hello Paul, and many thanks for your excellent proposals: they deserve a bit better light, such as in the Wiki section, for instance.

The noise is perhaps the secondary problem involving problems in a long wired SPI. The first one is surely the not-adapted impedance.
It's a complex task, but I'll try to show a brief explanation.

You know, any waveform shape could be seen as a composition of sine waves, so called "harmonics". That's what basically Mr. Fourier explained a couple of centuries ago.
As it were a stainless steel string, the current/voltage can reach a stationary frequency, also called "resonance".
Since the SPI is a kind of "square wave" (i.e. full of sharp edges), the involved harmonics are decaying very slow on the spectrum (as 1/N), thus even a 100MHz component could be somewhat important.

When you are dealing with short wiring, the effect of the resonance could be insignificant, and your circuit works all the times.

Now, let's take a steel string (e.g. a guitar string). It's tight fixed on both edges, so there's no vibration on these points: only in the middle.
What if you take a steel stick, fixed on one edge only? The free edge is vibrating.

Now let's compare a "fixed" edge, to a point where the voltage is kept stable. For instance an output port. The logic imposes a voltage on that out, and you should make an effort to "move" that voltage.
At contrary, on an input, there's no "fixing", and the voltage is free to "move".

Whereas the delay/period (or whatever you want to call it) of this "resonance" could be significant, the related signals are not "adapted", and the actual signal will be distorted.

This could help: http://en.wikipedia...._of_electricity

There are other factors involving problems, but they are easily solved. The only "real" problem is that the cost will get higher!
Main factors to improve the communication:
  • low-impedance;
  • low-speed;
  • low-noise (good immunity);
  • good protocol.
The very best solution is the CAN-bus, which is used with success in automotive solutions. However, I'd like to hear from Andrea whether the budget could be a problem.

Cheers
Biggest fault of Netduino? It runs by electricity.

#18 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 05 April 2012 - 09:42 AM

Oh, I didn't think that a hobby problem could become so complex (and interesting! :-)). Yes, the budget is important, I think not only for me, but in general for any hobbist (but it depends on what we consider a budget; I think a budget is what an average person can spend thinking it will *NOT* have a cash return selling solutions): good solutions are the cheap ones, otherwise we step into professional solutions (that are not for me, anyway, since I am a software programmer!). So, thanks to anybody for any contribution, every single concept is really interesting, even if I thought this was the *easy* one, and it became the *worst*: the sad conclusion is that if this is the situation, it is difficult to imagine Netduino used in real solution different from toys/educational kits. When I see here people trying to make home automation, don't they crash in problems like this??? My thanks become larger and larger, I'm learning really a lot of things! ...and the dinner invitation is for Mario too (maybe it's easier, for him!) Andrea

#19 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 05 April 2012 - 11:38 AM

Città di Castello!...Such a wonderful village!...Highly recommended for any foreign coming in Italy for a trip. (I'd also say such a wonderful women!...Think Bellucci, for instance...okay, my wife is shooting me) Andrea, is it too hard to collect a rough number of leds, relays, and whatever should be involved in the automation? A sketch on a paper, with the railroad plate area, and a rough distribution of the actuators would be very useful. My habit is to wait a little bit before throwing something into the can bin. Ciao
Biggest fault of Netduino? It runs by electricity.

#20 Kermit

Kermit

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationCitt di Castello, Italy

Posted 05 April 2012 - 12:22 PM

:-) Yes, I think that Monica Bellucci is our national monument! :-)))
Apart from this, I don't want to throw anything away, I'm not that kind of guy... only, I'll have to study much more, and understand how I can solve the problem: challenges are made to be solved! :-)
Posted Image
Done with Paint, here is an image of how the model is: 10mx4m, there are a lot of different devices to control (the image cannot convey how many different, and small, electric devices there are), and my friend kept adding every single device, linking cables from any single device to the control center.
So, other than the wires, other problems are all the power adapters (from 220VAC to 9V, to 12V, to 6V, to 16VAC..., many for each voltage), buttons, pulse buttons...
And he would like to go on adding other devices, so reordering things, finding a different solution than the brute force, is becoming NOT an option!
I was pretty sure that I could send signals over a 10meters span, it didn't look a so great distance, but I believe in you, so I only have to explore other possibile solutions!
Andrea




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.