capacitive sensor with no ground

I am trying to set up a capacitive sensor on an attiny. I have it working fine when the power is supplied from an arduino but when I cut it off and only do battery it goes completely haywire. I am assuming this is because I am cutting off a large ground. I just ordered a 7 segment display to see what the readings are like when its cut from ground since I can't do serial monitor with it unplugged :confused:

any suggestions on how to handle this would be great. I rather not have to connect it to a large ground (like Arduino Playground - HomePage suggests).

That's not making sense. battery power should not matter.

pwillard:
That's not making sense. battery power should not matter.

why would it not make sense? You are completely removing the ground from the system. attiny grounded through arduino through usb through computer through house...right? when you go battery the ground from house is removed, hence changing the values.

or am I missing something?

pwillard:
That's not making sense. battery power should not matter.

It does for capacitive sensors...when you switch to batteries you remove a huge chunk of metal from the system.

I too saw this same effect of the sensor not working when on battery verses on the "mains". I made some slight modifications to the CapacitiveSensor.cpp library file that seems to have fixed this issue. Essentially, I wanted to drive the circuit to an output set low as to connect it to ground. This helps to drain the capacitance on each iterative cycle so that each measurement cycle starts from zero. In addition, I also added a timer to the loop to help allow the discharge be complete before the next measurement.

int CapacitiveSensor::SenseOneCycle(void)
{
    noInterrupts();
	*sOut &= ~sBit;		   // set send pin to INPUT     (<<<<<<<<<<<< Change here <<<<<<<<<<<<)
	*sOut &= ~sBit;        // set Send Pin Register low

	*rReg &= ~rBit;        // set receivePin to input
	*rOut &= ~rBit;        // set receivePin Register low to make sure pullups are off

	*rReg |= rBit;         // set pin to OUTPUT - pin is now LOW AND OUTPUT
	*rReg &= ~rBit;        // set pin to INPUT

	*sOut |= sBit;         // set send Pin High
    interrupts();

	while ( !(*rIn & rBit)  && (total < CS_Timeout_Millis) ) {  // while receive pin is LOW AND total is positive value
		total++;
	}

	if (total > CS_Timeout_Millis) {
		return -2;         //  total variable over timeout
	}

	// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
    noInterrupts();
	*rOut  |= rBit;        // set receive pin HIGH - turns on pullup
	*rReg |= rBit;         // set pin to OUTPUT - pin is now HIGH AND OUTPUT
	*rReg &= ~rBit;        // set pin to INPUT
	*rOut  &= ~rBit;       // turn off pullup
        *sOut |= sBit;	 // set send pin to OUTPUT     (<<<<<<<<<< change here <<<<<<<<<<<<)
	*sOut &= ~sBit;        // set send Pin LOW
    interrupts();
long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
{
	total = 0;
	if (samples == 0) return 0;
	if (error < 0) return -1;                  // bad pin - this appears not to work

	for (uint8_t i = 0; i < samples; i++) {    // loop for samples parameter - simple lowpass filter
		if (SenseOneCycle() < 0)  return -2;   // variable over timeout
		delay(5);                              // (<<<<<<<<<<< Change Here <<<<<<<<<<<<<<<<<<<)
	}

	return total;
}

I will be testing this very soon. thank you for this reply :slight_smile:

Mrjohnk,

Would you be willing to post your modified CapacitiveSensor.cpp file? I don't have a c++ compiler.

Thanks!