Capacitive sensor problem

I have problem with capacitive sensor, when I power up Arduino the capacitive sensor doesn’t start to work unless I disconnect and connect again the sensor pin from Arduino board. After this it works fine. Im using battery power, with usb connected it works fine.

Is there a way to reset it with some code?
Connections are made like this:
http://playground.arduino.cc/uploads/Main/CapSense.gif I used 1 M ohm resistor.
This is main part of the sensor code in my project:

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor = CapacitiveSensor(9,8);

void loop() {
// store the value reported by the sensor in a variable
long sensorValue = capSensor.capacitiveSensor(30);

//if the value is greater than the threshold
if(sensorValue > threshold) {
// turn the LED on
digitalWrite(LedPinCap, HIGH);
}
// if it’s lower than the threshold
else {
// turn the LED off
digitalWrite(LedPinCap, LOW);
}
}

Hi anttif

Im using battery power, with usb connected it works fine.

What type of battery are you using? What voltage? How is it connected to the Arduino?

Regards

Ray

Hi Ray,

Im using 12V lead acid battery powering relays, Syren 10A motor driver and Arduino via DC jack. Also tried while battery is charging but no effect.

Regards Antti

I suspect that the Arduino should be connected to ground (earth), somehow. Does it work when you touch the Arduino GND while approaching/pressing the sensor?

It didn't work when I touched Arduino ground.

Anyway problem is solved, thanks for the answers. I got it to work by taking off other functions from twisted pair cable so I could lower the threshold value.