Capacitive Sensor Library

Based on Capacitive Sensing Library for Arduino

This library had several issues and I will explain the changes I made here.

The first issue was related to the Register Data Type. XMEGA uses 16 bits while Arduino only 8. So I changed the following line in the header file:

#define REGISTER_DATA_TYPE uint8_t*

I change to something like “if it is XMEGA, make it 16 bits”. After that the program starts to compile, but it doesn’t work at all and I could only imagine the problem was in the bit mask. In the CapacitiveSensorDue function, there are several lines to make the bit mask and save it in the class. Basically, I “removed” everything from the Lithne code and start it again with a “#if defined(__AVR_XMEGA__)”. All the code I added is inside this if functions. I also created some definitions and other variables to make the code more clear.

After those changes, the library was still not working. I did some code analyzing and notice that the behavior of I/O registers is different in XMEGA. So, again, in the senseSample function, I “removed” everything and restarted. What the code is doing is setting the receiver pin LOW, turning off the pull resistors, setting the sender HIGH and then counting the time to the receiver becomes HIGH also. After that, it turns the receiver HIGH and the sender LOW and counting again the time to the transmitter becomes LOW. If you touch the wire/foil, the time to the receiver becomes HIGH is much bigger.

I tested the capacitive sensor with a resistor of 1M ohms and a capacitor of 100pF and it works pretty well, returning values between almost 0 and approximately 4000, but with 10M ohms resistor, it doesn’t work, because it was counting too much.