user16615: I have 64 hall effect sensors (magnetic field sensors) (DRV5023AJQLPG) connected to an Arduino MEGA 2560. For that, I use 48 digital pins and 16 analog pins as digital input pins. The program in the Arduino continuously reads out those 64 digital input pins (in a for-loop) and sends out a serial byte, to a laptop via a usb cable, whenever one of those input pins changes between LOW and HIGH. I also use the ATmega2560’s internal pullup resistors to prevent irregular readouts of the input pins when the sensor is not giving a 0v output (i.e. when it isn’t measuring a strong enough magnetic field).
Everything is working fine when I only physically connect up to 16 of the sensors. The laptop then only receives bytes that correctly tell when a sensor measures a state-change.
However, when I start connecting more of those 64 sensors, then the laptop starts receiving irregular streams of bytes, erroneously suggesting that all the connected sensors measure changing magnetic fields (HIGH and LOW readouts).
I can think of two reasons why these irregular readouts occur:
1. Either, the ATmeag2560’s internal pullup resistors arent working as I think they should.
2. Or, the arduino board and >16 hall effect sensors combined are pulling to much current.

Pullup resistors not working?
When testing with only 16 sensors connected, it is a stable, properly working setup. When I explicitly don’t use the pullup resistors in the program then the setup does start behaving irregular, also with just 16 sensors. So the internal pullup resistors seem to be properly activated by the program. 
Arduino + 64 sensors draw to much current?
Besides powering the Arduino board and sensors via the usb cable (providing 500mA), I’ve also tried external power sources that provide 1 or 2A. I’ve also powered the Arduino board via an external 5v, +1A voltage regulator, bypassing the Arduino’s onboard voltage regulator. This all doesn’t seem to make a difference in the system’s irregular behaviour. When measuring those setup’s current usage (with amperage meter) it didn’t go above 250mA.

Calculating the combined current usage (using datasheet info), I came to this:
- sensor operating current:             2,7 mA
- sensor output current when in ‘active’ state:     0,25 mA
- TOTAL current of 64 sensors   (3mA*64=)       192 mA
- Arduino MEGA current (approx)             150 mA
- TOTAL current of 64 sensors + Arduino MEGA:       **342 mA**

So, both the calculated and measured current usage seem within the range of what can be handled by mentioned power sources.
I’m kinda stuck now. Can anyone suggest anything that might cause this problem? Or point to incorrect thinking of me? Thanks for any responses.

'Schematic' (only showing first 8 sensors connected. The ArduinoMEGA is connected to a computer via a usb cable with disabled power line):

Top-side: the hall effect sensors and Vcc an GND rails:

Bottom-side: ArduinoMEGA + the 64 red sensor output lines to the input pins:

Below is the Arduino code:
/*
Arduino to Pd protocol

 Placing or removing a chess piece sends only one byte.
 There’s no other serial data being communicated.

 event:                     byte value:
 placing a chess piece on a square:     0-63
 removing a chess piece from a square:          64-127

 ArduinoMEGA pin to byte value mapping:
  byte      pin
  0     6
  1     7
  2     8
  3     9
  …
  46        52
  47        53

  48        A0 (analog 0)
  49        A1
  50        A2
  …
  62        A14
  63        A15

*/

byte prevSquares[64];  // array that holds previous readouts
byte i;                // variable used in for-loops
byte sensorValue;      // variable temp storing of digitalread

void setup() {
  Serial.begin(9600);
  for (i = 0; i < 64 ; i++) {
    pinMode(i+6, INPUT_PULLUP);  // activate pullup resistor
  }
  for (i = 0; i < 64 ; i++) {
    prevSquares[i] = digitalRead(i+6);
  }
}

void loop() {
  for (i = 0; i < 64 ; i++) {
    sensorValue = digitalRead(i+6);
    if (sensorValue != prevSquares[i]) {
      // chess piece was placed or removed from square
      if (sensorValue == 0) {
        // chess piece was placed
        // send serial byte (0-63):
        Serial.write(i);
      }
      else {
        // chess piece was removed
        // send serial byte (64-127):
        Serial.write(i + 64);
      }
      //update prevSquares array:
      prevSquares[i] = sensorValue;
    }
  }
}


user11086: After seeing your schematic, I think the general problem here is that you need to more closely read and follow the datasheet for this part. A lot of times, when you are hobbling a project together at a small scale there are a lot of things you can ignore and get away with, but as you scale up, all the rounded corners start to add up. In this case, you can get away with 16, but once you start adding too more, it becomes too much. Let's go over a few:
Pull up resistors
Like I mentioned in the comments, relying on the internal pull ups is probably not a good idea. In my personal experience, they are only really good for adding a quick button input for debugging purposes. I'm not sure specifically what would cause issues only after 16 sensors. It could be that collectively internal pullups are subject to a different and lower current limit, but I'm not familiar enough with the internal architecture of the ATMega2560 to do more than just speculate.
Whatever the case, the best practice here would be to use separate external pull up resistors, within the specifications of the datasheet. (See equation 1 on page 13 of the datasheet.)
Decoupling Capacitor
Per the datasheet, page 17:

A 0.01-µF (minimum) ceramic capacitor rated for VCC must be placed as close to the DRV5023 device as possible.

This capacitor is referred to as C1 in the datasheet figures and is required. This type of capacitor is called a decoupling capacitor, and one of the things it does is provide a local energy storage, so that when the sensor starts pulling a little extra current, it doesn't have to wait for the regulator to respond, it has some local energy already.
Your schematic indicates a single 1uF capacitor on the output of the voltage regulator, however, there are no capacitors for the sensors. The one capacitor is only for output filtering for the regulator, and will not satisfy any decoupling needs of the sensor.
Filtering Capacitor
Referred to as C2 in the datasheet, this an optional capacitor to filter out high frequency noise. This isn't needed in most cases, in fact, per the datasheet, page 13:

Most applications do no require this C2 filtering capacitor.

However, your project might not fall into "most applications" and there are a few things specifically that worry me.

You have long wires that are basically acting as antennas that will capture any nearby noise.
You have long power lines running parallel to your signal lines which can cause stray inductance which leads to other interesting things. (With the thickness of the plywood separating them, this might not be an issue, but it is something to be aware of).

I would definitely try the first two ideas above to begin with, and only pursue this as a last resort. The  only way to know for sure if this is something you need is to stick an oscilloscope on the line and see how noisy it is, but that is not something you probably have handy.
Other Concerns
I'm just a little bit worried about voltage drop. Depending on the gauge of wire used, with all the sensors connected, at the end of a strand you might have a non-negligible voltage drop.
With that many wires running together into the Mega, there might be cross talk problems.
Another common problem with scaling up is that there is a higher probability of having faulty components. This is especially with this part which per the datasheet indicates that is particularly sensitive to ESD. It could be that there is a dead sensor that has failed in such a way that it is causing problems for the other sensors. Maybe go through and test all 64 sensors, 8~16 at a time to make sure that separately they all work, and that the issue only occurs when all of them are connected.
Other Ideas
Many other comments on the question have suggested a multiplexer and/or multiplexing and I will just briefly echo that here. Multiplexers are typically used in situations where you don't have enough pins and you want to split a single pin to multiple other pins. While that isn't an issue here with the Mega (it appears you have enough pins to do what you are wanting to do), there are other benefits that can be derived from multiplexing this.
For example, one thing you could do is split up the board into 8 - 2 x 4 chunks and then have a multiplexer (such as a 4051 or a 74151) local to each chunk. Then you would have just 8 short wires going into the multiplexer, as opposed to having long antenna-like wires heading to the Mega. This could improve overall signal integrity.

16615: <p>I have 64 hall effect sensors (magnetic field sensors) (<a href="http://nl.farnell.com/webapp/wcs/stores/servlet/ProductDisplay?partNumber=2455494" rel="nofollow noreferrer">DRV5023AJQLPG</a>) connected to an Arduino MEGA 2560. For that, I use 48 digital pins and 16 analog pins as digital input pins. The program in the Arduino continuously reads out those 64 digital input pins (in a for-loop) and sends out a serial byte, to a laptop via a usb cable, whenever one of those input pins changes between LOW and HIGH. I also use the ATmega2560’s internal pullup resistors to prevent irregular readouts of the input pins when the sensor is not giving a 0v output (i.e. when it isn’t measuring a strong enough magnetic field).</p>

<p>Everything is working fine when I only physically connect up to 16 of the sensors. The laptop then only receives bytes that correctly tell when a sensor measures a state-change.</p>

<p>However, when I start connecting more of those 64 sensors, then the laptop starts receiving irregular streams of bytes, erroneously suggesting that all the connected sensors measure changing magnetic fields (HIGH and LOW readouts).</p>

<p>I can think of two reasons why these irregular readouts occur:
1. Either, the ATmeag2560’s internal pullup resistors arent working as I think they should.
2. Or, the arduino board and >16 hall effect sensors combined are pulling to much current.</p>

<ol>
<li><p><strong>Pullup resistors not working?</strong>
When testing with only 16 sensors connected, it is a stable, properly working setup. When I explicitly <em>don’t</em> use the pullup resistors in the program then the setup <em>does</em> start behaving irregular, also with just 16 sensors. So the internal pullup resistors seem to be properly activated by the program. </p></li>
<li><p><strong>Arduino + 64 sensors draw to much current?</strong>
Besides powering the Arduino board and sensors via the usb cable (providing 500mA), I’ve also tried external power sources that provide 1 or 2A. I’ve also powered the Arduino board via an external 5v, +1A voltage regulator, bypassing the Arduino’s onboard voltage regulator. This all doesn’t seem to make a difference in the system’s irregular behaviour. When measuring those setup’s current usage (with amperage meter) it didn’t go above <strong>250mA</strong>.</p></li>
</ol>

<p>Calculating the combined current usage (using datasheet info), I came to this:</p>

<pre><code>- sensor operating current:             2,7 mA
- sensor output current when in ‘active’ state:     0,25 mA
- TOTAL current of 64 sensors   (3mA*64=)       192 mA
- Arduino MEGA current (approx)             150 mA
- TOTAL current of 64 sensors + Arduino MEGA:       **342 mA**
</code></pre>

<p>So, both the calculated and measured current usage seem within the range of what can be handled by mentioned power sources.</p>

<p>I’m kinda stuck now. Can anyone suggest anything that might cause this problem? Or point to incorrect thinking of me? Thanks for any responses.</p>

<hr>

<p>'Schematic' (only showing first 8 sensors connected. The ArduinoMEGA is connected to a computer via a usb cable with disabled power line):</p>

<p><a href="https://i.stack.imgur.com/E57Se.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E57Se.jpg" alt="&#39;schematic&#39; (only showing first 8 sensors connected. The ArduinoMEGA is connected to a computer via a usb cable with disabled power line)"></a></p>

<p>Top-side: the hall effect sensors and Vcc an GND rails:
<a href="https://i.stack.imgur.com/cuwYF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cuwYF.jpg" alt="Top: the hall effect sensors and Vcc an GND rails"></a></p>

<p>Bottom-side: ArduinoMEGA + the 64 red sensor output lines to the input pins:
<a href="https://i.stack.imgur.com/0eqiP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0eqiP.jpg" alt="Bottom: ArduinoMEGA + the 64 red sensor output lines to the input pins"></a></p>

<p>Below is the Arduino code:</p>

<pre class="lang-c prettyprint-override"><code>/*
Arduino to Pd protocol

 Placing or removing a chess piece sends only one byte.
 There’s no other serial data being communicated.

 event:                     byte value:
 placing a chess piece on a square:     0-63
 removing a chess piece from a square:          64-127

 ArduinoMEGA pin to byte value mapping:
  byte      pin
  0     6
  1     7
  2     8
  3     9
  …
  46        52
  47        53

  48        A0 (analog 0)
  49        A1
  50        A2
  …
  62        A14
  63        A15

*/

byte prevSquares[64];  // array that holds previous readouts
byte i;                // variable used in for-loops
byte sensorValue;      // variable temp storing of digitalread

void setup() {
  Serial.begin(9600);
  for (i = 0; i &lt; 64 ; i++) {
    pinMode(i+6, INPUT_PULLUP);  // activate pullup resistor
  }
  for (i = 0; i &lt; 64 ; i++) {
    prevSquares[i] = digitalRead(i+6);
  }
}

void loop() {
  for (i = 0; i &lt; 64 ; i++) {
    sensorValue = digitalRead(i+6);
    if (sensorValue != prevSquares[i]) {
      // chess piece was placed or removed from square
      if (sensorValue == 0) {
        // chess piece was placed
        // send serial byte (0-63):
        Serial.write(i);
      }
      else {
        // chess piece was removed
        // send serial byte (64-127):
        Serial.write(i + 64);
      }
      //update prevSquares array:
      prevSquares[i] = sensorValue;
    }
  }
}
</code></pre>
 11086: <p>After seeing your schematic, I think the general problem here is that you need to more closely read and follow the datasheet for this part. A lot of times, when you are hobbling a project together at a small scale there are a lot of things you can ignore and get away with, but as you scale up, all the rounded corners start to add up. In this case, you can get away with 16, but once you start adding too more, it becomes too much. Let's go over a few:</p>

<p><strong>Pull up resistors</strong></p>

<p>Like I mentioned in the comments, relying on the internal pull ups is probably not a good idea. In my personal experience, they are only really good for adding a quick button input for debugging purposes. I'm not sure specifically what would cause issues only after 16 sensors. It could be that collectively internal pullups are subject to a different and lower current limit, but I'm not familiar enough with the internal architecture of the ATMega2560 to do more than just speculate.</p>

<p>Whatever the case, the best practice here would be to use separate external pull up resistors, within the specifications of the datasheet. (See equation 1 on page 13 of the datasheet.)</p>

<p><strong>Decoupling Capacitor</strong></p>

<p>Per the datasheet, page 17:</p>

<blockquote>
  <p>A 0.01-µF (minimum) ceramic capacitor rated for VCC must be placed as close to the DRV5023 device as possible.</p>
</blockquote>

<p>This capacitor is referred to as C1 in the datasheet figures and is required. This type of capacitor is called a decoupling capacitor, and one of the things it does is provide a local energy storage, so that when the sensor starts pulling a little extra current, it doesn't have to wait for the regulator to respond, it has some local energy already.</p>

<p>Your schematic indicates a single 1uF capacitor on the output of the voltage regulator, however, there are no capacitors for the sensors. The one capacitor is only for output filtering for the regulator, and will not satisfy any decoupling needs of the sensor.</p>

<p><strong>Filtering Capacitor</strong></p>

<p>Referred to as C2 in the datasheet, this an optional capacitor to filter out high frequency noise. This isn't needed in most cases, in fact, per the datasheet, page 13:</p>

<blockquote>
  <p>Most applications do no require this C2 filtering capacitor.</p>
</blockquote>

<p>However, your project might not fall into "most applications" and there are a few things specifically that worry me.</p>

<ol>
<li>You have long wires that are basically acting as antennas that will capture any nearby noise.</li>
<li>You have long power lines running parallel to your signal lines which can cause stray inductance which leads to other interesting things. (With the thickness of the plywood separating them, this might not be an issue, but it is something to be aware of).</li>
</ol>

<p>I would definitely try the first two ideas above to begin with, and only pursue this as a last resort. The  only way to know for sure if this is something you need is to stick an oscilloscope on the line and see how noisy it is, but that is not something you probably have handy.</p>

<p><strong>Other Concerns</strong></p>

<p>I'm just a little bit worried about voltage drop. Depending on the gauge of wire used, with all the sensors connected, at the end of a strand you might have a non-negligible voltage drop.</p>

<p>With that many wires running together into the Mega, there might be cross talk problems.</p>

<p>Another common problem with scaling up is that there is a higher probability of having faulty components. This is especially with this part which per the datasheet indicates that is particularly sensitive to ESD. It could be that there is a dead sensor that has failed in such a way that it is causing problems for the other sensors. Maybe go through and test all 64 sensors, 8~16 at a time to make sure that separately they all work, and that the issue only occurs when all of them are connected.</p>

<p><strong>Other Ideas</strong></p>

<p>Many other comments on the question have suggested a multiplexer and/or multiplexing and I will just briefly echo that here. Multiplexers are typically used in situations where you don't have enough pins and you want to split a single pin to multiple other pins. While that isn't an issue here with the Mega (it appears you have enough pins to do what you are wanting to do), there are other benefits that can be derived from multiplexing this.</p>

<p>For example, one thing you could do is split up the board into 8 - 2 x 4 chunks and then have a multiplexer (such as a 4051 or a 74151) local to each chunk. Then you would have just 8 short wires going into the multiplexer, as opposed to having long antenna-like wires heading to the Mega. This could improve overall signal integrity.</p>
