Hello guys, I just wanted to share this little piece of code I threw togehter, it will read a value from a capacitive sensor. The best part is: it only uses one (analog) pin.
to use the code as it is shown here, connect A0 to ground via a short piece of wire and connect a piece of wire to A1 which is used to detect a nerby conductive object. If you connect a large conductive surface to the sense wire, it will be more sensitive.
It works in the following steps:
- create input, set it to pullup to charge the pin.
- discharge the internal ADC capacitor by reading an analog input that's connected to ground via a wire
- dispose the input, set it to tristate (=no pullup/down resistor) beforehand
- create analog input, read it, dispose it
this pattern is repeated more than 100 times and the average is calculated to give more accurate results.
the code looks like that
public static int read() { AnalogInput zero = new AnalogInput(AnalogChannels.ANALOG_PIN_A0); int ret = 0; for(int y = 0; y < 400; y++) { InputPort charge = new InputPort(Pins.GPIO_PIN_A1, false, Port.ResistorMode.PullUp); Thread.Sleep(1); zero.Read(); charge.Resistor = Port.ResistorMode.Disabled; charge.Dispose(); AnalogInput input = new AnalogInput(AnalogChannels.ANALOG_PIN_A1); ret += input.ReadRaw(); input.Dispose(); } zero.Dispose(); return ret / 400; }
in order to use it for a different input, you need to change both the charge and the input pin.
It works by distributing the charge between the (charged) capacitor formed by the input pin and the (discharged) capacitor inside the ADC, so it makes use of the internal structure of the chip.
with no wire connected it gives me a perfectly constant value of 1699, it goes up by two when I put my finger on the header.
with a large metal plate connected, just putting my hand under the desk (~3cm thick) will result in a change of over 200. Touching it with just a bit of insulation in between results in an even bigger change.
This particular way to abuse an ADC works better on an Arduino, but on the Netduino, it's the only way I have found to work at all, since it is too slow for the normal resistor + time measuring way of doing it. I am using a Netduino Plus 2, with the older models that have less bits on the ADC it will be less accurate.
anyways, I hope you found it useful for something,
Greetings.