Model Railway Block detection

Hi All

I am in the process of building a model railway in oo gauge and I need to develop a system of detection as to where my trains are on the track. I have seen many ways of doing it, but I need to keep the costs down.
My layout will consist of about 50 blocks. The method I am favouring at the moment is to use two reed switches per block (one at either end) so that I can detect a train arriving into the block either way.

I am looking at using an Arduino Mega but I feel that I will be short of pins on the board. If I use relays to tie the two reed switches per block (i.e. as a two way switching system) I feel that I will be cutting it a bit fine plus the cost of the relays may be more than another Arduino Mega. I am open for ideas I am sure that someone somewhere has cracked it.

Thanks

Roy

50 reed switches can easily be put into a matrix, and read all with just 15 pins (a 7x8 matrix can handle 56 reed switches). Or by using two shift registers you can bring that number down even more.

Scanning goes pretty fast, even if your switches are active for just 10 ms you should have no problem reading them frequently enough to not miss any events.

That sounds good can you point me to where I can read up on developing a matrix to do the job. If I can do 50 reed switches on 15 pins then I could do 100 on thirty pins and do away with the relay circuitry. That sounds great Thanks.

Rko1405:
That sounds good can you point me to where I can read up on developing a matrix to do the job.

Look for "switch matrix"

If I can do 50 reed switches on 15 pins then I could do 100 on thirty pins and do away with the relay circuitry. That sounds great Thanks.

100 inputs would need a 10x10 matrix - 20 pins.

Thanks for That it sounds even better.

Hi All I think that I have found what I am looking for posted by Marco at the following links:-

http://www.edn.com/design/power-management/4413677/Connect-a-4-3-matrix-keyboard-to-a-microcontroller-using-two-I-O-pins (http://www.edn.com/design/power-management/4413677/Connect-a-4-3-matrix-keyboard-to-a-microcontroller-using-two-I-O-pins)
http://www.edn.com/design/components-and-packaging/4408027/Read-10-or-more-switches-using-only-two-I-O-pins-of-a-microcontroller (http://www.edn.com/design/components-and-packaging/4408027/Read-10-or-more-switches-using-only-two-I-O-pins-of-a-microcontroller).

Thanks everyone for your help and guidance.

In that case: it's much easier to use a port expander.

There are 8-port expanders (PCF8574, MCP23008) or 16-port expanders (PCF8575, MCP23017) that connect over I2C, and you can connect 8 of those to a single I2C bus. So that's up to 128 switches with just two pins. Use 8x MCP and 8x PCF chips and you have 256 switches on 2 pins. But your scan speed is going to suffer badly.

Shift registers and a 10x10 matrix is the easiest. Well, make that an 8x13 matrix so you can do it with just three such shift registers (in fact it'd be an 8x16 matrix - 8 pins per register - 128 pins total). Shift registers such as the 74HC595 are dirt cheap and well supported by Arduinos.

There are 50 pieces. It means at least 100 connections (or 200?).

Myself I do this kind of things by first checking the feasibility. Say in this case: using only one pair of reeds and a simple control via few pins of an Arduino nano. But this is me, of course.

If the system were DC a voltage-drop sensing array might be the cheapest, but these sets are usually AC powered I believe?