[SOLVED] Microswitch reading as both low and high?

This is my first post, so hello everyone!

I'm really at a loss. I have a microswitch, which is true to this diagram. I am using the Bounce library, the change example. When I try to get input from the switch, the controller detects it as active when it isn't. I have 5v connected to the common contact, and my digital input connected to the normally open contact.

For anyone unfamiliar with the Bounce code:

void startSweep(){  
int sweepRelayValue = LOW;
  
  if (resetBounce.update()) {
     if (resetBounce.read() == HIGH) {
       if (sweepRelayValue == LOW) {
         sweepRelayValue = HIGH;
       } else{
         sweepRelayValue = LOW;
       }
       digitalWrite(sweepRelay, sweepRelayValue);
     }
   }
}

It's only slightly modified from the original bounce example. Instead of 'LED', the variable to switch on is 'sweepRelayValue', and 'button' is 'resetBounce'. I've tried different switches of the same kind, and adjusting the software in all sorts of ways. This must be one of the times I"m overlooking something small, or just doing it wrong. Any help would be appreciated, thanks!

I have 5v connected to the common contact, and my digital input connected to the normally open contact.

You have what is called a 'floating input pin' condition when the switch is not being operated. A floating input reads not as a valid high or low, but rather switches around with electrical noise. You need to add a pull-down resistor from the digital input pin to ground, anything from 1k - 20k ohms would be fine. That will supply a valid low signal when the switch is not operated.

Lefty

retrolefty:

I have 5v connected to the common contact, and my digital input connected to the normally open contact.

You have what is called a 'floating input pin' condition when the switch is not being operated. A floating input reads not as a valid high or low, but rather switches around with electrical noise. You need to add a pull-down resistor from the digital input pin to ground, anything from 1k - 20k ohms would be fine. That will supply a valid low signal when the switch is not operated.

Lefty

Okay, that makes sense. So I should add the resistor to ground, attached to the digital pin. What's the best way to go about that? Should I splice the resistor into the input pin wire, attach that to ground, and attach the digital pin wire as normal?

I appreciate the help! :smiley:

IUNIXI:

retrolefty:

I have 5v connected to the common contact, and my digital input connected to the normally open contact.

You have what is called a 'floating input pin' condition when the switch is not being operated. A floating input reads not as a valid high or low, but rather switches around with electrical noise. You need to add a pull-down resistor from the digital input pin to ground, anything from 1k - 20k ohms would be fine. That will supply a valid low signal when the switch is not operated.

Lefty

Okay, that makes sense. So I should add the resistor to ground, attached to the digital pin. What's the best way to go about that? Should I splice the resistor into the input pin wire, attach that to ground, and attach the digital pin wire as normal?

I appreciate the help! :smiley:

That would work, but whatever works out easier for you. You could wire it from the normally open contact and any shield ground pin.

retrolefty:

IUNIXI:

retrolefty:

I have 5v connected to the common contact, and my digital input connected to the normally open contact.

You have what is called a 'floating input pin' condition when the switch is not being operated. A floating input reads not as a valid high or low, but rather switches around with electrical noise. You need to add a pull-down resistor from the digital input pin to ground, anything from 1k - 20k ohms would be fine. That will supply a valid low signal when the switch is not operated.

Lefty

Okay, that makes sense. So I should add the resistor to ground, attached to the digital pin. What's the best way to go about that? Should I splice the resistor into the input pin wire, attach that to ground, and attach the digital pin wire as normal?

I appreciate the help! :smiley:

That would work, but whatever works out easier for you. You could wire it from the normally open contact and any shield ground pin.

Just added a pull-down to my breadboard (10k), on the same rail as the digital input, going to ground. My solid state relay now turns it's AC load on reliably and stably, instead of intermittently which is bad for the motor. This solves my biggest issue with my current project, thanks for your help!

Your welcome. Remember only in software does a bit value always have a high or low value. Once you enter the world of electronics the rules are well different and it depends, etc. :wink:

Lefty