How can I tell when a pin is receiving current?

I am working on an arduino project that involves some basic circuits and LED's.

I have one basic circuit that goes from pin 3 (which is set to high), to a pushbutton, and into pin 4. What happens next will be determined (with an "if" statement) by whether pin 4 receives current (by the button being pushed) or not.

I have tried both google and youtube and could not find anything that would help.

What code would I use to determine if current is flowing into pin 4?

Thanks.

What code would I use to determine if current is flowing into pin 4?

digitalRead() will tell you whether the voltage on the pin is above the threshold to be considered HIGH, or not. Nothing on the Arduino measure current.

Wonderful, Thank you so much.

If current is flowing into the pin, then your Arduino is probably broken.

And your scheme for connected one pin to another via a push-button is conceptually flawed. If the purpose is to detect whether the button has been pressed, it won't work.

Try this as an example

byte pin3 = 3;
byte pin4 = 4;
byte pin13 = 13;
void setup(){
pinMode (pin3, OUTPUT);
pinMode (pin4, INPUT_PULLUP);
pinMode (pin13, OUTPUT); // onboard LED
}
void loop(){
// connect pin 3 to 4
if (digitalRead(pin4) == LOW){
digitalWrite (pin13, LOW);
}
else{
digitalWrite (pin13, HIGH);
}
digitalWrite (pin3, HIGH);
delay(250);

}

Now the L LED will follow the output of pin 3 high or low using pin 4 as an input.
Same thing can be done using a push button to Gnd on pin 4 instead of the output of pin 3. The weak internal pullup resistor (20K to 50K) on pin 4 will hold it high until something takes pin 4 low.

That software needs a little work, I forgot to put anything to make pin3 change state. Easily added tho.
Change this line:

digitalWrite (pin3, HIGH);

to
PIND = 0b00001000; // toggle D3 output by writing 1 to input register

IF

pin 3 is set low,

AND

pin 4 has its pullup resistor turned on,

THEN

pin 4 could detect the state of the pushbutton (LOW == not pushed, HIGH == pushed)

...but I agree that this is not a great strategy. Thee are better ways to wire things.

If pin 3 is an output, then when pin3 is connected to pin 4 (via a jumper, or via a push button), pin 4 will go high & low following pin 3 whether pin 4 has its pullup turned on or not. The pullup will make pin 4 go to a known state when pin 3 is disconnected, and not float high & low yielding random results.

CrossRoads:
PIND = 0b00001000; // toggle D3 output by writing 1 to input register

I thought PIND would only read the value of Port D. I thought you need to use PORTD = xxxx to write to it.

And I'm not in favour of blindly writing 0s to all the irrelevant bits in case they are in use for something else.

digitalWrite(3, ! digitalRead(3))may be more easily understood by the OP.

...R

Writing a 1 to a bit in the PIN register will TOGGLE the associated bit in the PORT register - the zeros will have no effect on the rest of the bits in the PORT register.

Thanks Tom,
Now that you mention it I vaguely remember reading about that in the Datasheet.

I must remember it - its a neat way of doing that, and much faster than digitalWrite().

...R