Confusion over HIGH LOW with limit switches

Hi
Can someone please advise what's happening here.
The below code tells pin 11 to go HIGH if A0 is HIGH.
It does this and puts power to a relay but the Serialprint says pin11 is low "0".

Pin 11 "A0" is wired to a limit switch with an external 10k PULLUP resistor.

Shouldn't the serialprint be "1"?

int azLimitSwitch;

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  //pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop() {
  azLimitSwitch = digitalRead(A0);
  Serial.print("LIMIT SWITCH STATUS =.....");
  Serial.print(azLimitSwitch);
  Serial.print('\n');

  if (azLimitSwitch == HIGH) {

    digitalWrite(11, HIGH);
    //digitalWrite(12, LOW);
  } else {

    digitalWrite(11, LOW);
    //digitalWrite(12, HIGH);
  }
}
  • How do you have A0 wired ?

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.

Your sketch never prints the status of pin 11.

It does print the status of A0.

If A0 is grounded (ie LOW), it will print LIMIT SWITCH STATUS = .....0.

If A0 is pulled high (ie HIGH), it will print LIMIT SWITCH STATUS = .....1.

If you aren't using a board with native USB, there will however be a significant delay between the level on A0 changing and that change being reflected in the serial monitor. This is caused by printing the level every time loop() executes and using such a slow baud rate. You'll have to wait until all the old statuses are sent and shown before seeing the current value.

Come in to the 21st century. Use 115200 unless you have a very good reason not to. And only print the switch status when it changes.

More confused now thanks.

Since it has pullup, it reads high when switch is open
Most relays are triggered with low signal, so your circuit behavior is normal.

1 Like

Then I'll trouble you no further. Good-bye.

1 Like

I built your project here. I added a delay in the loop just to slow down the printing.

and it seems to function.

Does it do something different to what is in from of you? Does it do what you want?

a7

I Hope that's a typo.

Thank you.
What you sent does what I expect it to do.
I get 0 printed if the switch is not pressed (closed) and 1 when pressed (open)

Cool program your using there.

Is that push button NC?

Thank you..
That's all it needed.

No.

Exactly wrong. The pushbutton is closed (conducts) when it is pressed, and is open (does not conduct) when not pressed. NO pushbutton.

Pulled high, the input normally reads HIGH. When you press the button, you are connecting the pin to ground, which will make the pin read LOW.

There are a few things like this, usually turning something upside down is a matter of fixing the issue close to where it happens:

  bool buttonIsPressed = digitalRead(someButtonPin) == LOW;  // pin pulled up; LOW is pressed

and you can plan ahead by having one line describe the situation:

# define PRESSED  LOW  // pulled up input.

and always

  bool buttonIsPressed = digitalRead(someButtonPin) == PRESSED;

so the one line can account for normally open or normally closed contacts, as well as a pin pulled either high or low BOOM.

a7

Hi Thanks for taking the time with this and your explanation.
I have fried my brain going over this today and I am still not fully grasping it.
I need to look again tomorrow

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