Confused about Touch (microbit)

Issue #443 resolved
Wenjie Wu created an issue

What is touchPin variable? I didn't see its definition.

Comments (9)

  1. John Maloney repo owner

    You found a bug! That should be "pin" rather than touchPin. (I didn't notice because that code is not essential to proper operation. It's goal is to start draining the charge on the touch pin so it is ready for the next cycle. But the pin is also discharged at the start of the cycle.)

    The micro:bit has weak pullup resistors (10M ohms, I think) that gradually raise the voltage on the pin during the timing loop. The amount of time it takes to charge the pin is determined by the capacitance -- higher capacitance takes more time. The pin has higher capacitance when it is touched.

    To use this on pins that do not have weak pullup resistors you should add an external pullup resistor (5M to 20M ohms is probably good). Alteratively, you could try enabling the built-in pullup resistor. Internal pullup resistors tend to be around 10k to 20k, so they would charge the pin very quickly, perhaps too quickly to detect a chance in capacitance. But with a fast processor (e.g. ESP32) that might work.

  2. Wenjie Wu reporter

    thank you for the explanation.

    I found the esp32 touchpin to be very easy to use and this seems to work well.

  3. John Maloney repo owner

    Glad you found that primitive! It's specific to the ESP32, which is why it is hidden away in a system primitives library. Perhaps we should create a "Touch (ESP32)" library based on that primitive and add it to the Sensing folder?

  4. Wenjie Wu reporter

    I demonstrated this example in Saturday's online sharing session, and people were very interested. After the session, many people tried making different buttons using fruits. Each fruit has its own unique capacitive properties, so when a pin is inserted into different fruits, the triggering threshold of the switch varies. This is an interesting feature in itself, as it means that the capacitive pin can even be used to differentiate objects! The graph feature in MicroBlocks is extremely suitable for observing these thresholds, which is a huge advantage of its liveness. What would the blocks look like if we create a 'Touch (ESP32)' library?

  5. John Maloney repo owner

    That sounds like a fun activity!

    A basic 'Touch (ESP32)' library might include both the "capacitive sensor" block and perhaps a "set touch threshold _" and "is pin _ touched?" blocks. The pin parameter might have a dropdown menu with just the ESP32 pins that support touch sensing.

  6. Log in to comment