Yes, sorry I’m resurrecting an old thread… But this thread was referenced in a new thread so I thought it best to put my explanations here instead of there. My apologies to the forum gods if I offend.
[quote author=Nick Gammon link=topic=102716.msg770902#msg770902 date=1335243369]
[/quote]Long bits snipped for brevity.
As I implied earlier you haven’t shown that it is an output pin. If it was not, you could write HIGH to it and read LOW, because it was not set to output. Or you might write LOW and read back HIGH.
With this test (and nothing connected):
Long bits snipped for brevity.
So the writing LOW is not having an effect.
On initial start, the pins are all inputs. Doing a digitalWrite() on an input pin just enables (HIGH) or disables (LOW) the internal pullup.
On the example you demonstrate (yes, Nick, you probably know this, but I’m explaining for others here), because you never initialize the pinMode of pin 10, it is an input. Then setting it HIGH with digitalWrite() enables the internal pullup resistor enforcing the HIGH state because you explicitly stated nothing is connected to the pin (it’s floating). Then in the first iteration of the loop you send a !digitalRead() which would evaluate to LOW, effectively turning off the internal pullup. Now the pin is truly floating. Floating pins on digital chips usually have a tendency to drift towards HIGH as it picks up stray EMF either directly on the die or from the physical pin. Because the pin is HIGH already, it probably just hangs out around the HIGH status, never drifting low enough to trigger a LOW state. Thus, the next iteration of the loop again evaluates to LOW, attempting to turn off the already off internal pull up. This keeps happening ad infinitum. (This is why liudr made the assumption that the pin may go LOW if you touch it, but then that would then be connecting something to the pin.)
Just a bit of hardware electrical theory for your Monday. 8)