Arduino Uno: False read from toggle switch

hi everyone,

I'm new here so if I make a mistake, let me know (wrong topic or such) and don't come down hard on me please :slight_smile:

So. My problem is, it looks like Arduino Uno is inconsistent on reading the value of a simple toggle switch (I'm using something very similar to this thing I googled: "MTS-102-E1"). My configuration is super-simple: middle leg on toggle switch: 5V pin on arduino, one of the side legs: pin #2, and that's it.
I tried the 5V pin both without resistor and with a 1K Ohm one, no difference. The arduino is plugged in with USB to a desktop PC.

Please note I'm not playing with the switch. I plug things together, start the arduino and monitor debug logs on the COM port, that's it, I'm not touching anything.

The test code I'm running is:

const int kTOGGLE_SWITCH_PIN = 2;
const int kWRITE_SUM_AFTER_ITERATIONS = 100; //write a summery of right / false values after 'n' read iterations

int switchStatus = 0; //reading switch status into this int
int wrongRes = 0;  //register for wrong reads
int rightRes = 0;  //register for right ones
int iteration = 0; //iteration count

void setup() {
  Serial.begin(9600);
  pinMode(kTOGGLE_SWITCH_PIN, INPUT);
}


void loop() {
  switchStatus = digitalRead(kTOGGLE_SWITCH_PIN);
  if (switchStatus == 0) {
    rightRes++;
  } else {
    wrongRes++;
  }
  Serial.println("The Switch Status is: " + String(switchStatus));
  iteration++;
  delay(500);
  if (iteration == kWRITE_SUM_AFTER_ITERATIONS) {
    Serial.println("Switch value    OK: " + String(rightRes));
    Serial.println("Switch value ERROR: " + String(wrongRes));
    wrongRes = 0;
    rightRes = 0;
    iteration = 0;
    delay(2000);
  }
}

I let it run while I was registering to the forums and writing this message, here are the results:

Switch value OK: 62
Switch value ERROR: 38

And the rest of the runs:
74 / 26; 74 / 26; 76 / 24; 69 / 31; 100 / 0; 96 / 4; 88 / 12; 95 / 5; 75 / 25; 100 / 0; 78 / 22; 93 / 7; 92 / 8; 93 / 7; 100 / 0; 97 / 3

False reads tend to come in batches, 8-10 together, but sometimes a lonely false read pops in, then either nothing for a long time or a batch of 8-10 follows after 3-4 proper reads inbetween.

TBH I'm a little puzzled... My assumption is this should be some voltage mumbo-jumbo as if the switch is the other way around (i.e. I toggle it to ON / HIGH), no errors at all. But how can I make the reads consistently right even with trying to read 0 ("LOW") status?

Thanks for the help!

do you have a pull-up resistor? if not, configure the pin as INPUT_PULLUP

Thanks for coming back! I don't have one. If I set INPUT_PULLUP for the pin, the result is always 1, no matter what the switch status is or if I use a resistor or not.

with a pull-up, the switch needs to connect the pin to ground

gcjr:
with a pull-up, the switch needs to connect the pin to ground

Wow! You are wonderful! Problem solved, thank you so much!!! Still a lot to learn it seems (I'm new to electronics. New hobby)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.