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


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); 
  // 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

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

  • What would happen if you connected the USB cable ground with a jumper wire to the 12v. power supply ground without plugging in the USB cable? Oct 9 at 6:51
  • As far as I can tell from Trinker M0 schematic, there should be no difference by powering via VBUS or VBAT. So here are a few of suggestions that you could try, 1) add a 10uF e-capacitor at the output of your DC-DC converter (i.e. between 5V and ground), 2) add another 0.1uF between VBat and GROUND near the VBat pin. 3) You should also add a protection diode (1N4148 or equivalent) between the fan by connecting the cathode of the diode to +12V and anode to pin 1 of Q1. 4) reduce the expose or the surface of the aluminium foil, as it might be too sensitive and acts like an antenna...
    – hcheung
    Oct 9 at 6:59
  • Have you tried putting another foil connected to ground near the sensing foil? Capacitive touch sensors work by building a capacitor between the input pin and ground whose capacity is changed by the finger. Bringing to devices ground near the foil might help
    – chrisl
    Oct 9 at 9:10

The problem is most likely your power supply coupled with the poor design of the capacitive sensing system.

The capacitive sensing is done by measuring the capacitance between your foil plate and "ground". That "ground" is usually a connection through the USB cable and your computer's power supply through the earth wire of your power cable to the physical ground, so you are then between the two "plates" of the capacitor. That ground connection is "clean". For a laptop the "ground" is usually just the case of the laptop or metal parts of the internals. This has no, or little, power supply noise.

However when you use a USB power supply you no longer have that "clean" connection. Instead your "ground" is going through the negative lead of the USB into the negative rail of the USB power supply which is then capacitively coupled to the neutral of the mains through a "class Y" capacitor across the transformer (there to remove inductive switching noise). That capacitor handles some quite high voltages and lots of noise, so you get that noise also reflected into your USB "ground", meaning the ground that you are using in your capsense "capacitor" is really quite noisy. Noisy enough to make your readings meaningless. On some laptops you may even notice a change when you plug the charger in compared to running on batteries for this same reason. (It's also why some early aluminium Apple laptops could give you an electric shock if you used them while plugged in...)

If you look at proper capacitive sensing arrangements you will see that the aren't just a bit of metal foil. They are properly arranged traces on a PCB which not only include the "sense" side but also the "ground" side of the capacitor. Your finger then affects the capacitance between these two points, not between a bit of foil and the floor of your room.

You should get a piece of cardboard and glue two pieces of foil side by side on it with a small gap between them. Then connect one of them to the Arduino's ground and the other to your input pin. Then recalibrate your readings to work with the new arrangement. You should find it far more stable and reliable.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.