I've been playing with the CTSU unit on the new R4 boards. On the Minima in particular the Love Pin (the little heart shaped blob of solder on the back) is connected to one of the touch sensor pins. So I thought it would be cool to make a touch sensitive button out of it.
Once I had it working I put it together into a library. You can find the code here or it should be in the Library Manager tomorrow.
All you need for this is a 1uF ceramic capacitor. Other values may work, but might need some experimentation. 1uF is what I had when I set it up. Place this cap between pin 10 and Ground keeping the leads as short as you can so it's not sticking way up.
The example just uses the touch button to toggle the built-in LED on pin 13. If you touch the Love Pin then the led state will toggle on and off.
The class is pre-instantiated. All you do is call love.begin()
in setup once and then use love.read()
just like you would digitalRead a pin.
The example is pretty simple:
#include "LoveButton.h"
bool oldTouch = false;
uint8_t ledState = LOW;
void setup() {
pinMode(13, OUTPUT);
love.begin(); // Start the capacitive sensor
}
void loop() {
// read the touch sensor and store the result
bool touch = love.read();
if (touch && !oldTouch) {
// if there's a new touch.
// toggle the led state
ledState = 1 - ledState;
digitalWrite(13, ledState);
}
oldTouch = touch; // save the state for next time
delay(50); // for debounce a little
}
It should be noted that this library ONLY works with the UNO-R4 Minima. The Love Pin on the WiFi board is connected to a different pin that is not touch sensitive.
There's also a love.debug()
method that will return a char* with the raw numbers and the difference. The difference (labelled diff) is the number that I'm using to tell if there's a touch or not. Currently I have a default threshold of 23000, but there is a setThreshold method that you can use to change it if you find that you get different numbers.
There's a Debug.ino sketch in the examples that will just print out the raw numbers every half a second so you can watch and see what sort of threshold you need.
Let me know what you think...