How does digitalRead() work exactly?

I'm new to Arduino and this forum; hopefully this is the correct place to put my question.

I am just learning and have a very simple circuit, wherein pin 2 is reading an input. If it receives a LOW input, channel 9, which is connected to an LED, gives a LOW output signal. Otherwise, channel 9 outputs a HIGH signal, and the LED receives current and lights up. My question is this: When nothing is connected to pin 2, not even a wire, it is technically receiving no current, and therefore should not allow the LED to light up, right? However, the LED is lit. Also, when I connect pin 2 to ground, however, the LED stops glowing. What is the physical difference between the 2 scenarios? How does the microcontroller "know" that channel 2 is connected to ground when in both cases it is receiving no current?

When a pin is not connected, it is influenced by all the electromagnetic fields that surrounds it. We say that it "floats". It may be HIGH or LOW depending on what is around the pin.

Try moving your hand over your Arduino and you might see the LED light up and close as you do.

When you connect it to ground, you force that pin to go LOW.

Hope this helps.

Jacques

I see. Yes, I experienced exactly what you're referring to the other day; it was quite weird! Thank you