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

**URL:** https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845
**Category:** micro:bit
**Created:** [September 1, 2021, 10:23pm UTC](https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845 "2021-09-01T22:23:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![TinyAnts](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TinyAnts](https://forum.makecode.com/u/TinyAnts)
#### Post date: [September 1, 2021, 10:23pm UTC](https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845/1 "2021-09-01T22:23:58Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [September 2, 2021, 3:04am UTC](https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845/2 "2021-09-02T03:04:10Z")

</div>

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:

- [AdaFruit’s CircuitPlayground Floating Input](https://learn.adafruit.com/circuit-playground-digital-input/floating-inputs)

- ProgrammingElectronics has a good video: [Floating Pins, Pull-Up Resistors and Arduino](https://www.programmingelectronics.com/floating-pins-pull-up-resistors-and-arduino/)

---

<div class="post-metadata">

### Author: ![TinyAnts](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TinyAnts](https://forum.makecode.com/u/TinyAnts)
#### Post date: [September 3, 2021, 3:02pm UTC](https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845/3 "2021-09-03T15:02:19Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [September 6, 2021, 10:11pm UTC](https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845/4 "2021-09-06T22:11:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TinyAnts](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TinyAnts](https://forum.makecode.com/u/TinyAnts)
#### Post date: [September 14, 2021, 3:58pm UTC](https://forum.makecode.com/t/replicating-on-pin-touched-for-pins-8-12-14-15-16-why-does-this-work/9845/5 "2021-09-14T15:58:09Z")

</div>

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!
