Jump to content
 

Using a PIC micro-processor to control trains


MarshLane
 Share

Recommended Posts

  • RMweb Gold

Evening all,

Sorry this thread isn’t moving quickly, time is my problem! I am still working things out with software - thanks @Andy Reichert for the Diptrace suggestion, i’ll take a look.

 

Seeking to bring us back on topic a bit, I am looking at this as a series of small modules,which may or may not be interconnected. So ignoring the ‘big’ picture at the moment and looking specifically at the controller module. I am working on the following stages - the aim being to purely control the speed of the train using DC current from a microprocessor. For this level, we are not looking how the speed is controlled or any other aspect.

 

So, we have power to our PCB (nominally at 5v for a PIC microprocessor. From the PIC one of the PMW outputs, which will have a low voltage output (milliamps) is used to start the circuit, that will conclude with a positive power feed to the track. As electrons move from positive to negative I am assuming we are working with the positive feed and having a common return to complete the circuit?

 

The circuit from the PIC output PIN, feeds through two PNP transistors, which can boost the track voltage to 12v and handle up to 3 a,ps. I am using the latter on the premise the N and OO locos can draw 1.3-1.5 amps, so if two are double-heading presumably there is double the amps?  I appreciate I still need to establish the value of the resistors that surround the transistors. The other option was to replace these two transistors with a H-Bridge chip, for which I am assuming something like this chip is an option. If I have understood correctly, the PMW signal from the PIC feeds into 1,2EN pin on the HBridge, and the output feeds out to the motor with the same PMW signal, but at the voltage being fed through VCC2, in this case 12v? Presumably VCC1 can be fed from the same 5v supply as the PIC?

 

If I have understood things right, in a normal (non-microprocessor circuit) there would need to be some form of timing chip to generate the PMW wave signal (like a sine wave) but in to case, we would do this through the programming of the PIC, so no timing circuits are needed.

 

Looking at direction control for a minute, the various books that i’ve been looking at and read have a Dp-DT switch in the circuits to swap direction of the train, but back on Page 1, in relation to H-bridge chips (potentially in place of the two transistors) @sharris said:

 

On 09/01/2020 at 21:32, sharris said:

Re: DIY transistor driver or H-bridge chip

 

Up to you really. One of the nice things about some H-drive chips (e.g. The L298) is that the digital inputs that control the transistors are logic levels, so you don't need level shifting transistors for the P-channel FETs (or PNP transistors if you stay bipolar) that switch the high voltage.

 

One thing you will have to do with an H-drive (whether chip or home-brew) is work out some steering logic to combine your PWM with direction to switch the correct transistors for forwards and reverse.

 

So a question to @sharris does the DPDT switch cover the steering logic, or are alternative means needed with a H-bridge chip?

 

Rich

Link to post
Share on other sites

10 hours ago, MarshLane said:

 

So a question to @sharris does the DPDT switch cover the steering logic, or are alternative means needed with a H-bridge chip?

 

Rich

 

Here is a datasheet for the L298 

 

https://docs.rs-online.com/d24b/0900766b8135f847.pdf

 

Pay particular attention to Figure 6 in the datasheet and you will see that indeed you can use a DPDT switch on the C and D inputs to control the direction, while providing pulses on Ven.  (you can ignore the 'fast motor stop' option on the truth table for model railway purposes).

 

The block diagram on page 1 of the datasheet shows the internal logic of this device. Other types of integrated bridge might work differently.

 

 

Sorry - the first time I read it, I skipped down to my my name and missed that you were talking about a SN754410 instead, but I think your idea for that chip is correct. 

Edited by sharris
Missed a bit.
  • Thanks 1
Link to post
Share on other sites

9 hours ago, MarshLane said:

So, we have power to our PCB (nominally at 5v for a PIC microprocessor. From the PIC one of the PMW outputs, which will have a low voltage output (milliamps) is used to start the circuit, that will conclude with a positive power feed to the track. As electrons move from positive to negative I am assuming we are working with the positive feed and having a common return to complete the circuit?

Couple of errors here. Voltage is not measured in milliamps, although I know what you meant, but more importantly actually electrons flow from negative to positive. The idea that current flow was from positive to negative was a very early mistake in electrics history, and is now referred to as Conventional Current Flow. This may or may not be relevant, but keep it in mind.

  • Thanks 1
Link to post
Share on other sites

Several things.   Firstly the SN74410 is a very old and nasty chip ( I know I use them on my own CBUS designs ) , you’ll get nothing like 1A out of them as they will fry. The are not short circuit protected or protected adaquately from over temp. They are very inefficient too, consuming considerable power themselves ( due to high internal resistance ) which all appears as heat. Mine get noticeably warm when drawing 100mA

 

The L298 dual bridge is a better bet , again it’s not well protected , so you need to have active current detection , so that you shut down the chip in times. They  are good for around 1A with some hestsinking. 

 

much easier in my opinion is to move away from PICs and into the Arduino environment you can get any number of motor driver shields and there are associated software libraries and examples to help you develop solutions.  With PiCs there aren’t good development tools available cheaply , whereas the Arduino environment has stacks available for free 

 

Note you don’t need DPDT switching if you have proper full bridge drivers , nor do you need a low ( voltage or current ? ) startup , everything can be done by adjusting the mark space ratio of the PWM signal. you will need to generate two voltages , the logic supply and the track supply, these can be derived from one supply voltage. 

 

Be be careful with common return , so that you appreciate the issues 

 

I suggest you join MERG , where you can piggy back on various very similar projects, which might enable you to get up the learning curve faster, especially if you haven’t much experience of electronics or embedded software 

 

Dave  

Edited by Junctionmad
Link to post
Share on other sites

1 hour ago, Junctionmad said:

Note you don’t need DPDT switching if you have proper full bridge drivers

As I understood it, the DPDT switch was to apply High and Low logic signals to the appropriate driver inputs (as per truth table in the L298 datasheet) to determine which pair of transistors in the H would be switched on when enable (from the PIC PWM output) was high, rather than used in the manner of the conventional polarity reversal switch often seen on the track feeds of many conventional DC controllers. 

 

I'd agree with you about the SN754110 as being a somewhat old design now, although I can't comment on whether they live up to their 1A rating (was the heatsink large enough and well enough coupled?). I see you de-rated the L298 from its specified current too. What PWM frequency were you using, could it have been so high that a significant part of the duty cycle was spent in the linear rise-and-fall of the output transistors making them eat more power? 

 

Link to post
Share on other sites

  • RMweb Gold
3 hours ago, sharris said:

 

Here is a datasheet for the L298 

 

https://docs.rs-online.com/d24b/0900766b8135f847.pdf

 

Pay particular attention to Figure 6 in the datasheet and you will see that indeed you can use a DPDT switch on the C and D inputs to control the direction, while providing pulses on Ven.  (you can ignore the 'fast motor stop' option on the truth table for model railway purposes).

 

The block diagram on page 1 of the datasheet shows the internal logic of this device. Other types of integrated bridge might work differently.

 

 

Sorry - the first time I read it, I skipped down to my my name and missed that you were talking about a SN754410 instead, but I think your idea for that chip is correct. 

 

Thanks for this.  Looking at your L298, which I am assuming is a more modern item, you refer to Figure 6.  I just want to make sure I am understanding this right:

 

1267960724_Screenshot2020-02-04at15_15_56.png.b196fd54ddc5e56ef72bdd51bc811db9.png

Taken from https://docs.rs-online.com/d24b/0900766b8135f847.pdf for the purposes of clarity.

 

As I understand it - the black box in the centre (with logic gates 3 and 4) is the L298 chip. Ven is controlling the direction of the motor depending whether the pin is High or Low.  Pins 13 and 14 (Outputs 3 and 4) are feeding directly to the motor (or the track in our case).  I am assuming D1 and D3 diodes are to prevent Back EMF?  I don't quite understand what the base of D2 and D4 is connected to - is is that just a diagram form to signify feeding back to negative?  The 100nF capacitor on Vss (where would this come from?) feeding into the chip, is presumably smoothing the supply coming in and acting as a decoupling capacitors and reducing any electric noise?  The 'Control Circuit' connection bottom left is presumably the PMW feed coming in from the PIC to Pin 15 which is the current sensing input.  Pin 4 is presumably the 12v 3amp feed coming in that ends up going to the motor?

 

Can you advise me as to what Rs (bottom of the diagram) is for, and where the 'Control Circuit' connection goes from ... or is that the PMW signal and is Ven controlling the direction of the motor depending whether the pin is High or Low?

 

EDIT: Having just re-read your comments, I see C and D are the directional control, with PMW pulse is entering through Ven from the PIC - in which case, what is the control circuit feed bottom left?

 

Sorry if the above sounds daft - I just want to make sure I fully understand each bit before moving forward!

 

Rich

Link to post
Share on other sites

You are right - the thick black line that D2 and D4 (and a few other bits) are connected to is a common representation for the 0V line. 

Vss is the supply for the digital parts of the circuit (+5V - you could use the same supply as your PIC if it's a 5V device). 

 

Think about where Rs is in the H (you'll need to go back to the diagram on page 1 of the datasheet) and what the voltage across it could signify, and what you might want to do if that voltage gets too high.

 

 

 

 

 

  • Thanks 1
Link to post
Share on other sites

  • RMweb Gold

Hi Richard,

 

I know you have seen some of my work with LNWR 5" gauge signal box, (I have made progress and am still working on it), but I was wondering if you had seen my (very) similar work on using a PIC driving a L298?

 

 

I am going to use this PIC controlled H-Bridge to drive trains from that signal box.

 

I am already using the circuit (very successfully) as my main general purpose controller...

 

 

Have you done any PIC programming yet? If so, what programmer are you using?

 

FYI

I use DIPTRACE with a MikroElekronika Basic compiler programming the PIC with a PicKit3.

All highly recommended.

 

 

Kev.

 

Link to post
Share on other sites

  • RMweb Gold
8 hours ago, sharris said:

You are right - the thick black line that D2 and D4 (and a few other bits) are connected to is a common representation for the 0V line. 

Vss is the supply for the digital parts of the circuit (+5V - you could use the same supply as your PIC if it's a 5V device). 

 

Think about where Rs is in the H (you'll need to go back to the diagram on page 1 of the datasheet) and what the voltage across it could signify, and what you might want to do if that voltage gets too high.

 

 

Are we saying that Rs is managing the voltage going into the H, and ensuring it cannot exceed a set amount? I missed the comment in the data sheet when I looked earlier that Rs allows measurement of the intensity of current.

Link to post
Share on other sites

  • RMweb Gold
6 hours ago, SHMD said:

Hi Richard,

 

I know you have seen some of my work with LNWR 5" gauge signal box, (I have made progress and am still working on it), but I was wondering if you had seen my (very) similar work on using a PIC driving a L298?

 

‘Hi Kev,

Glad to hear your still progressing with your signal box. I must admit, I was enjoying following your progress on that one!

 

No, I ha d missed those two threads - but I’ll go back and read them.

 

6 hours ago, SHMD said:

 

I am going to use this PIC controlled H-Bridge to drive trains from that signal box.

 

Have you done any PIC programming yet? If so, what programmer are you using?

 

FYI

I use DIPTRACE with a MikroElekronika Basic compiler programming the PIC with a PicKit3.

All highly recommended.

 

Kev.

 

Oooh, what’s the plan for the control aspects then? 

 

Yes, i’ve programmed pics before. I have a basic grounding in C, enough to get by, and have also done some assembler Code before, but majority of pic programming has been the same system - the Mikro Basic compiler. A system I got on well with, but the last couple of projects have been joint ones with a chap that knew electronics but couldn’t program! It worked well! Unfortunately, he’s no longer with us! He taught me enough to able able to understand (roughly at times) what a circuit would provide or do from the PIC signal, but for various reasons I didn’t progress my knowledge. So now, there are things I know I can get the pic to do, but once it leaves the digital environment of the PIC and into the analogue world, I get a little lost!

 

Nice to have you along though Kev, and thanks for the links to your PIC projects.

 

Rich

Link to post
Share on other sites

The C and D pins are the PWM drive inputs to the H bridge. In the case of the example the motor is being run at full speed.  For speed control the mark space of the PWM controls the speed .  Ven is the “ enable” control typically tied high for PWM applications . It does not control direction 

 

pin 15 is the current out of the bridge , when a voltage is developed across a small resistor , this voltage represents  the load current and can be input into a micro controller to determine when to shut the PWM drive to avoid over loading the driver chip 

 

diodes D1 to D4 are flyback diodes to prevent the collapsing field of the motor on shutdown or reverse from killing the driver chip , the 298 is quite sensitive in this regard 

Edited by Junctionmad
Link to post
Share on other sites

11 hours ago, sharris said:

As I understood it, the DPDT switch was to apply High and Low logic signals to the appropriate driver inputs (as per truth table in the L298 datasheet) to determine which pair of transistors in the H would be switched on when enable (from the PIC PWM output) was high, rather than used in the manner of the conventional polarity reversal switch often seen on the track feeds of many conventional DC controllers. 

 

I'd agree with you about the SN754110 as being a somewhat old design now, although I can't comment on whether they live up to their 1A rating (was the heatsink large enough and well enough coupled?). I see you de-rated the L298 from its specified current too. What PWM frequency were you using, could it have been so high that a significant part of the duty cycle was spent in the linear rise-and-fall of the output transistors making them eat more power? 

 

The typical way is to drive the C& D inputs from a micro controller output , the motor direction being controlled by which one is high or low there is no need for any DPDT logic ? 

 

The SN driver while rated for 1A ( in theory ) is, short of massive heatsinks ( which are impractical with dip chips ) unable to get anywhere near 1A continuous without frying. 

 

My comment re L298 is not “ derating “ you must take into account the  junction to heatsink calculations to determine the chip die temperature rise. This sets the operating point of the device and hence the current capability,  De-rating would then occur from that point for example to handle an increase in ambient temp range typically 

 

Furthermore the worst case DC  operating point , for this chip is an absolute maximum of 2A. No designer would operate at that point , a spike would kill the device , equally the thermal resistance of the device to the case is 3degrees/W and as it’s bipolar the source and sink  saturation voltage at 2A is nearly 3 volts. , ie 6V across the full bridge.  Using a big heatsink might get you a combined figure of say 6-8 degrees /watt , so you could see 12 watts of heat in the chip , this would cause 96 degrees of rise above ambient , not nice. For both sets of drivers this would fry the chip rapidly. 

 

Hence given modest heatsinks and saying inside a reasonable operating envelope, about 1A is practical with this chip. Mosfet H bridge drivers with their dramatically lower Rds on resistance are capable of much higher power. 

 

Edited by Junctionmad
Link to post
Share on other sites

3 hours ago, MarshLane said:

 

Are we saying that Rs is managing the voltage going into the H, and ensuring it cannot exceed a set amount? I missed the comment in the data sheet when I looked earlier that Rs allows measurement of the intensity of current.

 

Hi Rich,

 

Whatever form of driver you use to supply current to the track it has to be capable of handling an indefinite short-circuit without destroying the power output stage. The method I originally suggested uses a positive temperature coefficient thermistor (otherwise known as a light bulb) to protect the driver. It's crude, but it is also very effective and when the bulb lights up you know something has gone awry.

 

If you really want to be able to support bi-directional operation within a section you can use some sort of H-bridge as suggested above but you could also use one bi-polar power supply (+12v, 0, -12v) for the entire layout and use a different output from the PIC to drive a negative pulse power driver similar to the positive drive circuit for reverse operation.

 

Cheers,

Andy

  • Like 1
Link to post
Share on other sites

3 hours ago, Junctionmad said:

Furthermore the worst case DC  operating point , for this chip is an absolute maximum of 2A. No designer would operate at that point , a spike would kill the device

 

 

It's never a good idea to operate a device close to its maximum current rating but it's also useful to remember that current alone won't damage an electronic device. Heat destroys semiconductors but a very short duration current pulse (spike) that exceeds the absolute maximum rating isn't necessarily going to destroy the device. Voltage spikes are another thing.

 

 

  • Agree 1
Link to post
Share on other sites

Have  a look at the VNH7100BAS rather than the L298.

This is a much more modern design, with vastly higher ratings; 15A maximum output and rated for up to 38V, though nominally 12V.

It also has internal self-protection circuits, so near indestructible.

https://www.st.com/resource/en/datasheet/dm00314832.pdf

 

The IC itself is surface mount and not easy to use directly - but you can get them ready fitted to a small PCB for evaluation, for around £4 or so.

 

eg.

https://www.mouser.co.uk/ProductDetail/STMicroelectronics/EV-VNH7100BAS?qs=W0yvOO0ixfHw4RXpSmrSSA==

https://uk.farnell.com/stmicroelectronics/ev-vnh7100bas/eval-board-h-bridge-motor-driver/dp/2855563

 

 

  • Informative/Useful 2
Link to post
Share on other sites

3 hours ago, RobjUK said:

Have  a look at the VNH7100BAS rather than the L298.

This is a much more modern design, with vastly higher ratings; 15A maximum output and rated for up to 38V, though nominally 12V.

It also has internal self-protection circuits, so near indestructible.

https://www.st.com/resource/en/datasheet/dm00314832.pdf

 

The IC itself is surface mount and not easy to use directly - but you can get them ready fitted to a small PCB for evaluation, for around £4 or so.

 

eg.

https://www.mouser.co.uk/ProductDetail/STMicroelectronics/EV-VNH7100BAS?qs=W0yvOO0ixfHw4RXpSmrSSA==

https://uk.farnell.com/stmicroelectronics/ev-vnh7100bas/eval-board-h-bridge-motor-driver/dp/2855563

 

 

1.25m SMD pitch isnt at all difficult to hand solder 

Link to post
Share on other sites

50 minutes ago, cliff park said:

Wow, if the pins are 1.25 m apart how big is the chip (sorry, couldn't resist)

I'm thinking a flamethrower might be more appropriate than a soldering iron! 

 

Seriously though, some interesting alternatives there from Rob and Andrew. 

 

  • Like 1
Link to post
Share on other sites

4 hours ago, Junctionmad said:

1.25m SMD pitch isnt at all difficult to hand solder 

In itself, no - but it needs a very specific and fairly complex copper layout underneath it, with vias to conduct heat.

 

Having an appropriate layout PCB available with the IC already mounted is much easier for one offs or prototyping.

 

Link to post
Share on other sites

  • RMweb Gold
17 hours ago, MarshLane said:

 

‘Hi Kev,

Glad to hear your still progressing with your signal box. I must admit, I was enjoying following your progress on that one!

 

No, I ha d missed those two threads - but I’ll go back and read them.

 

 

Oooh, what’s the plan for the control aspects then? 

 

Yes, i’ve programmed pics before. I have a basic grounding in C, enough to get by, and have also done some assembler Code before, but majority of pic programming has been the same system - the Mikro Basic compiler. A system I got on well with, but the last couple of projects have been joint ones with a chap that knew electronics but couldn’t program! It worked well! Unfortunately, he’s no longer with us! He taught me enough to able able to understand (roughly at times) what a circuit would provide or do from the PIC signal, but for various reasons I didn’t progress my knowledge. So now, there are things I know I can get the pic to do, but once it leaves the digital environment of the PIC and into the analogue world, I get a little lost!

 

Nice to have you along though Kev, and thanks for the links to your PIC projects.

 

Rich

 

My control scheme for "Tenbione" will be the SignalBox.

If the route is clear then I can set the points... If the Route is set then I can set the Signals... Once the "Off" is obtained then the train will drive it self to the signals. I will be the Signalman!

 

Of course, I will have "Manual Override" and "Full-Auto" modes too.

To achieve this I have basically I had to wire the layout for "CAB Control" with my PIC based H_Bridge "Smart Driver Boards" doing all the driving leaving me to watch the trains pass the Signal Cabin. Details Here:-

 

 

What PIC programmer did you say you have?

 

 

Kev.

 

Link to post
Share on other sites

On 01/02/2020 at 12:32, Junctionmad said:

You have mentioned this before Andy. It’s issues are the problems of real world applications , the resistivity of track varies, from section to section , the round trip resistance includes the motor which again is again variable , the noise floor is high compared to the measured quantity.

 

so the theory is sound , but so is controlled fusion:D 

 

Controlled fusion works well enough. The problem is getting it to output more energy than input energy.

 

Yes, I have mentioned this before but I probably didn't explain it well enough so I'll try again:

 

NS rail resistance is relatively large (compared to copper) and it is also quite consistent. I've measured many samples.

 

The motor's resistance has nothing to do with it. There is a shunt regulator across the motor that maintains a voltage across the motor proportional to the current supplied by the controller. This means the controller is controlling the motor's speed by current rather than voltage which also makes any voltage drop due to rail resistance irrelevant.

 

Except that the voltage "seen" at the controller consists of two components: The controller knows the current and it also sees the track voltage. It determines the motor voltage by controlling the current so it can easily determine the difference. The difference is the voltage drop along the track and as it knows the current it can easily determine the track resistance and therefore the distance from the feed.

 

I admit this does seem a bit outside the "conventional wisdom" so feel free to ask questions ;)

 

 

Link to post
Share on other sites

2 hours ago, AndyID said:

There is a shunt regulator across the motor that maintains a voltage across the motor proportional to the current supplied by the controller.

 

Can you clarify that?

It makes no sense to me in the way it's written.

(Working with electronics and motor controllers for decades).

 

The voltage across a DC motor relates to it's speed-dependant back emf, plus voltage due to current and resistance.

Current on its own controls torque, or varies with load, depending how you look at it - not speed.

 

I agree resistance can be ignored to some extent if you alternately measure the back EMF then adjust the current up or down as required (with a series regulator) - or with a fixed series resistance, you can calculate the additional voltage that is adding to the back emf - but that's just with a bare motor at the far end of the circuit.

 

Where does the "shunt regulator" part come in to it?

 

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...