Capacitive sensor unpredictable results - what's to blame?

Hi. Long time lurker on the forum as I’ve never really seen anything I’ve needed to help with. Unfortunately I’m needing some help of my own.

I’ve got two Arduinos setup to talk to one another using Xbees. Both Xbees are XBEE2 and both Arduinos are UNOs. I’ve got a capacitive sensor set up using 6 330ohm resistors soldered together with a long piece of wire wounded together set up on both Arduinos. Both sensors are showing up as fine in the Serial Monitor and showing predictive results. Each Arduino has an led in pin 9.

The Xbees are wired up as such:

XBEE GND > Arduino GND
XBEE VCC > Arduino 3.3v
XBEE DOUT > Arduino pin 2
XBEE DIN > Arduino Pin 3

The problem I’m having is when I power on both Arduinos (via USB ports on my laptop) they work predictably for a period of a time (seems different each time, sometimes 3/4 seconds, sometimes until I’ve used the sensor once). After that, the lights have moments where they flash very quickly once and then stay off for a bit, then again every so often. Nothing seems to prompt this.

This is the code that I am using:

#include <NewSoftSerial.h>

#include <CapacitiveSensor.h>

NewSoftSerial myport(2,3);
CapacitiveSensor   cs_6_4 = CapacitiveSensor(6,4);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int incomingByte = 0;
void setup()                    
  cs_6_4.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example

void loop()                    
  long start = millis();
  long total1 =  cs_6_4.capacitiveSensor(30);

if (total1 > 140){ 

if (myport.available()){
// set the values to the ‘incomingByte’ variable
incomingByte =;

if (incomingByte==1){
analogWrite(9, 255);
incomingByte = 0

analogWrite(9, 0);



This code is identical for both chips. The result of the random, quick, one of flashes leads me to think that somewhere the incomingByte value is picking up a 1 from somewhere which when I follow the Serial Monitor doesn’t seem to be the capacitive sensor. I’m trying to figure out how to stamp out these random flashes as I’m looking to modify the result when incomingByte == 1 so that it displays a 1 minute light show.

I’m also not sure what is at fault? I’ve been bashing my head against a brick wall, and while it’s made me a wee bit better looking, it’s also pretty sore and infuriating and I just need some help with this.

The problem is that the technique used for cap sensing in that library is inherently unstable. It relies on timing the discharge time of small capacitors with a very high impedance input. Consequently it is prone to all sorts of interference pickup which is what you are seeing. There is no easy fix. Maybe try and isolate it more from the electrical surrounds by running off batteries, or use another technique of cap sense with chips specially designed to do the job.

Thanks for the reply.

Ahh, that is a problem. Could coating the soldering in heatshrink improve it?

Yesterday, there was a period where the connection was really stable in one direction, but completely unresponsive in the other. And it appears that today, it's swapped that for the ability to talk back and forth. Unfortunately I don't have the previous code saved (as my laptop restarted itself) so I can't explain what I was doing differently, just that I know I had it working in one direction.

Could coating the soldering in heatshrink improve it?

I would not have thought so.