Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

digitalRead on NodeMCU user switch #123

Closed
hallard opened this issue Apr 26, 2015 · 7 comments
Closed

digitalRead on NodeMCU user switch #123

hallard opened this issue Apr 26, 2015 · 7 comments

Comments

@hallard
Copy link
Contributor

hallard commented Apr 26, 2015

Hi there,

I've got a NodeMCU board with a flash switch and a user switch. The user switch is set as follow

image

R3 is not populated and here the full schematic

My sketch is as follow

#include "ESP8266WiFi.h"

void setup() 
{
  Serial.begin(115200);
   pinMode(16, INPUT);
}
void loop() 
{
  Serial.print("GPIO16=");
  Serial.println(digitalRead(16));
}

As soon I launch it, GPIO16 is displayed as 1 on serial then I push the user switch, then GPIO16 is displayed as 0, and then GPIO16 never get back to 1 when I release the switch. Is there something I missed or it is related to internal hardware (I could not find a descent datasheet) ? Of course I also tried with INPUT_PULLUP with same results

Thank you for your help

@GerryKeely
Copy link

Hi
I think it's to do with the do with the way digitalRead for pin 16 is handled.if you look at the file "core_esp8266_wiring_digital.c" you will see that digitalRead for pin16 is different (around line 113)
Gerry

@hallard
Copy link
Contributor Author

hallard commented Apr 26, 2015

Gerry,

you're right, this pin have special function (wake)
image

But this pinout says I should be able to use it as normal I/O pin (no interrupt ok) I'd like to find a datasheet as good as the ATMel ones which tell us what we can do on each pin and what we can't ;-)

@hallard
Copy link
Contributor Author

hallard commented Apr 27, 2015

Ok, @GerryKeely pointed me on the right way, I investigated on nodemcu source, key switch is mulplipexed with onboard led, this is really well done, just one pin for switch and LED. The trick is to do things as follow (set gpio16 to input to read then go back pin to output)
the following function (grabbed from NodeMCU platform firmware) does all the stuff, you call it with parameter to (LOW) to light the LED and HIGH to set the LED off, in return you have 0 if switch is pressed else 1

// KEY_LED functions 
uint8_t key_led( uint8_t level)
{
  uint8_t temp;
  digitalWrite(16, 1);
  pinMode(16, INPUT);
  temp = digitalRead(16);
  pinMode(16, OUTPUT);
  digitalWrite(16, level);
  return temp;
}

following my original sketch is now

#include "ESP8266WiFi.h"

// KEY_LED functions
uint8_t key_led( uint8_t level)
{
  uint8_t state;
  digitalWrite(16, 1);
  pinMode(16, INPUT);
  state = digitalRead(16);
  pinMode(16, OUTPUT);
  digitalWrite(16, level);
  return state;
}

void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  // blink NodeMCU onboard led 
  uint8_t led = ((millis() % 1000) < 500);

  // Set the LED and read switch
  uint8_t gpio16 = key_led(led);

  Serial.print("GPIO16=");
  Serial.println(gpio16);
}

works like a charm

@linagee
Copy link
Contributor

linagee commented Apr 29, 2015

Today I learned: NodeMCU has a way to tie GPIO16 and nRST together. (You need this to get out of ESP.deepSleep.) Just need to add that non-populated resistor R3!

@hallard
Copy link
Contributor Author

hallard commented Apr 29, 2015

@linagee
glad you learned, I think we all learn everyday with these devices ;-)
A quick question for you, If I populate R3 does this have any incidence on program if I do not use deep sleep ?

@linagee
Copy link
Contributor

linagee commented Apr 30, 2015

If you populate it, you should not really be using GPIO16 for anything else unless you want it to reboot every time.

@hallard
Copy link
Contributor Author

hallard commented May 2, 2015

@linagee,

Thanks, looking at the schematics, makes sense, except if I want to do a soft reset or use the user switch as real reset :-)

@hallard hallard closed this as completed May 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants