micropython icon indicating copy to clipboard operation
micropython copied to clipboard

Button pins and read_digital

Open rhubarbdog opened this issue 5 years ago • 5 comments

I was researching something regarding microbit pins. I found an up coming feature that button pins (5 & 11) were reusable as read_digital pins. It may be the case, i didn't read all the source. Once pinX.set_pull or pinX.read_digital has run on pin 5/11 then it should now throw an exception if button_a or button_b methods such as .is_pressed() is used.

rhubarbdog avatar Dec 04 '18 03:12 rhubarbdog

Hi @rhubarbdog,

I want to make sure I understood this part correctly:

Once pinX.set_pull or pinX.read_digital has run on pin 5/11 then it should now throw an exception if button_a or button_b methods such as .is_pressed() is used.

Do you mean that once the pull up/down method is used on the button pins, then using the button_a/b methods should raise exceptions?

microbit-carlos avatar Dec 14 '18 13:12 microbit-carlos

Yes. Otherwise users will think they can connect external components to pin5/ pin11 and use buttons. If the program is in # read component pressing the button will cause glitches and accessing button_a.get_presses() after a component has buzzed pin5 will yeild an unexpected result.

rhubarbdog avatar Dec 14 '18 15:12 rhubarbdog

A user may want to use both the button and input pin (5/11) on the edge connector at the same time, so I think the code should allow such flexibility. Using the button pins as digital inputs isan advanced feature, so users should in such a case be aware that advanced usage can lead to unexpected behaviour (which may actually be expected if the user knows what they are doing and wants it to behave this way).

If anything, this would just need a note in the docs, rather than an addition to the code to restrict usage.

dpgeorge avatar Dec 15 '18 01:12 dpgeorge

The docs mention the pull up resistors in https://microbit-micropython.readthedocs.io/en/latest/pin.html , what can of note can we add to the docs to make the digital read behaviour more clear?

microbit-carlos avatar Jan 02 '19 17:01 microbit-carlos

Add the text. This implies that pins 5 and 11 are only usable as read_digital pins. Testing for if pin5.read_digital() == 0:

rhubarbdog avatar Feb 04 '19 11:02 rhubarbdog