It mostly works superbly. But ... if the touch is too direct, like a person places a hand firmly on the sensor, the sensed "proximity" suddenly drops from 100% to 0%. This renders the whole thing rather useless.
My suspicion is that a touch that is "too strong" overloads the PWM and basically shorts it out, cutting off its ability to sense the capacitance around the sensor. But that's just a guess.
Here is the code I'm using:
Code: Select all
"""CircuitPython Essentials Capacitive Touch example"""
import time
import board
import digitalio
import touchio
from analogio import AnalogOut
touch_pad = board.A1 # Will not work for Circuit Playground Express!
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
led_cycle = 0;
touch = touchio.TouchIn(touch_pad)
analog_out = AnalogOut(board.A0)
analog_factor = 60
offset = 0.0;
for i in range(100):
offset += touch.raw_value
offset /= 100.0
offset = int(offset + 0.5)
print("Initial offset: ", offset)
filtered_value = offset;
n = 0;
while True:
filtered_value = touch.raw_value; # /100.0 + filtered_value*0.99
output = (int(filtered_value) - offset) * analog_factor
if output < 0:
output = 0
elif output > 65535:
output = 65535
analog_out.value = output;
led_percent = int(output * 100.0 / 65536.0)
if led_cycle < led_percent:
led.value = True
else:
led.value = False
led_cycle += 1;
if led_cycle > 99:
led_cycle = 0
n += 1
On a related note, I've discovered that if I touch two of the capacitive sensors at once, one with my left hand and one with my right hand (both powered from the same source), they both drop out, showing zero proximity. I can only guess that the PWM signals are passed through my body and the two Gemmas are confusing each other.
Any suggestions?
Thanks!