Jump to content
 

Using a PIC micro-processor to control trains


MarshLane
 Share

Recommended Posts

2 hours ago, MarshLane said:

@AndyID could you point me in the right direction over the type and values that you were thinking for the transistors please?  I have looked at various options, and assume that the end voltage out of the latter needs to be a maximum of 12v, but cannot quite see/work out at this point how that steps through the two transistors.  Presumably one is boosting from 5v to ?, then the second takes that on the 12v that is needed for the track?

 

Hi Rich.

 

I could do that but then you won't learn very much ;)  I'd prefer to try to help you fill in some of the gaps in your understanding of electrical theory. Believe me, it's a lot simpler than programming!

 

It might have been a typo, but you mentioned "I am assuming that the maximum outbound (emitter?) current needs to be 12v". Assuming it wasn't a typo it means you need to get your head around the difference between voltage and current and how resistance can affect them. Once you really get that most of the other stuff falls into place.

 

Here's one link that explains Ohm's Law. (There might be better examples.)

https://www.electronics-tutorials.ws/dccircuits/dcp_2.html

 

BTW, you are on the right track in terms of understanding what the circuit is doing.

 

Cheers!

Andy

 

  • Like 1
Link to post
Share on other sites

  • RMweb Gold
11 hours ago, AndyID said:

 

Hi Rich.

 

I could do that but then you won't learn very much ;)  I'd prefer to try to help you fill in some of the gaps in your understanding of electrical theory. Believe me, it's a lot simpler than programming!

 

It might have been a typo, but you mentioned "I am assuming that the maximum outbound (emitter?) current needs to be 12v". Assuming it wasn't a typo it means you need to get your head around the difference between voltage and current and how resistance can affect them. Once you really get that most of the other stuff falls into place.

 

Here's one link that explains Ohm's Law. (There might be better examples.)

https://www.electronics-tutorials.ws/dccircuits/dcp_2.html

 

BTW, you are on the right track in terms of understanding what the circuit is doing.

 

Cheers!

Andy

 

 

Hi Andy,

I totally agree with your comment about filling in the electrical gaps (no pun intended!) I just didn’t want to be a pain in the **** by keep coming back with queries! 

 

I had made the rookie mistake by thinking that because motors are running at up 12v, that was what was needed. However, given that N gauge motors are running at around 300mA, I am assuming that the current supplies to the track would need to be around that figure, or around 600-700mA to allow for double heading? Am I correct in that assumption, or is there something I haven’t taken into account?

 

Rich

Link to post
Share on other sites

2 hours ago, MarshLane said:

 

Hi Andy,

I totally agree with your comment about filling in the electrical gaps (no pun intended!) I just didn’t want to be a pain in the **** by keep coming back with queries! 

 

I had made the rookie mistake by thinking that because motors are running at up 12v, that was what was needed. However, given that N gauge motors are running at around 300mA, I am assuming that the current supplies to the track would need to be around that figure, or around 600-700mA to allow for double heading? Am I correct in that assumption, or is there something I haven’t taken into account?

 

Rich

 

Hi Rich,

 

Don't worry about asking questions. They can help us try to point you in the right direction.

 

Starting from the load (the motor in this case) is a good idea. You know that second transistor (let's call it a power transistor (it's PNP btw) has to be able to pull the track voltage up to around 12V every time the PWM output from the PIC goes high.

 

The power transistor has to be able to handle a collector current of around 0.7A, but I'd go with something that can handle something like 3A to be on the safe side. It won't cost much more. The term you are looking for in a data sheet is I subscript C (max). The transistor has to be able to handle 12 volts between its emitter and collector when the transistor is not conducting ("off") but you'll be hard-pressed to find a power transistor that cannot handle a V subscript CE (max) of 12 volts. Most of them will safely  handle much more than that.

 

Something to watch is the power dissipation in the power transistor. When it is turned on (conducting) it is passing current to the motor and it is not a perfect switch. That means there will be a small voltage between its emitter and collector. That's Vce (sat) for saturated in the data sheets. The power dissipated in that state is voltage times current. Maximum pulse width is the worst case condition.

 

Keep plugging,

Andy

 

 

Edited by AndyID
  • Thanks 1
Link to post
Share on other sites

For ease of use I would suggest designing in a conventional H bridge motor driver IC. This avoids having to deal with short circuit situations , shoot through etc. 
 

Remember 70% of electronics ends up protecting the 30% actually doing the work !!! 
 

I would seriously suggest you join merg and put out a request for a design. There are considerable numbers of PIC experts there. 

Edited by Junctionmad
  • Agree 1
  • Thanks 1
Link to post
Share on other sites

  • RMweb Gold
2 hours ago, Junctionmad said:

For ease of use I would suggest designing in a conventional H bridge motor driver IC. This avoids having to deal with short circuit situations , shoot through etc. 
 

Remember 70% of electronics ends up protecting the 30% actually doing the work !!! 
 

I would seriously suggest you join merg and put out a request for a design. There are considerable numbers of PIC experts there. 

 

Thanks, I am a MERG member actually!  This is as much of me wanting to learn more about components and how the work/what they do, as it is creating a working board for the new layout.  Im just struggling with time tho, my understanding of things is definitely increasing, thanks to kind help from various people. And I have also read the MERG book on electronics for beginners this last weekend.

 

Rich

Link to post
Share on other sites

  • RMweb Gold

Hi all,

Sorry for the lack of progress on this, work has been a bit manic.  But also taking the advice from various people on here, I have acquired a couple of railway electronic books.  Back as a kid I remember my Dad acquiring 'Practical Electronics for Railway Modellers' and we built a couple of circuits from it - I still have the book somewhere, although cannot find it.  I recently discovered there was a second volume from Roger Amos, which World of Books had for £2.90!  Having bought it, I then found that Roger had produced 'The Complete Guide', which is basically Volumes 1 and 2 updated, at £4.80 it was a bit of a no brainer, and both have now arrived (in pristine condition and I doubt they were actually ever used), so I am browsing through those, and still learning the DesignSpark software.

 

I am determined to progress this and get my head around it, and thanks to everyone, but especially @AndyID for his help and encouragement.  Shall post an update shortly.

 

Rich

IMG_0578.jpg

  • Like 1
Link to post
Share on other sites

It seems to me that railway electronics, in fact electronics in general, have moved on an awful long way since the Roger Amos books were published. They will, of course, give you a good grounding. And all the information is correct, and still works. But so much has changed. As a retired electronics engineer I can say one of the biggest challenges of the job was keeping up to date in a fast changing world. We used to say that we would get a mass of new equipment, teach ourselves how to make it work and maintain it over the next year, then we would get training on how to maintain it, then 6 months later it would be replaced with something new and we start all over again. 

  • Agree 2
Link to post
Share on other sites

  • RMweb Gold
1 hour ago, cliff park said:

It seems to me that railway electronics, in fact electronics in general, have moved on an awful long way since the Roger Amos books were published. They will, of course, give you a good grounding. And all the information is correct, and still works. But so much has changed. As a retired electronics engineer I can say one of the biggest challenges of the job was keeping up to date in a fast changing world. We used to say that we would get a mass of new equipment, teach ourselves how to make it work and maintain it over the next year, then we would get training on how to maintain it, then 6 months later it would be replaced with something new and we start all over again. 

 

Yes I know what you mean. I used to feel the same way about computer and server systems - take two weeks holiday, you come back and something had changed!  My core issue is understanding the various components etc that feed into / handle the output from the PIC processors.  I can work out and programme the microprocessors fine, there's not much I struggle with, its all logical and logic.  But I want to be able to put the rest of the circuits together without having to ask for help every time.  Hence, I thought that the transistor and logic gates referred to in the Amos books would be useful background - again my Dad struggles to understand logic gates, to me with the computer programme, AND, OR, XOR etc, are somewhat second nature.  But put a circuit diagram in front of me, and while I can identify most components, I'm struggling to see how they work. The penny just won't drop at the moment - but im determined it will, even if its become a pound by that time!!!

 

Rich

Link to post
Share on other sites

3 hours ago, Junctionmad said:

 

alternatively a simple series pass transistor ( emitter follower ) could be used with the pic generating the base current drive. 

 

An emitter-follower driven from a PIC cannot produce 12 volt pulses. They'll be more like 2 volts. I think a PIC can only source about 5mA so you'd probably have to use a Darlington which would knock the pulse down to something like 1.2 volts.

  • Agree 1
Link to post
Share on other sites

20 hours ago, AndyID said:

An emitter-follower driven from a PIC cannot produce 12 volt pulses. They'll be more like 2 volts. I think a PIC can only source about 5mA so you'd probably have to use a Darlington which would knock the pulse down to something like 1.2 volts.

Perhaps “ simple” was misplaced , by the application of a simple amplifier , series pass element can be used.  That can be a Darlington if required 

 

pic IO can handle upto 20 mA on certain pins 

Link to post
Share on other sites

3 hours ago, Junctionmad said:

Perhaps “ simple” was misplaced , by the application of a simple amplifier , series pass element can be used.  That can be a Darlington if required 

 

pic IO can handle upto 20 mA on certain pins 

 

You could, but it would be hard to beat Andy's PNP switching circuit on page 1 for simplicity.

 

I think driving the output with an emitter follower rather than as a hard driven switch will increase the power dissipation in the output transistor too.

 

  • Agree 1
Link to post
Share on other sites

8 hours ago, sharris said:

 

You could, but it would be hard to beat Andy's PNP switching circuit on page 1 for simplicity.

 

I think driving the output with an emitter follower rather than as a hard driven switch will increase the power dissipation in the output transistor too.

 

yes , but some people dont like PWM based drivers 

Link to post
Share on other sites

7 minutes ago, Junctionmad said:

yes , but some people dont like PWM based drivers 

 

I thought you were the one suggesting a PWM driver... 

 

On 19/01/2020 at 04:17, Junctionmad said:

The easiest way to interface the pic is to a full H bridge driver chip. Then use alternating PWM signals from the pic 

 

alternatively a simple series pass transistor ( emitter follower ) could be used with the pic generating the base current drive. 

 

I read it as either feeding the PWM into an H-bridge chip (which has the advantage that with the right choice it will work straight from logic levels so you don't need any level shifting), or using the PWM output as the input to an emitter follower (in which case your digital ~0V and ~3-5V depending on your PIC power requirements would need boosting to switch between 0-12V) - apologies if I misunderstood and you meant to use the PIC DAC module to drive an emitter follower as a true DC controller  - you'd still need a gain stage, probably easiest with a non-inverting op-amp circuit to boost your DAC output (I can't remember the analogue voltage range for a PIC) to 0-12V for a DC controller at the base of a emitter-follower - actually I'd suggest a power supply of about 15V and gain to max out the base voltage at a little over 12V to take into account op-amp head-room and the 0.6V Vbe (or more for a darlington) drop on the emitter follower if you want to go to full 12V output. 

Link to post
Share on other sites

I'm re-reading this topic and it seems to me the only information automatically provided regarding the train speed originates solely from the train. Unless the trackside equipment can receive that information from each train on a per block basis and act on it, there is no way of setting the train speed correctly.  A solution of that kind would require some form of overall layout central control.

 

What could be done, without a central control set-up , is for the train to carry it's maximum speed info, and then filter the power from the track and feed it to the locomotive from the train instead of from the track.

 

That implies a power pick up and voltage limiting  (first?) car in the train wired to the locomotive, with the locomotive not receiving power from the track.  By just using a maximum speed limit, the train will obey all the DC voltage control for lower speeds, stopping., starting , etc. The car to loco power could be via a common model metal coupler (and ground) so that the same loco can be easily moved and connected to haul various different trains of different max speeds.

 

Andy 

 

 

 

 

Link to post
Share on other sites

  • RMweb Gold
1 hour ago, Andy Reichert said:

I'm re-reading this topic and it seems to me the only information automatically provided regarding the train speed originates solely from the train. Unless the trackside equipment can receive that information from each train on a per block basis and act on it, there is no way of setting the train speed correctly.  A solution of that kind would require some form of overall layout central control.

 

What could be done, without a central control set-up , is for the train to carry it's maximum speed info, and then filter the power from the track and feed it to the locomotive from the train instead of from the track.

 

That implies a power pick up and voltage limiting  (first?) car in the train wired to the locomotive, with the locomotive not receiving power from the track.  By just using a maximum speed limit, the train will obey all the DC voltage control for lower speeds, stopping., starting , etc. The car to loco power could be via a common model metal coupler (and ground) so that the same loco can be easily moved and connected to haul various different trains of different max speeds.

 

Andy 

 

 

 

 

 

Hi Andy,

The topic is possibly getting a little off target, but that is my fault I guess!!  My original idea is a PCB that contains a PIC that provides (through PWM) a DC controller.  At its very basic level, thats it - it would allow me to set the speed from the code etc, or develop a handheld plug in containing a pot which could set the speed.  My plan was to crack all that and get it working, then progress.

 

The ultimate aim is that each DC controlled section has its own section controller PCB (SC), which has several settings coded into the software for the PIC for the class of train (UK being Class 0 (Light Engines) Class 1 (express passenger), Class 2 (local passenger), Class 4 (75mph freights), Class 5 (ECS), Class 6 (60mph freights) , Class 7 (45mph freights) Class 8 (35mph) Class 9 (25mph)).  In essence knowing which class of train is it would set a 'maximum line' speed on the PIC microprocessor, which is controlling the current section.

 

The information on the Class of freight would come from the timetable PIC processor, which is connected through a 'PIC'network to pass information to the first section board.   This can therefore set the maximum speed that is achieved through the section.  Through track circuiting, as the train approaches the end of the section, the Current SC passes current speed and train type data to the next SC over the 'PIC' network, therefore the information is being passed through with the progression of the train.  Through track circuiting, the current SC would know whether the next section was free or busy, if busy, it would slow the train and stop it by the signal.  Once the next section becomes clear and the signal (either automatically or manually) displays a proceed aspect, it would gradually increase the train train.  All of this can be done through track circuit inputs to the PIC and PIC software, although with a few other features that are on my 'to do' list - one for example, being the introduction of an RfID reader at the fiddle yard exit.    In essence all of this has already been done (with the exception of different speeds) by the MERG Superbloc system, but this is all component based from around 20 years ago, and I wanted to bring the PIC microprocessor into the mix to make it more flexible and dynamic.

 

On this basis, the locomotive identity is then passed to the next SC (along with the train class etc..) which would not only allow the maximum speed to be either the line speed or loco max speed, which ever is lower, but would also allow a form of individual loco settings to be created (such as initial boost when starting, or maximum/minimum speeds, crawl speed etc) to be stored.  Again its all data that can be handled by and communicated to/from the PIC microprocessor, But I am getting a along way down the development route at that stage.

 

The whole idea is that other than maybe the RfID chip, there is no modification required to the trains in anyway.  Everything is handled by the PCBs to allow the layout to run smoothly and prototypically, while allowing the operator to take on the roll of the signaller, rather than train driver.

 

Rich

Link to post
Share on other sites

2 hours ago, Andy Reichert said:

I'm re-reading this topic and it seems to me the only information automatically provided regarding the train speed originates solely from the train. Unless the trackside equipment can receive that information from each train on a per block basis and act on it, there is no way of setting the train speed correctly.  A solution of that kind would require some form of overall layout central control.

 

What could be done, without a central control set-up , is for the train to carry it's maximum speed info, and then filter the power from the track and feed it to the locomotive from the train instead of from the track.

 

That implies a power pick up and voltage limiting  (first?) car in the train wired to the locomotive, with the locomotive not receiving power from the track.  By just using a maximum speed limit, the train will obey all the DC voltage control for lower speeds, stopping., starting , etc. The car to loco power could be via a common model metal coupler (and ground) so that the same loco can be easily moved and connected to haul various different trains of different max speeds.

 

 

Assuming you don't put a throttle of some sort in each train/locomotive (which Rich prefers not to do, and I don't blame him) some sort of throttle is required for each block section. The problem then is to know where each train is and how fast it is traveling.

 

I'm sure there are many ways to solve that problem but some examples are IR sensors to determine absolute position. Also, with a pair of sensors a known distance apart the PIC can calibrate the actual speed against a throttle setting.

 

It's also possible for the PIC to determine motor speed and combine that with the calibrated speed to determine actual instantaneous speed. Motor speed is proportional to back EMF and if the PIC can interrogate the motor current it can determine motor speed. In order to calculate the speed the PIC needs the resistance for the particular motor. It can determine that by applying a small voltage to the motor and measuring the current (or vice versa) while the motor is stopped.

 

BTW, if the PIC can monitor the current it can also compensate for additional motor load on gradients and curves to maintain speed or at least prevent stalling.

 

Of course there are going to be some approximations with all this and it does require good power feeds to the rails to minimize stray resistance.

 

 

  • Like 1
Link to post
Share on other sites

The thread was originally about a pic version of MERGs superbloc . Superbloc is DC and each block section control feeds the next section with the DC control level being used in the current bloc. The idea being it sets the track voltage for the next section . The section can either accelerate or decelerate the loco from  there.

 

Superbloc has no idea what actual speed the loco is doing 

 

Duplicating suoerbloc is easy to do with a micro 

 

 

If you start getting into track detection , or trainsponding , you’d be better off going DCC 

dave 

Edited by Junctionmad
Link to post
Share on other sites

hS 

4 hours ago, sharris said:

 

I thought you were the one suggesting a PWM driver... 

 

 

I read it as either feeding the PWM into an H-bridge chip (which has the advantage that with the right choice it will work straight from logic levels so you don't need any level shifting), or using the PWM output as the input to an emitter follower (in which case your digital ~0V and ~3-5V depending on your PIC power requirements would need boosting to switch between 0-12V) - apologies if I misunderstood and you meant to use the PIC DAC module to drive an emitter follower as a true DC controller  - you'd still need a gain stage, probably easiest with a non-inverting op-amp circuit to boost your DAC output (I can't remember the analogue voltage range for a PIC) to 0-12V for a DC controller at the base of a emitter-follower - actually I'd suggest a power supply of about 15V and gain to max out the base voltage at a little over 12V to take into account op-amp head-room and the 0.6V Vbe (or more for a darlington) drop on the emitter follower if you want to go to full 12V output. 

 

I meant some people dont  like PWM, ie 

pulse width modulation of a pulse signal. So the alternative is a DC level control via series pass element , yes via a amplifier which could   be driven in a number of ways from the PIC , R2R , DAC , or frequency to voltage etc. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...