Pages: [1]   Go Down
Author Topic: [SOLVED] Microswitch reading as both low and high?  (Read 1078 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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!
« Last Edit: February 13, 2012, 07:21:38 pm by IUNIXI » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16459
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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-grin
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16459
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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-grin

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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-grin

 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!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16459
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-wink

Lefty
Logged

Pages: [1]   Go Up
Jump to: