544Views11Replies

Author Options:

Arduino capacitive touch sensor help Answered

so I have been messing around with the Arduino Capsense library with the immensely helpful guide; https://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/  (many thanks to DangerousTim!)   using the code provided, i managed to get a relay to operate with a sensor.  It worked great and I was able to adjust the distance and sensitivity.  But when i tried adding another sensor and relay, it stopped working.  I've been trying to isolate the problem for a while now and have gotten nowhere.  I'm hoping someone can help me, here's the original code for the single sensor and relay;

#include <CapacitiveSensor.h>

#include <CapacitiveSensor.h>                                          //change '42' to any desired pin...

long time = 0;
int state = HIGH;


boolean yes;

boolean previous = false;


int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2); 

// To add more sensors...
//CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup()                   
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);

   pinMode(13, OUTPUT);
 
}

void loop()                   
{
    long total1 =  cs_4_2.capacitiveSensor(30);
  
   
    if (total1 > 60){yes = true;}
    else {yes = false;}   
   
    // to toggle the state of state
   
    if(yes == true && previous  == false && millis() - time>debounce){
  
     
    if(state == LOW){
         state = HIGH; }
    else state = LOW;
    time = millis();
    }
      
      
      digitalWrite(13, state);
    
      previous = yes;
    

    
      delay(10);



}

and here's the code for the two sensors and relays

#include <CapacitiveSensor.h>


int led = 13;    
int led2 = 12;   

long time = 0;
int state = HIGH;
long time2 = 0;
int state2 = HIGH;

boolean yes;
boolean previous = false;
boolean yes2;
boolean previous2 = false;


int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired


CapacitiveSensor   cs_5_7 = CapacitiveSensor(4,6);       

void setup()                   
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
   cs_5_7.set_CS_AutocaL_Millis(0xFFFFFFFF);  //Calibrate the sensor...
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
}

void loop()                   
{
    int total1 =  cs_4_2.capacitiveSensor(30);
    Serial.println(total1);
    int total2 =  cs_5_7.capacitiveSensor(30);
     
     
   
    if (total1 > 60){yes = true;}
    else {yes = false;} 
  if (total2 > 60){yes2 = true;}
    else {yes2 = false;}  
   
    // to toggle the state of state
    if(yes == true && previous  == false && millis() - time>debounce){
     
     
       if(state == LOW){
         state = HIGH;
       }
       else
         state = LOW;
         time = millis(); 
    }
        
       
        if(yes2 == true && previous2  == false && millis() - time2>debounce){
         
        if(state2 == LOW){
         state2 = HIGH;
       }
       else
         state2 = LOW;
           time2 = millis();
      
    }
      
  
      digitalWrite(led, state);
      previous = yes;
      digitalWrite(led2, state2);
      previous2 = yes2;

    
     
      delay(10);


}

Any suggestions are always helpful.

11 Replies

user
Jonathanrjpereira (author)2016-04-16

I think you are making a mistake initializing **cs_5_7**.

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);

CapacitiveSensor cs_5_7 = CapacitiveSensor(4,6);

You are using same pin 4 for both Capacitive sensors. Try using different pins. I am quite sure that, this would help.

Select as Best AnswerUndo Best Answer

user
AtranE (author)2016-03-13

1:int total1 = cs_4_2.capacitiveSensor(30);

2:Serial.println(total1);
3:int total2 = cs_5_7.capacitiveSensor(30);

when these three lines together the capacitance of 4.2 i have to touch it several time to turn the LED on and sevral times to turn it off. by commenting line 3 with pins 5.7, 4.2 works fine. any idea why

Select as Best AnswerUndo Best Answer

user
CraigC26 (author)2015-12-28

@dudes, is there a way to make this function like a press button instead of a toggle ON/OFF? As in the LED will only be HIGH when total1 > 60 while still debouncing as 1 button press?

Select as Best AnswerUndo Best Answer

user
dudes (author)CraigC262015-12-30

Absolutely! Take a look at the regular debounce sketch, the one in the examples. Simply substitute the button for the capacitive touch sensor. (ie. instead of requiing the button ipn be pulled HIGH, require the total be > 60.) From there it should be pretty easy!

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)2015-01-06

I haven't worked with capsense but it may be that each sensor needs to be on its own send/receive pin. The additional sensors hooked up to pin 4 is confusing the arduino and may change the electronic properties of the circuit ganging them up. Try to upload a picture of your wiring setup. It would help see what you are doing.

Select as Best AnswerUndo Best Answer

user
dudes (author)caitlinsdad2015-01-06

oh, forgot that part. I have one sensor on pins 4+6 with 6 as the receive pin, the other sensor is on 5+7 with 7 as the receive pin. If you need any other info I'll be glad to (attempt to) provide it.

Select as Best AnswerUndo Best Answer

user
dudes (author)dudes2015-01-07

oops pin 6 is supposed to be pin 2, my mistake

Select as Best AnswerUndo Best Answer

user
CraigC26 (author)dudes2015-12-28

is there a way to make this function like a press button instead of a toggle ON/OFF? As in the LED will only be HIGH when total1 > 60 while still debouncing as 1 button press?

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)dudes2015-01-06

so CapacitiveSensor cs_5_7 = CapacitiveSensor(4,6);

should be CapacitiveSensor cs_5_7 = CapacitiveSensor(5,7); when declaring your second sensor?

Select as Best AnswerUndo Best Answer

user
dudes (author)caitlinsdad2015-01-07

Thanks! I kinda wondered if it would be something simple like that. I don't have access to my Arduino right now, but when I do, I'll try that out and get back to you.

Select as Best AnswerUndo Best Answer

user
dudes (author)dudes2015-01-07

Well, I uploaded it and it works perfectly! Thank you for the help!

Select as Best AnswerUndo Best Answer