Note that to get to this point, I used much of the helpful information from the Post How to use capactive touch on Adafruit board in Arduino mode?
I am using a Trinket M0 with the Arduino IDE with the Adafruit_FreeTouch.h library. Also used the example code found here
https://github.com/adafruit/Adafruit_FreeTouch/blob/master/examples/freetouch/freetouch.ino
I modified the code to use the A0 input as a touch sensor with a wire and aluminum foil. Using a FET on the digital output A2 to turn a fan on or off when the foil is touched. I am using a small 12VDC wall transformer (1.25A) and then a DC-DC converter to output 5vdc for the Trinket on the BAT in. The 12Vdc also supplies power to the small computer fan ( uses about 200ma)
The circuit and code work great when the USB cable from my laptop is connected to the Trinket. BUT, when I disconnect the USB cable from the Trinket and touch the sensor, everything goes unstable. Fan and built-in led cycle on/off randomly at 0.5sec to 2sec intervals.
I've tried all different parameters for the library including the series resistor, sampling rate and frequency modes. I've checked grounds multiple times and this one is really stumping me. I suspected fan noise early on so the most interesting experiment I've tried so far IMHO is to remove the 12vdc to the fan but keep it supplying the DC-DC converter for the 5v to the trinket-But, the onboard led has the same instability as when fan is connected. Lastly, I used an Arduino Uno to develop the code and the circuit earlier and it worked fine without USB cable connected. I feel so close. Any suggestions or insights much appreciated.
Here is my code
/ touch - Capacitive touch demo using FreeTouch. Trinket M0 version.
// Note: the pin silkscreened 1 is Analog Pin 0.
#include "Adafruit_FreeTouch.h"
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_0, FREQ_MODE_NONE);
int led = 13; // blink 'digital' pin 1 - AKA the built in red LED
int FAN = 2; //
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(FAN, OUTPUT);
digitalWrite(led, LOW);
digitalWrite(FAN, LOW);
delay(50);
// Initialize A0 as a touch sensor
if (! qt_1.begin())
//Serial.println("Failed to begin qt on pin A0");
digitalWrite(led, HIGH); // Turn on the led
}
void loop()
{
int counter;
int result = 0;
counter = millis();
result = qt_1.measure();
if (result > 950) {
digitalWrite(FAN, HIGH); // Turn on the FAN
digitalWrite(led, HIGH); // Turn on the led
}
else {
digitalWrite(FAN, LOW); // Turn off the FAN
digitalWrite(led, LOW); // Turn off the led
}
delay(50);
}
Here is schematic
Not sure if I'm doing this right by posting an answer?
I didn’t expect this to be as complex! Unfortunately adding the suggestion to add the capacitors did not change anything. But the answer makes sense to me because when I think of why it worked so well on my Arduino Uno development circuit, it could have been because I was using the Capacitive Sense library which utilized two pins that to my understanding measured a RC type of decay to achieve the touch sensing.
Too bad that the Trinket doesn’t operate the same way. And my programming skills are not strong enough to come up with a similar design solution as the Arduino Uno RC sensing. So, I will try the idea in the answer where I implement the “side by side with a gap” touch sensor just for learnings sake. I’ve seen these touch sensors often as traces on a pcb.
But I should share what my ultimate design goal is. That is to make a touch sensitive art lamp that has a steel body. The lamps are diodes and diodes strips which will be brightness controlled with a PWM input. So, there is no need for an AC connection other than for the wall transformer which supplies DC. The design needs to sense a touch whenever someone touches anywhere on the lamp body so that the SW can simulate a three-way switch. So, a “side by side” gap doesn’t work in this application. I was hopeful that the Trinket would work since I had everything working in my Arduino proto. And I’m really looking for a “small” trinket type board solution to fit in the lamp. In the past I have been successful achieving this functionality with AC power lamps by using those replacement touch circuit modules for 3-way AC switched bulbs. But for this I wanted to use LED energy saving light sources with a microcontroller.
I think I will try to research “dedicated or discrete” touch sensing circuits that can give me digital input to the Trinket when a touch is sensed on the lamp body. Any other suggestions/comments are greatly appreciated. Cheers