Raspberry Pico Touch Sensor

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
cebersp
Posts: 29
Joined: Mon Feb 08, 2021 12:07 pm

Raspberry Pico Touch Sensor

Post by cebersp » Tue Feb 16, 2021 8:56 am

Hi, this is a a piece of metal connected to Pico as a a capacitive touch sensor. It turned out, that this can just (!) be done just with MicroPython. It is inspired by the Arduino touch sensor library.
Two GPIOs are used. One (16) is output and coupled via a 1 Megaohms resistor to the metal pad. The second (17) is coupled directly to the metal pad. The onboard LED is turned on and off. The time needed to charge the capacity of the pad is measured. With increased capacity a longer time is needed.

To make it more selective, switching needs two consecutive same states.

Have fun!
Christof

Code: Select all

# touchA.py by CWE

from machine import *
from utime import *

led= Pin(25, Pin.OUT)
led.value(0)

send= Pin(16, Pin.OUT) # via 1meg resistor connected to touch pad
send.value(0)

t1= Pin(17,Pin.IN) # directly connected to touch pad
sleep(1)

trigLevel= 0

def getT1(): # Sensor routine gives back a number
    start=0
    end=0
    start= ticks_us()
    send.value(1)
    while t1.value()<1:
        pass
    end= ticks_us()
    send.value(0)
    return(end-start)

def calibT1():
    global trigLevel
    for i in range(0,50):
        trigLevel= max(getT1()*1.3 , trigLevel) # factor 1.3 perhaps needs tuning
        print(".",end="")
        sleep(0.05)

print("Calibrating....")
calibT1()
print("TrigLevel: ", trigLevel)

actT1= False

while True:
    lastT1= actT1
    t1Val= getT1()
    
    actT1= t1Val > trigLevel
    if actT1 and lastT1: # switch only, if two consecutive same levels detected
        led.value(1)
    elif actT1==False and lastT1==False:
        led.value(0)
    print(trigLevel,t1Val) # use with plotter of thonny
    sleep(0.05)
    


cebersp
Posts: 29
Joined: Mon Feb 08, 2021 12:07 pm

Re: Raspberry Pico Touch Sensor

Post by cebersp » Tue Feb 16, 2021 7:23 pm

a version with PIO can be found here:
viewtopic.php?f=21&t=9833&p=55035#p55035

Post Reply