-->
Page 1 of 2

esp12e nodemcu sink current 3.3v to D4 gpio2

PostPosted: Sat Nov 04, 2017 12:46 pm
by danluanodemcu
greetings,

i have a NC reed switch connected from 3.3v to D4 (gpio2) on the nodemcu. i was doing some reading and i found out gpios can only source (supply) 12mA and sink (receive) 20mA. so i hooked up a multi meter between D4 and a potentiometer and the potentiometer to 3.3v to measure how much current was being sunk to D4. it turns out 20mA wasn't enough to change the state of D4 from low to high so that my blynk app reports "door closed". interesting enough, the light for d4 shuts off but it took about 38mA for the state to change. i was hoping to use a resistor in series with the reed switch to limit the current being sunk to D4 from 3.3v to 20mA to stay in spec but it looks like i can't.

if it helps, in arduino i have pinMode(D4, OUTPUT); is this correct?

is it ok to have about 40mA going from 3.3v to D4?

is it possible to have a NC reed switch hooked up to D4 and have the onboard light of the esp12e off when there is a connection (closed circuit) and on with disconnection (open circuit) ? the only way i've been able to do this is to have 3.3v connected with the reed switch to D4 and have D4 as an output in the arduino sketch. but, 40mA is being sunk from 3.3 to D4. i don't get why D4 has to be set as output since current is being sunk into it from 3.3v. it won't work at all if i set D4 as input or input_pullup. you'd think D4 would have to be an input since the current is going from 3.3 to D4?

Re: esp12e nodemcu sink current 3.3v to D4 gpio2

PostPosted: Sat Nov 04, 2017 3:29 pm
by rudy
is it ok to have about 40mA going from 3.3v to D4?


No.

You can not have the pin set as an output and use it as an input. What you are doing will damage the module if it has not been damaged yet.

As an output the maximum for the port is 12 mA (as an output) and that is not recommended.

In order to use D4 as an input you should make sure that it is not set as an output. Then you connect your switch to D4 and ground. Not Vcc. The led will turn on when the switch is closed. (a low) Use the serial port to show you the read of the pin.

Re: esp12e nodemcu sink current 3.3v to D4 gpio2

PostPosted: Sat Nov 04, 2017 4:00 pm
by danluanodemcu
rudy wrote:

In order to use D4 as an input you should make sure that it is not set as an output. Then you connect your switch to D4 and ground. Not Vcc. The led will turn on when the switch is closed. (a low) Use the serial port to show you the read of the pin.


thanks for the reply rudy. what i don't understand is if i connect D4 to ground, wouldn't the current flow from D4 to ground? wouldn't D4 be outputting to ground? hence, D4 is output? this is very confusing.

Re: esp12e nodemcu sink current 3.3v to D4 gpio2

PostPosted: Sat Nov 04, 2017 4:18 pm
by rudy
What you have is a resistor in series connected to 3.3 volts and the other end to D4.

With D4 set as an output and D4 set low, 0, then you have current through the LED. So turning on the led requires a 0. To turn it off you need to write a 1 to D4.

If you had D4 set as an input, with nothing else connected to D4, the led should be off. With this arrangement the led-resistor will sort of put a high on the input to D4 but it isn't a really good high because there is an inherent voltage drop across the led. So it is best to set the port to an INPUT_PULLUP.

With D4 set as an input, and the switch to ground, the current through the switch will be about 5 mA. This is the current through the led-resistor and not current from D4.