Toggle switch has rapid HIGH/LOW output in ON position

I’m using a 3 prong toggle switch wired through a 1kohm resistor as suggested in this forum thread I read about toggle switches. I had it working fine yesterday, but today, after doing some experimenting with the tone() function, and then reverting back to the original code, my toggle switch now behaves in the following manner (tested first to the connected LCD screen and then through serial out) : in the “off” position, it works as expected, returning a “0” output. In what should be the “on” position, it should be returning “1”, but it returns “0” and “1” in uneven intervals when I have it printing a constant stream to serial, for example, it should return “111111111…” in the on position, it returns “000100010001100001”. I’m curious as to why this is happening now, as I have reverted both the code and wiring to their previously functional state. I’m including my code, the input pin for the switch is digital pin 7, the other lead from the switch is wired directly to ground.

// * LCD RS pin to digital pin 12
// * LCD Enable pin to digital pin 11
// * LCD D4 pin to digital pin 5
// * LCD D5 pin to digital pin 4
// * LCD D6 pin to digital pin 3
// * LCD D7 pin to digital pin 2
// * LCD R/W pin to ground
// * LCD VSS pin to ground
// * LCD VCC pin to 5V
// * 10K resistor:
// * ends to +5V and ground
// * pot to LCD VO pin (pin 3)
// *toggle switch on digital pin 7
// *analog joystick on analog pins 0,1, and 4.
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int xpotPin = 0;
int ypotPin = 1;
int bpotPin = 4;
int sPin = 7;
int xval = 0;
int yval = 0;
int bval = 0;
int switchState = 0;

void setup() {
  lcd.begin(16, 2);
  pinMode(sPin, INPUT);

void loop() {

xval = analogRead(xpotPin);
yval = analogRead(ypotPin);
bval = analogRead(bpotPin);
switchState = digitalRead(sPin);

if (bval<50){
  lcd.print("button on");
} else { lcd.setCursor(0,1);
         lcd.print("button off");}         


Use pinMode(sPin, INPUT_PULLUP); OR put a 10k resistor from pin 7 to 5 volts.

Switching it to INPUT_PULLUP fixed it, thank you!