# Replicating 'On Pin Touched' for pins 8,12,14,15,16; Why does this work?

I would love help understanding why in the world this works from a physics/electronics perspective as I’m totally stumped. Can someone please help explain??

in MakeCode block editor, I put in a forever loop

forever {
digitalWrite [Pin 15] to (1)
if <(digitalRead [Pin 15]) = (1)>:
Play tone (middle C) for (1/2) beat
}

Now, when I touch Pin 15 with my finger it makes the sound.
Sometimes I need to be also touching GND (particularly when the microbit power source is only a battery pack - but, not always). And sometimes I do not need to also be touching GND pin (particularly when the microbit is connected to a computer via usb).

I have created an 8 key musical conductors keyboard this way with pins 0,1,2,8,12,14,15,16

Please help explain why this actually behaves like a “on Pin touched” and (if possible) why sometimes I need to be also touching GND and others not.

Thank you!

I think:

1. The `digitalWrite()` puts the pin in an output mode and writes a 1 (3V on the output — That is, 3V relative to the GND).
2. Then the `digitalRead()` puts the pin in an input mode, which disables the output mode…The 1 you wrote is no longer relevant. Since Pin 15 is not connected to anything, it’s a “floating input” and may be impacted by anything that induces a voltage.

This idea of a “floating input” is related to voltage being a relative measure rather than absolute. The “ground” isn’t absence of electricity, it’s just where we decide to start measuring from. The 3V is “3 more than at GND”, not 3 of something in total.

Here are two things that may help you understand this idea of “floating inputs” a bit better:

1 Like

Thanks Bill! This was helpful, yet revealed deeper confusion after some experimentation.

Oddly,

WITH digitalWrite() in the loop, the pin does reliably read 1 only when I touch it (while touching GND);
WITHOUT the digitalWrite() block in the forever loop, the digitalread() on that same pin (pin 15 for example) does not output 1 - and is unaffected by me touching it.

Weirdness 1:
With digitalWrite() in the loop, it behaves like ‘on pin touched’ and without digitalWrite() in the loop, it doesn’t.

Weirdness 2:
With digitalWrite() in the loop;
When connected to power via 3V/GND pins (external battery pack): briefly connecting the relevant Pin to GND directly does not change digitalRead() output from 0 to 1 but, having my finger in between does reliably change digitalRead() output from 0 to 1.

Weirdness 3:
With digitalWrite() in the loop;
When micro:bit is connected to power via USB: the other pins (like Pin 15) are responding like capacitive touch - that is, I don’t also need to be touching GND for digtitalRead() to output 1 - it reliably only outputs 1 when I touch just the pin that is reading with my finger - this is true of pins 0,1,2,8,12,14,15.

Thank you for your help in understanding these very surprising observations!

Ah, interesting…but still pretty normal sorts of stuff for “floating”. I suspect the `digitalWrite()` may leave a stray voltage on the output pin, somewhat like a capacitor. (Just guessing…this isn’t my area of expertise). That would explain the consistency of writing a 1 and then touch+ground changes it.

Weirdness 3 just seems to be part of the nature of “floating”.

You probably should avoid anything that depends on “floating”, because often it truly is erratic. I teach a class where I have several students do tests on floating pins on Arduinos: The same code may behave a little different for different people or on different Arduinos.

Bill

This is extremely helpful, thanks a million!

I will definitely play with floating pins on my Arduino now as well. Very good to know that they are not dependable and typically not this consistent.

Have a fantastic week, your help is much appreciated!