digitalRead causes Leonardo to freeze.

I tried to follow the tutorial called DigitalReadSerial. However, when I upload the sketch to my Leonardo, the board stops working. The TX LED goes into a state where it is either permanently on or permanently off. On the computer's side, the serial port disappears (I can momentarily get it back by resetting the board).

After testing a few different sketches, I determined that the problem occurs when I use the digitalRead function. Is there something wrong with this board?

Post your code and your schematic (circuit inter-board layout).

I followed this tutorial:

With the same schematic as in the tutorial, I tried the example program (with the read pin set to 2) on this page:

If you upload a simple sketch like 'blink', does that upload and run OK?

Do you get the same problem with your DigitalReadSerial if you run it on a bare Leonardo i.e. with no hardware at all connected?

Blink works. The example in my second link works if I replace the digitalRead statement with a constant value. I've also tried to read from different pins, but it seems that I have the problem with all of them. Also tried with and without components attached to the pins; no luck.

I'll have a friend (who has had success with his project) try to program it next week. If there is a problem on the computer side of things, then that would make it clear.

Hi guys. I think I have the same problem. I have a "4 states switch" connected to my board like follows: common leg connected to ground. Each of the 4 "state" legs is connected to another digital pin (2-5, respectively).

Now, when I sample only once a second, it works ok. When I sample every loop - the board freezes.

I used the following example :

Here's my code:

// pins layout
const byte switchIn1 = 2;   // the pin for switch 1
const byte switchIn2 = 3;   // the pin for switch 1
const byte switchIn3 = 4;   // the pin for switch 1
const byte switchIn4 = 5;   // the pin for switch 1

long time = 0;         // the last time we've checked switch state

void setup() {

        pinMode(switchIn1, INPUT);
        pinMode(switchIn2, INPUT);
        pinMode(switchIn3, INPUT);
        pinMode(switchIn4, INPUT);        
         while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

int getSwitchState() {

      return 0;

void loop() {
      if (millis()-time > 1000) {
                int currentProgramNumber = getSwitchState();
                time = millis();  

Any ideas ? Thanx.