[SOLVED] Switch inputs not being read correctly

I'm an idiot, the pins weren't connected correctly.


I've been working on a project and I've been stuck troubleshooting trying to get my microcontroller to read my input properly. I'm using a teensy 3.2 and a DPDT switch.

I currently have 5V out to the middle leg of one side of the DPDT and another leg on the same side connected to pin 2 of my teensy. I've tested my switch and another teensy and both are working fine. I used a multimeter to verify that there was a current going into the teensy.

Here's the code I'm using to test.

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

}

void loop() {
  int sensorVal = digitalRead(2);
  
  Serial.println(sensorVal);
  delay(1500);
}

The controller only prints HIGH regardless of toggle switch position.
Does anyone know what I'm doing wrong?
Thanks!


I've tried using both the teensy included pull-up resistor and an 8.2k ohm pull-down resistor, both the same results except the pull-down would only return 0.

Pictures here:

Pictures

The yellow wire is attached to 5V from the teensy and the red is attached to pin 2.
The resistor is 8.2k ohms, tried both with and without the resistor.

Do you have a pull-down resistor on pin 2 to connect it to ground when not supplied with 5V?

Can you take a picture of your breadboard please? If you're using a button, theyre kind of tricky to wire up.

evanmars:
Do you have a pull-down resistor on pin 2 to connect it to ground when not supplied with 5V?

Sorry I'm new so I'm not sure if this is the right way to reply, but I tried with and without the pull down, I'm uploading pictures of the set up in a second.

beaubeautastic:
Can you take a picture of your breadboard please? If you're using a button, theyre kind of tricky to wire up.

My attachments were too big so I uploaded them here.
The yellow wire is attached to 5V from the teensy and the red is attached to pin 2.

maxeseo:

[b]const int button= 2;[/b]

int buttonState= 0;

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

void loop()
{
 buttonState= digitalRead(switch);  // Read the switch position
 
 if (buttonState== HIGH) {
   Serial.println("HIGH");
   delay(1500);
 }
 else {
   Serial.println("LOW");
   delay(1500);
 }

}




Does anyone know what I'm doing wrong?
Thanks!

what is 'switch' ??
you should read 'button'

alnath:
what is 'switch' ??
you should read 'button'

Sorry my original code has all button, but when I pasted over here I changed everything to switch to kind of eliminate confusion of what I was talking about. Realized that I can't use keyword switch and changed it back and forgot that last one.

To read a switch you must use a pull-up or pull-down resistor, because otherwise an open switch leaves
the pin floating (it can read anything if floating).

Normally you would switch the pin to ground, not 5V, because then you can use the built-in pull-up
resistors:

  pinMode(button, INPUT_PULLUP);

MarkT:
To read a switch you must use a pull-up or pull-down resistor, because otherwise an open switch leaves
the pin floating (it can read anything if floating).

Normally you would switch the pin to ground, not 5V, because then you can use the built-in pull-up
resistors:

  pinMode(button, INPUT_PULLUP);

Thank you!!
I'm currently on my way home so I can't try it now but I'll update as soon as I get back!

from you photo, I can't see if the wiring is correct .

is the contact OK between the resistor and the little red wire ?

And you should use the board and colors as they are supposed to be used .

red is for power "+"
black or blue for 0V
your board has "power lines" red and blue the red one is for "+" the blue one is for 0V

this makes everything easier ....

alnath:
from you photo, I can’t see if the wiring is correct .

is the contact OK between the resistor and the little red wire ?

And you should use the board and colors as they are supposed to be used .

red is for power “+”
black or blue for 0V
your board has “power lines” red and blue the red one is for “+” the blue one is for 0V

this makes everything easier …

The contact is fine, the multimeter reads voltage and current through it. I’ve tried to fix to conventions, sorry I’m new to this.

Updated pics here:

MarkT:
To read a switch you must use a pull-up or pull-down resistor, because otherwise an open switch leaves
the pin floating (it can read anything if floating).

Normally you would switch the pin to ground, not 5V, because then you can use the built-in pull-up
resistors:

  pinMode(button, INPUT_PULLUP);

Still getting the same results from the built-in pull-up, except only returning 0's.
Simplified the code to:

int const button = 2;
void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);

}

void loop() {
  int buttonState= digitalRead(2);
  
  Serial.println(buttonState);
  delay(1500);
}

Picture 2 is my set-up
Pictures

as far as I can see on your photo, the red wire is connected to pin1 , not to pin2
it should be on the next pin

alnath:
as far as I can see on your photo, the red wire is connected to pin1 , not to pin2
it should be on the next pin

So I've been trying to figure this out for the past 4 hours... and now I feel like a complete idiot.

This was completely the problem and I never bothered to check my pin connections...

Thank you so much, I think I'm going to go cry now.

:slight_smile:

being focused on one thing prevents you to see what is around :wink:

that's why a picture is important