I need to support 18 push buttons and 66 IR sensors on my Arduino Mega (model railway). I have a Mega Sensor Shield, but won't have enough pins for all the sensors. Can I get a second sensor shield and nest it some way?


  • 2
    Can you give a link to the shield in question please? There is almost certainly more than one.
    – Nick Gammon
    Jul 12, 2023 at 8:14
  • aliexpress.com/item/…
    – PaulN
    Jul 12, 2023 at 10:14
  • No, Nick is talking about a schematic @PaulN. At the very least some sort of specifications. Honestly, if I were going this route, most model railway setups I have seen are room sized. Like way over 10 feet from end to end. With runs like that, you will likely have problems keeping noise out of longer wire runs to/from sensor. I would use multiple Arduinos w/shorter runs. And pick communication protocols more immune to noise to mitigate any problems.
    – st2000
    Jul 12, 2023 at 12:10
  • If I see it right on aliexpress the entire shield essentially consists only of wiring individual pins together, along with power, to form groups of (pin, VCC, GND) couples for each pin. There is no logic, no electronics, nothing, just connectors. (And reset and LED)
    – gilhad
    Jul 12, 2023 at 14:13
  • Hi st2000 - sorry, thought Tom was asking what shield I was using. For complete transparency, I already have the Mega set up with the board, and I have 9 AdaFruit PWM Servo boards which drive 23 servos for turnouts, 106 LEDs for the mimic panel indicating turnout routes and block occupancy. The push buttons for the mimic panel are in the sensor shield. Although I can chain more servo boards on, the pins are output pins only, not input, eg, I can't read an IR sensor on them. Therefore I need them on the shield. But I don't have enough pins on the original shield to add the 66 IR sensors.
    – PaulN
    Jul 12, 2023 at 15:09

2 Answers 2


I may be interpreting your question wrong, but it seems that you're asking if you can somehow get more input pins for your sensor (general-purpose input/output), with which your sensor shield will not help with that. If you want to get more inputs for your sensors, you'll need to use shift registers.

Link to understanding how to add more digital GPIO pins

  • 1
    Thank you - yes that sounds about right. I will get some. Assuming as I have a number of spare GPIO ports on my shield, I can put these to multiple shift registers, if each register supports 8 addresses, I can put 8 chips on a breadboard.
    – PaulN
    Jul 12, 2023 at 15:29

For a model railway, this may not work as well as you are expecting. Sending 66 sensors values and 18 push-buttons along long cable lengths, as may be expected for a model railway, could introduce noise which would give bogus readings. The noise in long, adjacent, cables could be from one cable inducing a voltage in its neighbour, or simply from the railway operations themselves.

It may be better to use something like RS485 which is a balanced protocol. That is, of the two data wires connecting nodes, one is positive and one is negative simultaneously, for a signal to be considered genuine. Noise would tend to influence the cables in the same direction (eg. both positive) and would thus be discarded.

As described in the Wikipedia article, RS485 is used in aircraft for reliability. The wiring is minimal: two wires for data, plus an earth wire. You would also need 5V power available at the nodes.

What I would suggest here is a series of nodes, spread around your railway layout. Each node could be an inexpensive Atmeg328P processor (around $3 on eBay) with a couple of supporting resistors and capacitors. You would use 3 pins for the RS485 (transmit, receive and enable) which would leave 17 inputs for your sensors and switches per node. Thus, around 5 nodes.

You would keep the cabling for the sensors running to each node as short as possible, to reduce noise. Maybe use shielded cable.

I wrote an RS485 non-blocking library - available on Github.

There are some explanations about using it on my website most of which is also on the Github page.

Basically what you would do is give each node an address (stored in EEPROM) and then have a master query each node in turn for the values on its sensors or switches. The node would reply, and then the master could move onto the next one.

I realise this isn't directly answering the question about the Mega shield, but I am trying to answer the underlying problem of how to connect 84 sensors and switches to one Arduino reliably.

  • Amazing Nick - thank you for the comprehensive write up. I have heard about RS485, and will now look in to this much more. Appreciate your assistance.
    – PaulN
    Jul 13, 2023 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.