I played around a bit with Serial.println(digitalRead(2));
Smashed a knob switch between the circuit and let digital pin 2 be the GND for this circuit (because it has a resistor in it as I know). No I was reading in values.
If switch connects the circuit -> "1" is read.
If switch disconnects the cirtuit -> "1" or "0" is read on a arbitrary way.
I know about that while reading the documentation. So I was aware that this will happen.
Then I added a resistor which leads to GND and before that resistor I connect the digital pin wire to the circuit.
Now my questions. Since the digital pin will read arbitrary values IF the switch is OFF (disconnecting) - how come that the digital pin still reads constant "0" and no arbitrary values if the circuit is disconnected?
All I know is that digitalPin in Read mode leads to GND and has a high resistor. But if the digital pin seems connected (like the serial.read(2) proofs) it has to be coming from a power source because the new resistor needs to GND.
But how can a pin have a GND connection and a Power connection at the same time? Is there a specific component that switches to a connection depending on some conditions? Like the resistor? connecting to GND?
But how can a pin have a GND connection and a Power connection at the same time?
If these connections are a wire the power supply is shorted out and can cause too much current to flow.
If these connections are a resistor, the current is limited to a safe level.
A resistor of a certain value (10K), is sufficient to make a floating pin: high or low, depending if it is connected to +5 or GND.
You can compare is to a ball in the back of your truck. While driving the ball will roll left and right all the time. But is you add a rubber band to the ball and connect it to the left side the ball will stay there. But even with the rubber band if you grab the ball and apply some force you can still move it to the right. But if you let it go it will return to the left.
Just to add a little point here, on the logic of the connection.
It's your choice to use the resistor to ground or to 5V. In the first case, the pin will be low under normal circumstances, and high when the switch takes it to 5V. Or, secondly, the pin will be high normally and taken to ground for low when the switch is closed.
So you need to be aware of which way it's wired so you can "look" for the right value with your digitalRead().
It's usual to pull the pin high, using a so-called pullup resistor, and it's for that reason that the Arduino has pullups built in. Closing the switch takes the pin low, and this is usually known as "active low".
You can enable the internal resistors with pinMode(myPin, INPUT_PULLUP), thus doing away with the need for external resistors.