Strange I/O problem with ESP8266 NODEMCU V3

I have what seems like a strange problem. My NODEMCU is connected to switches that close to GND when activated. I am using internal pullups on D1 & D2. Both switches are open. The voltage on D1 and D2 measures 3.14V. When I test these pins they both show LOW. Relevant code below.

const int Open_Lim_Pin = D1; //D1 GPIO5 Low when door 102% open
const int Closed_Lim_Pin = D2;  //D2 GPIO4 low when door closed
void setup() {
  pinMode(Step_Pin, OUTPUT); // Sets the three pins as Outputs, two as inputs
  pinMode(Dir_Pin, OUTPUT);
  pinMode(Solenoid_Pin, OUTPUT);
  pinMode(Open_Lim_Pin, INPUT_PULLUP);
  pinMode(Closed_Lim_Pin, INPUT_PULLUP);
......
  if  (Closed_Lim_Pin == HIGH) {
    Serial.println ("Closed limit pin = HIGH");
  } else {
    Serial.println ("Closed limit pin = LOW");
  } 
  if  (Open_Lim_Pin == HIGH) {
    Serial.println ("Open limit pin = HIGH");
  } else {
    Serial.println ("Open limit pin = LOW");
  } 

The serial monitor prints the low result in both cases.
I must be missing something here.

Post your full code and circuit diagram

the most missing thing here is your complete sketch!

without seeing what your code looks like right in the

...........

lines of code it is impossible to analyse what is going on

My suspect is that you are either not assigning the read-in value to your variable

Closed_Lim_Pin

and then you are checking a number very different from 0 or 1 against a 0 or 1
or if you do assign

Closed_Lim_Pin = digitalRead(Closed_Lim_Pin);

then you change the number of which IO-pin you will read in the next time

if you want to use a variable for the checking you have to declare another variable

Hello bbutcher85

You might read the pin as follows:

openLimPin=digitalRead(Open_Lim_Pin)?LOW:HIGH; 

HTH

Have a nice day and enjoy coding in C++.

Your code will not compile, because you have not defined the variables, Step_Pin,
Dir_Pin and Solenoid_Pin (pins):

pinMode(Step_Pin, OUTPUT);
pinMode(Dir_Pin, OUTPUT);
pinMode(Solenoid_Pin, OUTPUT);

Try this code:

const int Open_Lim_Pin = D1; //D1 GPIO5 Low when door 102% open
const int Closed_Lim_Pin = D2;  //D2 GPIO4 low when door closed
void setup() {
  //pinMode(Step_Pin, OUTPUT); // Sets the three pins as Outputs, two as inputs
  // pinMode(Dir_Pin, OUTPUT);
  // pinMode(Solenoid_Pin, OUTPUT);
  pinMode(Open_Lim_Pin, INPUT_PULLUP);
  pinMode(Closed_Lim_Pin, INPUT_PULLUP);
  Serial.begin(115200);

}
void loop()
{
  if  (digitalRead(Closed_Lim_Pin) == HIGH) {
    Serial.println ("Closed limit pin = HIGH");
  } else {
    Serial.println ("Closed limit pin = LOW");
  }
  if  (digitalRead(Open_Lim_Pin) == HIGH) {
    Serial.println ("Open limit pin = HIGH");
  } else {
    Serial.println ("Open limit pin = LOW");
  }
  delay(1000);
}

Thank you. You and a few other folks pointed out that I needed to use digitalRead() to get the value. That solved my problem. I did not realize this was an int variable rather than boolean. I did not post the entire code since it is quite lengthy, and those who correctly pointed out the code would not compile were correct.

This is not the reason why it did not work!

D1 is GPIO 5
D2 is GPIO 4

this means in your earlier code-version

//if  (Closed_Lim_Pin == HIGH)
if  (4 == 1) {
    Serial.println ("Closed limit pin = HIGH");
  } else {
    Serial.println ("Closed limit pin = LOW");
  } 
//if  (Open_Lim_Pin == HIGH)
  if  (5  == 1) {
    Serial.println ("Open limit pin = HIGH");
  } else {
    Serial.println ("Open limit pin = LOW");
  } 

which of course can never be true!!!

what your code-snippet does not show if you did

Open_Lim_Pin = digitalRead(Open_Lim_Pin);
which would mean you assign either
Open_Lim_Pin = 1; // if IO-pin is HIGH
or
Open_Lim_Pin = 0; // if IO-pin is low

and the next time the digitalRead i no longer a
digitalRead(4);
but
a digitalRead(1)
or
a digitalRead(0)

which both read the wrong IO-pin

The difference between your wrong code and the one that works is

if (Closed_Lim_Pin == HIGH) {
which is
if (4 == HIGH)

and
if (digitalRead(Closed_Lim_Pin) == HIGH) {
which is
if (digitalRead(4) == 1) {

where the result of digitalRead(4) can be either 1 or 0
which boils down to
if (1 == 1) { // ==> condition true
or
if (0 == 1) { // ==> condition false

no booleans as values they are 1 or 0
but boolean expressions
1 == 1
0 == 1

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