INPUT pins are always HIGH even if clearly setted LOW

As the title says the code below keeps printing "1", why? how can i resolve? Thanks :')

void setup() {
  Serial.begin(9600);
  pinMode(D3,INPUT_PULLUP);  
}

void loop() {
  digitalWrite(D3,LOW);
  int value = digitalRead(D3);
  Serial.println(value);
  delay(2000);
}

If I recall correctly, on an Uno R3, once a pin is declared as an input, writing to it only serves to turn on or off the pull-up, nothing more. And an input pin w/o the pull up could float high or low.

Though I suspect from your use of D3 that you may not be using an Uno R3, so even that may not be true. But even so, with the pin declared as an input, writing to it will not likely have the effect you appear to think it should.

Edit: if perchance you're using an ESP8266 board of some type, D3 is likely GPIO0, which is physically pulled up on the board.

2 Likes

Oh thanks for the infos bro. btw im using an esp8266 board but i've tried with D3,D4,D5,D6 and D7 and no one works. In OUTPUT mode its all fine, in INPUT starts problems

Hi @radiatesenga ,

Welcome to the forum..

in output mode, you're in control..

in input mode you're a receiver and with no defined state, you float or pick up the local radio station broadcast..

you don't typically digital write to your inputs..
but yes, you can read the states that you write to outputs..

happy coding.. ~q

I moved your topic to an appropriate forum category @radiatesenga .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

On most Arduinos INPUT_PULLUP ensures that you will read a HIGH unless the input is connected to GND by a low resistance, switch or wire.

What do you mean by "clearly settled LOW"?

Go figure.

The INPUT_PULLUP mode charges the pin socket.
Then the digitalWrite( pin, LOW ) turns the pullup off... but unless the socket is drained, each read samples 1 microamp as HIGH.

A jumper to ground would drain that pin in one cycle.

Hey OP! See how many reads it takes to drain the charge down to less than 1.1V using a loop. Should be 1000's, maybe a lot of 1000's but enough reads will get a LOW result!

1 Like

@radiatesenga what is the reason you are using digitalWrite() with an input?

1 Like

I changed digitalRead to analogRead to see if it was floating current that made pin HIGH. The value of analogRead(pin) its always 1024 (the maximum :smiling_face_with_tear: ). If I connect the pin to GND it become LOW but it doesnt even comeback to HIGH with input...

With no jumper I call that charge static rather than floating.

Read the pin enough times at 1 uAmp per read, it will go LOW as long as nothing recharges the charge. And that has uses!

1 Like

there is a jumper that connect board pin to a data pin of yk04 radio receiver, the circuit is closed... arrrhhhhh

Writing to a pin set as an input has no effect on the pin state

@jim-p
It doesn't change between INPUT LOW and INPUT HIGH?
That is the PORTx register. The other 2 are DDRx and PINx.

The forum no longer addresses Replies to the post replied to!
Perhaps carrying the links has been a forum overhead problem?

Ignoring all the other responses - all valid - I'm wondering what you expect to achieve with this code? Do you just not understand about digital i/o?

I'd suggest you connect an LED and series resistor to pin D3 to show the effect (if any) of the digitalWrite when the pin is defined as an input.
Part of the issue is that the actual operation of the pinMode and DigitalWrite instructions is obfuscate by compilation. Using direct write to the I/O registers would be more transparent.

ESP8266 doesn't have those registers

1 Like

Ok so guys I resolved. It seems that the problem was my yk04 radio receiver, which pin D0 I used to close the circuit. This pin is always HIGH as it seems and not respond to trasmitter input (but that's another problem)... so the solution was simply to connect D3 pin to another pin of yk04. Yeah, it was a pretty stupid problem with my yk04. Thank u all btw for responding :smiling_face_with_tear:

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