Jump to content
 

Using a PIC micro-processor to control trains


MarshLane
 Share

Recommended Posts

  • RMweb Gold

Morning all,

I am making progress on a 2mm layout that will depict a main line scene, but for a variety of reasons have decided to keep it pure DC, rather than fitting locos with DCC. This then set me thinking about control options and wanting to implement what used to be called cab control with the layout split into sections.

 

In the main, it will just be me controlling things, so some form of semi-automation is needed for the main lines. I have been looking at the MERG Superbloc BC3 option, which in 95% of cases is flexible and ideal. I appreciate it is discontinued as a kit, but the boards are still available from MERG (I am a member) and the parts can be acquired from various electronic sellers. The problem with the Superbloc boards is running speed. Although they do far more, in a nutshell for those not familiar, the BC3 boards allow DC controlled trains to run at line speed through a section, unless the next section is not clear, at which point they will slow them, before restarting and progressing increasing speed once the next section becomes clear.

 

The problem is that a WD on minerals would have been doing 25-30mph, where as an A4 on ‘The Elizabethan’ would have been running at 90! There is no method within the BC3 to adapt the boards maximum speed automatically depending on the train type. Because I look for realism is layouts, I don’t think there an average speed that would work for both and look right.

 

This set me thinking if there was a way in which a Freight could be identified, by an RFID tag or reed switch/magnet which would set an input low and reduce the speed of the section to say 35% of the line speed through resistors. Logically it could be done as the BC3s work on a global line-speed setting which is fed into the boards, so by adapting this input on a board-by-board basis, I think it would work. The problem is that on entering the next section the speed would ramp up steadily to the normal line speed unless some input could be passed between the BC3s to indicate a Freight. That I think starts to require a redesign of the board - potentially if I could get a copy of the CAD files I suppose it’s possible, although theory is one thing and I am not sure my electronics knowledge is that good to start altering things, especially as the ‘Freight’ signal should only be passed to the next section once that next section was clear, to avoid affecting other non-freight workings.

 

All of this is a long way of saying, I then decided that maybe the answer was to attempt a design of my own, modernized around a PIC processor (which I can programme fine) to give all the same features, plus some additions/refinements. But, again it is my knowledge of circuits and the individual electronic components that lets me down. Let me just say, if I do this I am not looking for anyone to do it for me, I want to learn so that my knowledge grows and gives me confidence to tackle similar things in the future. I am looking at PICs, rather than Ardunio as each section would need its own board, and therefore needs to be able to be converted into a bespoke PCB, which I am happier doing from a PIC starting point. 

 

There is a risk of failing at the first hurdle here, and hence I am seeking some advice. First basic challenge is to slow a train down and quicken it up. My idea is to connect a digital potentiometer to the PIC, communicating over SPI, so that the pic software can control the speed curve. 2mm runs on 12v DC, most digital pots seem to be 0-5v - there are some 12v ones but they are running at £3-£4 a pot, where as the lower voltage ones are <£1.

 

So the initial question (sorry for the long run-in) is could I run a 0-5v digital pot connected to A PNP Darlington Pair to increase the voltage to the track? If so, are there any specifics for the Darlington Pair/chip that I need to take into account? Obviously I don’t want the voltage to the track to exceed 12v, but my thought was that if the pair was giving 15-18v for example at the top end, then this could be limited by preventing the software from setting the pot beyond the 12v level, or b) by using resistors to give a failsafe approach - or both!

 

My idea is that the PCB will, like the BC3, have two analog pots that can be set with a screwdriver, to allow the rate of acceleration/deceleration to be adapted for each board - basically feeding a value into the PIC which is used in the maths calculations to establish the change rate.

 

Any thoughts/suggestions on the query would be appreciated. 

 

Rich

Edited by MarshLane
Spelling!
Link to post
Share on other sites

How about approaching it from a slightly simpler direction. control the speed within the engine, ie a resistor in series with the motor circuit inside the engine, fitted to just the freight engines. The problems that spring to mind in 2mm is physical size, and getting the Ω right .

  • Like 1
Link to post
Share on other sites

  • RMweb Gold

Hi Cliff,

Thats an interesting thought, although it potentially looses flexibility - for example, a V2 could be used on both a 40mph goods as well as  fast freight and passenger. There is also the element that some N gauge locos are split frame, which I think would make that more complex??

 

Rich

Link to post
Share on other sites

If you want a 12v output from your 5v digital pot, you could use a non-inverting amplifier with a 2.4x gain based around an op-amp. Something like a TL081 if you just need one up to a TL084 if you want 4 channels on 1 PCB.

 

You could use the output of this fed to a Darlington pair as an emitter follower for track power.

 

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

  • RMweb Gold
25 minutes ago, sharris said:

If you want a 12v output from your 5v digital pot, you could use a non-inverting amplifier with a 2.4x gain based around an op-amp. Something like a TL081 if you just need one up to a TL084 if you want 4 channels on 1 PCB.

 

You could use the output of this fed to a Darlington pair as an emitter follower for track power.

 

 

Thanks. That ‘sounds’ like a better option. Excuse my ignorance but what is an ‘op-amp’? It seems silly taking a 12v in and changing that down to 5v, then having to amplify it back up again, but that seems the best way of achieving the end result.

 

Is the Darlington Pair needed if using a TL081?

 

EDIT: Ignore me, ‘op-amp’ being Operational Amplifier. Just visited Google! Need to have a read up on those.

 

Rich

Edited by MarshLane
Link to post
Share on other sites

Hi Rich,

 

Since you mention MERG membership and RFID I thought I'd say that one or more members of MERG are working on a DC control system (CANCMDDC) that should also work with JMRI. If you have a  JMRI compatible computer that might be combined with CANCMDDC, and RFID readers to give you some or most of the building blocks to help achieve what you want.

 

Regards

 

Nick (a MERG member, JMRI user and a member of a model railway club that has a layout with CANCMDDC) 

  • Thanks 1
Link to post
Share on other sites

  • RMweb Gold

Nick,

Thanks for that, I didn’t know anything about the CANCMDDC proposals. May be it’s just me, but it’s one of the areas I find problematic with MERG is to know what is being developed or worked on, unless your ‘in the know’.

 

Despite the idea of PIC microprocessors, my aim was to try and achieve this without needing a computer plugged in (plus I am not a great fan of JMRI ... nothing really against it, I can just never really get it to work for me!) however, if it is likely to achieve the end result and be quicker and better, that may need to be the option.

 

Thanks, I’ll go have a wander through the MERG forum.

 

Rich

Link to post
Share on other sites

Hi Rich,

 

If you can write code for a PIC you can easily make it produce a pulse width modulated (PWM) digital output that represents the desired track voltage. You can either drive that directly to the track via a suitable power switching transistor from 12 volts or you can smooth the PWM signal to produce a DC voltage to feed the track through a power transistor.

 

The hard bit is the PIC code. The power/analog stuff is not very complicated. Ask more questions or feel free to PM me for ideas.

 

Cheers!

Andy

 

 

 

Edited by AndyID
missing word
  • Like 2
Link to post
Share on other sites

On 27/12/2019 at 16:29, MarshLane said:

Nick,

Thanks for that, I didn’t know anything about the CANCMDDC proposals. May be it’s just me, but it’s one of the areas I find problematic with MERG is to know what is being developed or worked on, unless your ‘in the know’.

 

Despite the idea of PIC microprocessors, my aim was to try and achieve this without needing a computer plugged in (plus I am not a great fan of JMRI ... nothing really against it, I can just never really get it to work for me!) however, if it is likely to achieve the end result and be quicker and better, that may need to be the option.

 

Thanks, I’ll go have a wander through the MERG forum.

 

Rich

Hi Rich,

 

Might not need much of a computer once the system is finished. If using RFID tags might be able to use a Raspberry Pi with no monitor, keyboard or mouse connected to run the JMRI side of things.

 

Re local control: the right PIC for the application will probably give you the lowest cost in a through hole package but with Arduino Nano (DIP) clones down to a couple of pounds they might be useful as they have libraries for PWM and RFID etc that might be useful.

 

I have put notes on the MERG website on how to modify ~£1 MFRC522 13.56MHz NFC RFID reader/writers to give greater range.

 

 

Regards

 

Nick

  • Like 1
Link to post
Share on other sites

Hi Rich,

 

Here's a basic circuit to convert the PIC's PWM output to track power. All it does is amplify the voltage and increases the current drive. The output is still PWM. If the pulse frequency is relatively high the motor's inductance and back EMF will tend to smooth out the pulses so that the motor "sees" something a bit more like DC. I'm not up to speed on DCC but I believe the decoders work that way. Someone here will have a good idea about the frequency they use.

 

The filament light bulb is for short-circuit protection.

 

PickBoost2.jpg.c335b4e74b2e71c14df4dada71deaf20.jpg

 

This is only one of many ways to do it but I'm hoping it will give you a better insight into the electronics. I've not identified the component types and values (because I have not worked them out yet!) and I think it would be good exercise for you to work them out for yourself with help from other forum members. There are only about four things you need to understand to be able to do it.

 

Cheers,

Andy

 

 

  • Like 1
  • Thanks 1
Link to post
Share on other sites

  • RMweb Gold

Thanks Andy,

So if I have read this right, we are looking at four resistors and two transistors? 
 

I am assuming that through the transistors your amplifying the voltage twice, from a voltage of around 3v from the microprocessor’s PMW output pin? I’ll go and do some reading on what I think the values  and specific types are and come back to you.

 

Thanks for taking the time.

 

Rich

Link to post
Share on other sites

Nice idea for an output driver from Andy - I would make a small change and replace the output transistor with a p-channel MOSFET - when fully switched on these tend to have lower resistances than bipolar transistors, so there is less power dissipated (heat) in the transistor, and less requirement for a heat sink on the output transistor. A diode is also needed across the load to protect the MOSFET from back EMF.

Link to post
Share on other sites

13 hours ago, sharris said:

Nice idea for an output driver from Andy - I would make a small change and replace the output transistor with a p-channel MOSFET - when fully switched on these tend to have lower resistances than bipolar transistors, so there is less power dissipated (heat) in the transistor, and less requirement for a heat sink on the output transistor. A diode is also needed across the load to protect the MOSFET from back EMF.

 

Rich might want to try that but even with a bipolar transistor it's not going to need much in the way of a heatsink. Rich, you might want to calculate what the maximum power dissipation of the second transistor will be, or tell us if you need help with that.

 

A diode would be a good idea with bipolar transistors too. Rich, do you know where to put it in the circuit?

 

 

Link to post
Share on other sites

  • RMweb Gold

Hi Andy,

Sorry been a bit caught up with family the past couple of days.  I'm going to try and work these values and that out and then post up a circuit plan from designspark.  That way I think it will be easier for people to tell me what I've misunderstood or got wrong!

 

Happy New Year to all.

 

Rich

Link to post
Share on other sites

On 31/12/2019 at 08:41, MarshLane said:

Hi Andy,

Sorry been a bit caught up with family the past couple of days.  I'm going to try and work these values and that out and then post up a circuit plan from designspark.  That way I think it will be easier for people to tell me what I've misunderstood or got wrong!

 

Happy New Year to all.

 

Rich

 

Hi Rich,

 

That's a good idea. Word descriptions don't tend to work very well for this stuff.

 

Happy New Year to you too.

 

Andy

 

 

EDIT: BTW, I just sent for 100 IR transmitter/receiver pairs for train detection. They were really cheap - maybe too cheap ;)

 

 

Edited by AndyID
Moinfo
  • Friendly/supportive 1
Link to post
Share on other sites

  • RMweb Gold

Afternoon guys,

Well as I thought might happen, work got in the way!  However, I have spent some time trying to get to grips with Designspark this afternoon.  Still not totally there, but making progress and may hopefully get a circuit diagram on here over the weekend - I hit an immediate snag in that DS is a PC software and I am Apple Mac!  So out came the old PC laptop that I use for Templot, which isn't fast or modern, but I am finding my way around DS on it slowly ... I think!

 

Processor

I understand the various aspects to PIC processors, but I am still working out which is the best one to use for this project.  I have initially chosen the PIC18F27J13 - a 48Mhz 5v 28-pin chip with 128Kb flash programme memory that is surface mount, and reportedly low power.  It may well prove to be overkill for the project, but we'll see.  It provides UART, SPI and I2C if needed, along with two 10-bit PWM modules, and two Capture/Compare/PWM modules (not sure what the different between the two types is as yet) and three 8-bit timbers, four 16-bit timers - which may well come in handy if I can use them as I am hoping to.  There is also four external interrupts and  a 10-channel, 12-bit Analog to Digital converter input.  It also provides On Chip serial programming via two-pins, which may be useful if the code needs to change at any point post development. 

 

The data sheet explains that decoupling capacitors are needed for the power supply pins.  Can somebody tell me what a decoupling capacitor does?  Is it just to smooth out the inbound power supply and minimise any spikes/drops?  It is suggesting a 0.1uf (100nF) 10-20v capacitor.

 

My plan is to come out on Pin 25 - RB4/CCO4 - one of the PWM output pins that supports unto 5.5v, although at this stage in the proceedings, the actual pin used is a minor matter, and may of course change depending on where it lies on the PCB / other input/outputs.

 

At this point a question, while reading up and learning about various things, I discovered you can get Half H Bridge DC Motor Driver chips, with the PWM feed of the PIC providing the necessary power. So is it better to use transistors etc, or a Motor Driver?  If the latter, would it be beneficial to me to progress Andy's original plan so that my understanding of electronics improves then change later if I want to a Motor Driver, or is it better for model railway use to stick with the transistors either  way?

 

Looking at the circuit Andy provided, I am going to stay with the bi-polar transistors that Andy suggested initially ( @sharris I'll look at the advantages to changing that to a MOSFET subsequently, but just want to try and keep my feet on the ground at the moment - but thanks for making that suggestion).  In trying to identify the options for the bipolar-transistors, I am assuming that the maximum outbound (emitter?) current needs to be 12v .. given that its going to the track we don't need anything higher?  

 

I am also trying to understand the purpose of the four resistors in the circuit.  I know the principal use of a resistor, so I am assuming that the first one (lets call it R1) between the PWM output and the transistor is limiting the electrical input to a level suitable for the transistor (T1), the same - although a different resistance level - for R2 (between T1 and T2).  I have not yet grasped the purpose of R3 (connecting to +12v) and R4 (connection T2 to 0v), although I assume for R4 that this is part of what is required to avoid any form of short circuit on T2?

 

IN terms of adding in a diode to deal with any back EMF, I am assuming that it needs to go after the last transistor and link back to 0V in order to take any EMF out of the circuit?

 

I'll apologise now for those who know electronics, if the above questions seem stupid and blatantly obvious!  I am determined to get there and understand this :)

 

Rich

 

 

 

 

 

Link to post
Share on other sites

  • RMweb Gold
On 03/01/2020 at 01:43, AndyID said:

EDIT: BTW, I just sent for 100 IR transmitter/receiver pairs for train detection. They were really cheap - maybe too cheap ;)

 

Sounds interesting :) I shall be keen to see how that develops and what you're doing!

Link to post
Share on other sites

Have you considered using an Arduino driving an H Bridge shield. The Arduino can produce a PWM output and can also read many different types on imput for speed control. The H Bridge just bumps up the voltage and power. It can also control direction. You can supply the whole thing with 12v as the Arduino has its own built in 5v regulator.

 

Stu

  • Thanks 1
Link to post
Share on other sites

I would suggest getting a few basic hobby electronics books (both analog and digital/micro) and mugging up before taking serious decisions. I'm a professional, but I wouldn't start your project until I had a handle on all the options first. 

 

Mike Predko published several books on using PIC's to simply control hobby robots that cover most of the stuff you are looking at.

 

Andy

  • Thanks 1
Link to post
Share on other sites

Re: decoupling capacitors.

 

Since you're looking at a PIC, I'll consider it in digital terms. Consider the output of a typical logic gate (this could be almost any gate, not necessarily part of a microprocessor.

 

IMG_6664.JPG.8b9c0ed96f0512aa3b8f66fb570a4a4b.JPG

 

Imagine the 'brains' of the logic is somewhere off to the left. Typical logic outputs consist of two electronic switches (bipolar transistors in TTL, FETs in CMOS). When H is on and L is off from the 'brains' the output will be high, conversely when H is off and L is on the output is low. Some gates allow both H and L to be off which switches the output to a high impedance state.

 

As long as the state doesn't change the gate draws a constant current (which is very low for the CMOS gates in a PIC). However, the switches aren't perfect and momentarily as they change state they will briefly go through a stage where they are partially conducting - during this phase they draw more current than when fully on or fully off. With a PIC states of many gates are changing on every clock transition. What this does is put current bursts onto the power lines which causes the supply voltage to fluctuate - this means that devices connected to the power lines see noise in their power which can cause glitches in their operation (e.g. memory devices may corrupt values they are writing, counters may mis-count, monostables may incorrectly trigger, etc)

 

To get around this decoupling capacitor is used. A Decoupling capacitor is placed across the power rails as close to the component as possible - this has the effect of providing a local energy store preventing the noise from traveling to other parts of the circuit- hence 'decoupling' the effect of this component from the rest. Normally a small value capacitor is used (typically 100nF) - why not use a bigger one with more of an energy reserve? Because small capacitors have better high frequency response, and the fluctuations are short spikes and dips with a high frequency response.

 

 

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

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.

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

6 hours ago, MarshLane said:

I am also trying to understand the purpose of the four resistors in the circuit.  I know the principal use of a resistor, so I am assuming that the first one (lets call it R1) between the PWM output and the transistor is limiting the electrical input to a level suitable for the transistor (T1), the same - although a different resistance level - for R2 (between T1 and T2).  I have not yet grasped the purpose of R3 (connecting to +12v) and R4 (connection T2 to 0v), although I assume for R4 that this is part of what is required to avoid any form of short circuit on T2?

 

You have the basic idea fairly well down.

 

Bipolar transistors are current operated; the base-emitter junction is effectively a diode and conducts at somewhere around 0.6 - 0,7V, so if you try and drive it with a voltage the current goes crazy and things start to leak smoke!

 

The series resistor to the base sets a suitable current for the application.

The resistor from the collector of the first transistor to the base of the second serves the same purpose.

 

The upper resistor, base to emitter, acts as a bypass to any leakage current and ensures the transistor stays off when it should be.

 

There should also be a base-emitter resistor on the first transistor for the same reason - even more important with a signal from an MCU as the output could be high impedance and totally floating until the device is configured by a running program.

 

The resistor from the second transistor collector to ground is just a load so the switching action can be seen & tested even with nothing else connected, it's convenience rather than vital.

 

The back EMF diode would go from the output to ground with the cathode to output & anode to ground, as you surmised.

 

 

 

Link to post
Share on other sites

  • RMweb Gold

Evening Morning(!) guys,

 

4 hours ago, sharris said:

Re: decoupling capacitors.

 

Since you're looking at a PIC, I'll consider it in digital terms. 

 

To get around this decoupling capacitor is used. A Decoupling capacitor is placed across the power rails as close to the component as possible - this has the effect of providing a local energy store preventing the noise from traveling to other parts of the circuit- hence 'decoupling' the effect of this component from the rest. Normally a small value capacitor is used (typically 100nF) - why not use a bigger one with more of an energy reserve? Because small capacitors have better high frequency response, and the fluctuations are short spikes and dips with a high frequency response.

 

Thanks sharris (sorry, dont know your name!) - my surmising was basically right, although the my reasoning was off on a tangent! Thanks, that makes perfect sense.

 

 

1 hour ago, RobjUK said:

You have the basic idea fairly well down.

 

The upper resistor, base to emitter, acts as a bypass to any leakage current and ensures the transistor stays off when it should be.

 

There should also be a base-emitter resistor on the first transistor for the same reason - even more important with a signal from an MCU as the output could be high impedance and totally floating until the device is configured by a running program.

 

The back EMF diode would go from the output to ground with the cathode to output & anode to ground, as you surmised.

 

Well I am feeling quite positive from that, as I thought I had something wrong in my thinking, so to have the basics right, and have got the diode in the right place is a bit of a boost!  

 

@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?

Link to post
Share on other sites

  • RMweb Gold
8 hours ago, Stuart Birks said:

Have you considered using an Arduino driving an H Bridge shield. The Arduino can produce a PWM output and can also read many different types on imput for speed control. The H Bridge just bumps up the voltage and power. It can also control direction. You can supply the whole thing with 12v as the Arduino has its own built in 5v regulator.

 

Stu

 

Hi Stu,

Thanks for your input.  Yes I did think about using an Ardunio and shield.  The problem is that I need to replicate the final design onto a PCB so that I can produce 20 odd copies of the board, and I felt more comfortable doing this from a PIC perspective, plus I have played with PIC coding before.  I assume there is away to build the processor used on an Ardunio into a PCB, but I was unsure how to go about it.  My thinking was that doing it this way, I am adding to my (limited) electronics knowledge and building new skills at the same time.

 

7 hours ago, Andy Reichert said:

I would suggest getting a few basic hobby electronics books (both analog and digital/micro) and mugging up before taking serious decisions. I'm a professional, but I wouldn't start your project until I had a handle on all the options first. 

 

Mike Predko published several books on using PIC's to simply control hobby robots that cover most of the stuff you are looking at.

 

Andy

 

Andy,

Thanks for that.  I have got a couple of hobby electronics books here, one that was written before micro processors and digital signals and one after, while I understand the theory they talk about, I struggle to get my head around words to put it into practise.  As this is a hobby project, not my profession, so as long as I learn where I went wrong, or why something didn't work, I dont mind making mistakes, I decided the best way for me, was to jump in and do something.  Programming the PIC and getting the processor to respond to things is the easy part for me!  Although I am taking this step-by-step, I do have a plan of what I want the final outcome to be and do.  But I appreciate where your comming from, and I will have a look and see if the books you mention can be obtained in the UK, if so I'll try and take a look at one.

 

Cheers

Rich

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