Search code examples
touchmicropythonbbc-microbit

How to set Pin touch mode to capacitive on micro:bit v2 in MicroPython


If I use the makecode editor to create the code, it generates:

pins.touch_set_mode(TouchTarget.P0, TouchTargetMode.CAPACITIVE)

However, if I try and run this, it can't find 'pins'.

Note: I then investigated further and found out how to do this before submitting my question...


Solution

  • Capacitive mode can be set for each pin, e.g.

    from microbit import *
    ...
    pin0.set_touch_mode(pin0.CAPACITIVE)
    ...
    if pin0.is_touched():
        ...
    

    The last line checks whether the pin is touched - usually in a loop.

    Hope this saves other people some time...