Toggle Switch on Digital Input not sustaining HIGH return when closed

Hello,

I'm just doing a quick test with a basic ON/OFF toggle switch on the Mega 2560r3. I have it wired with a 10k resistor, following the circuit diagram attached (however i am currently using pin 50).

I have my code set to Serial.print the digitalRead state of pin 50. As far as i know, when the switch is open, it should be continuously printing 0 (LOW), and when the switch is closed, it should be continuously printing 1 (HIGH).

However, when I close the circuit, it only outputs 1 for about a second before it goes back to 0, even while the circuit is still closed, like this:
00000000011111111000000000000000000000000000000000000000
Screenshot of serial monitor attached.

So far I've removed the physical switch from the equation, and used an actual wire to jump the circuit closed to the 5V rail on my breadboard. This has the same results, so the switch itself is not the problem.

I need this switch to sustain its state, because it will be used in the main project to change an LCD character display between "Light/Dark" modes (for day/night operation, will dim the LCD and change the color from red/green). Whenever the circuit is closed I need it to read 1/HIGH, and whenever it's open I need it to read 0/LOW.

What am I missing here?

Code I'm using for testing:

const int buttonPin = 50;  
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}
void loop() {
  Serial.print( digitalRead(buttonPin) );
  delay(200);
}

EDIT: I added basic LED control to the code, so it will turn the LED on/off with the state of pin 50. See a video at this link:

2.png

(deleted)

That seems to have made the difference. Serial monitor output:

11:10:15.175 -> 1
11:10:15.378 -> 1
11:10:15.578 -> 1
11:10:15.784 -> 1
11:10:15.985 -> 1
11:10:16.190 -> 1
11:10:16.396 -> 1
11:10:16.601 -> 1
11:10:16.804 -> 1
11:10:16.974 -> 1
11:10:17.179 -> 1
11:10:17.380 -> 0
11:10:17.583 -> 0
11:10:17.787 -> 0
11:10:17.989 -> 0
11:10:18.191 -> 0
11:10:18.394 -> 0
11:10:18.594 -> 0
11:10:18.798 -> 0
11:10:18.999 -> 0
11:10:19.203 -> 0
11:10:19.407 -> 0
11:10:19.609 -> 0
11:10:19.811 -> 0
11:10:19.982 -> 0
11:10:20.186 -> 0
11:10:20.391 -> 0
11:10:20.590 -> 0
11:10:20.797 -> 0
11:10:21.001 -> 0

I'm going to go change it from INPUT to INPUT_PULLUP in my other code:

int buttonState = 0;
const int ledPin = 2;  
const int buttonPin = 50;  

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  Serial.print( buttonState );

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  delay(200);
}

EDIT:
That worked!!! Thank you very much! I'm going to go read up on INPUT vs INPUT_PULLUP now, thanks again!!

:smiley: :smiley: :smiley: :smiley: :smiley:

So far I've removed the physical switch from the equation, and used an actual wire to jump the circuit closed to the 5V rail on my breadboard

Fixing the problem by using INPUT_PULLUP has not provided an explanation of what the problem actually was.

What happens when using the original code if you jumper the input pin directly to 5V ? I would be suspicious of the breadboard and the connections to it

To be honest, I'm not sure what the problem was anymore either. I haven't been able to reproduce the problem since I rewired it per the instructions in your first reply- I put everything back the way I had it, and now it's still functioning properly. In fact, using INPUT vs INPUT_PULLUP doesn't seem to make a noticeable difference either.