Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

NodeMCU ESP8266 pin always low

Status
Not open for further replies.

mrnams

Junior Member level 1
Joined
Jan 3, 2021
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
96
Once we give low input to pin 16 of NodeMCU ESP8266, it remains low, see my code below which is not able to switch off led because of this issue, can't we use pin 16 as general purpose input?
See my complete code

Code:
#include <Arduino.h>
#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif

enum LedStatus
{
  LedOff = 0,
  LedOn = 1
};
enum LedStatus curentLedStatus = LedOff;
#define BAUD_RATE 9600
// Pins of push buttons and led
#define pinPButtonLedOn 16
#define pinPButtonLedOff 5
#define pinLed 4

void setup()
{
  Serial.begin(BAUD_RATE);
  // put your setup code here, to run once:
  pinMode(pinPButtonLedOn, INPUT_PULLUP);  // set arduino pin to input pull-up mode
  pinMode(pinPButtonLedOff, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(pinLed, OUTPUT);                 // set arduino pin to output mode
}

void loop()
{
  // put your main code here, to run repeatedly:
  Serial.printf("\n \n \n  On button state :");
  Serial.print(digitalRead(pinPButtonLedOn));

  Serial.printf("\n \n \n Off button state :");
  Serial.print(digitalRead(pinPButtonLedOff));

  if (digitalRead(pinPButtonLedOn) == LOW && curentLedStatus == LedOff)
  {
    digitalWrite(pinLed, HIGH);
    curentLedStatus = LedOn;
    Serial.printf("\n \n \n Led turned On by Push Button");
  }

  if (digitalRead(pinPButtonLedOff) == LOW && curentLedStatus == LedOn)
  {
    digitalWrite(pinLed, LOW);
    curentLedStatus = LedOff;
    Serial.printf("\n \n \n Led turned Off by Push Button");
  }
}
[moderator action: removed link to external file server]
 
Last edited by a moderator:

Hi,

try:
Code:
  if ( (digitalRead(pinPButtonLedOn) == LOW) && (curentLedStatus == LedOff) )

change the other "IF..." line the same way.

Klaus

***
added:
also check if this is correct:
Code:
enum LedStatus curentLedStatus = LedOff;

maybe change it to:
Code:
int LedStatus curentLedStatus = LedOff;
 

Hi,

try:
Code:
  if ( (digitalRead(pinPButtonLedOn) == LOW) && (curentLedStatus == LedOff) )

change the other "IF..." line the same way.

Klaus

***
added:
also check if this is correct:
Code:
enum LedStatus curentLedStatus = LedOff;

maybe change it to:
Code:
int LedStatus curentLedStatus = LedOff;

Its not problem of logic, i have printed state of pin and for pin 16 it always print 0 i.e. low
Code:
// put your main code here, to run repeatedly:
  Serial.printf("\n \n \n  On button state :");
  Serial.print(digitalRead(pinPButtonLedOn));

  Serial.printf("\n \n \n Off button state :");
  Serial.print(digitalRead(pinPButtonLedOff));
 
Last edited by a moderator:

Yes, but actually it low, thats why led is not turning off
 

1631871522506.png
 

Hi,

LED without current limiting resistor???
Did you check that all connections are valid?

Do you have a voltmeter?

Klaus
 

LEDs are current driven, not voltage driven, even at 3.3V from the NodeMCU it will overload the output pin. Use a resistor.

I can't see in your code where it makes the LED pin high.

Brian.
 
You are seemingly assuming that can use GPIO indexes to refer to I/O's, which is not allways true in the Arduino world.
There is an intrinsic mapping, so instead of using this...

Code:
#define pinPButtonLedOn 16
#define pinPButtonLedOff 5
#define pinLed 4

You should consider using that

Code:
#define pinPButtonLedOn D0
#define pinPButtonLedOff D1
#define pinLed D2
 

Personally, I've stopped referring to the different types of board and use the 'generic' description instead. Partly that's because after using something like a NodeMCU for development, the final product only uses the actual ESP module without the rest of the board.

Aside from the pin number/name issue and the LED and enumerating strangeness, I can't see why there is any need to record the present LED state when it never changes unless a button is pressed. Simply 'if on is pressed turn it on' and 'if off is pressed turn it off' should work equally well. Saving the state is only necessary if it is being toggled with a single switch and the previously recorded condition has to be changed.

Brian.
 

You are seemingly assuming that can use GPIO indexes to refer to I/O's, which is not allways true in the Arduino world.
There is an intrinsic mapping, so instead of using this...

Code:
#define pinPButtonLedOn 16
#define pinPButtonLedOff 5
#define pinLed 4

You should consider using that

Code:
#define pinPButtonLedOn D0
#define pinPButtonLedOff D1
#define pinLed D2
No, that does not make any difference
 

Personally, I've stopped referring to the different types of board and use the 'generic' description instead. Partly that's because after using something like a NodeMCU for development, the final product only uses the actual ESP module without the rest of the board.

Aside from the pin number/name issue and the LED and enumerating strangeness, I can't see why there is any need to record the present LED state when it never changes unless a button is pressed. Simply 'if on is pressed turn it on' and 'if off is pressed turn it off' should work equally well. Saving the state is only necessary if it is being toggled with a single switch and the previously recorded condition has to be changed.

Brian.
You are right, thanks a lot. I am new here, this is my first project of NodeMCU :p
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top