LED capacitive touch sensor

I am following a simple capacitive touch sensor using a metallic object project by Amal Matthew.

https://create.arduino.cc/projecthub/amalmathewtech/touch-controlled-light-using-arduino-d2f878?ref=tag&ref_id=capacitive&offset=1

The hardware was followed and the code was copied and pasted. But instead of changing state with touch, the LED was blinking instead whether I touch it or not.

My goal is to have it work like how it worked in the demo from the tutorials, turn the led on and off with a touch. Please help. I don't even know what's causing the problem.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4); // 1M resistor between pins 2 & 4, pin 4 is sensor pin, add a wire and or foil

int in = 2; 
int out = 4;  
int state = HIGH;  
int r;           
int p = LOW;    
long time = 0;       
long debounce = 200;
void setup()
{
  pinMode(4, INPUT);
  pinMode(8, OUTPUT);
}
void loop()                    
{
 
  r = digitalRead(4);
  if (r == HIGH && p == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else 
      state = HIGH;
    time = millis();    
  }
  digitalWrite(8, state);
  p = r;
}

Edit: Sorry for the trouble and thanks for being patient with me. ^^

Hello
Post the current sketch.

From the tutorial:

connect the coin(coin act as capacitive sensor) to the receive pin.

That's not the full truth. The coin and its connecting wire also act like an antenna, catching whatever EM noise from the ambient. Reliable touch sensors use 2 plates and a twisted pair or shielded wire for noise reduction.

At what frequency? Do you have an electronic device nearby that can emit pulses at that frequency? Move the coin around in your room, perhaps you can find out the trouble maker.

Hi, @yourstrulii To add code please click this link;

Tom... :smiley: :+1: :coffee: :australia:

I am using a metallic plate, I wonder if that's causing the problem after all?

At first run, it was blinking neither fast nor slow. As for an electronic device, will using a laptop cause a problem? I did move around the set-up but there was no noticeable change.
Second run, which is after I disconnected the arduino board for some time and then reconnected it once again, the LED wasn't blinking. It's just emitting light without turning off, disregarding the touch sensor.

A single-wire touch sensor is unreliable. You notice that already and I have no more remedy :frowning:

i did try to remove these lines, basically removing time and debounce from the code and those lines related to it, millis and time and i did notice that it's being affected. (ps. i also removed the plate leaving the jumper wire alone as the sensor)

while removed, I also reversed LOW-HIGH lines
for example:

to int state = LOW;
i did it for every LOW-HIGH pieces of code

the result was more noticeable. by default, the LED was emitting dim light, and touching the tip of the jumper wire would make the led emit brighter.

The open wire acts as an antenna and you see the ambient EM noise. A scope on the LED may show 50 or 60Hz, depending on your mains frequency.

1 Like

is there a cost-effective substitute to ensure a touch(only)-sensitive sensor? or make it more reliable? the tutorial seems reliable with just a coin and a clip wire.

Yes, see my #3. Add a second plate/coin and connect it to GND. Use a twisted pair or shielded cable to connect both to the Arduino.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.