Go Down

Topic: ~fF Capacitance measurement with mTouch 16f707 touch button! (Read 3900 times) previous topic - next topic


Recently I started to work with mTouch CSM module. It sounded like extremely interesting and high performance hardware to work with. It sounded right! :) there are countless applications for use of this hardware. Touch pad, proximity sensors, readout of capacitive sensors,extreme precision capacitance measurement, ultra small capacitance change detection,  soil moisture, skin moisture measurement are just some of them.. But then I started to search for examples and I was surprised because there wasn't any!!!! At least not ones that was helpfull.. That is why I made this complete guide for capacitive sensing using excellent Microchip CSM module inside some of them microcontrollers.

The CSM simplifies the amount of hardware and software setup needed for capacitive sensing applications. Only the sensing pads on the Printed Circuit Board (PCB) need to be added. The capacitive sensing modules allow for an interaction with an end user without a mechanical interface.

Here is block schematic of SCM module

The capacitive sensing oscillator consists of a constant current source and a constant current sink, to produce a triangle waveform. On picture below you can see typical waveform recorded ad one of the touch pads while measured with Microchip CSM module.

To explain it simply upper current source feeds current into analog pin capacitance until voltage reaches Ref+. Then upper current source shuts down and lower current source engages. He sinks current from analog pin capacitance until voltage reaches Ref- and it all goes in circle. This is why we call it oscillator. Since it is constant current voltage is changing linearly which we can see on picture above.

The oscillator is designed to drive a capacitive load (single PCB pad) and at the same time, be a clock source to one of the timers. Cap Sense Oscillator A can be input of Timer1 and TimerA depending on configuration we make. I personally prefer Timer1 since it is 16 bit timer and it is easier to count bigger number of oscillations with it.

It has three different current settings as defined by appropriate registers. The different current settings for the oscillator serve two purposes:

• Maximize the number of counts in a timer for a fixed time base.

• Maximize the count differential in the timer during a change in frequency.

Current settings are divided into two groups. Low range that use internal reference voltages. This means you cannot change upper and lower voltage threshold in CSM oscillator. For touch screen purposes and when you do not need to know amount of capacitance you are reading this is ok. High range If you want to use this module as extra precise small capacitance meter that can measure capacitance order or ~fF (that is FEMTOFARAD!! ~10 E-15) then it is easier to do calculations when you can set precise voltage thresholds. Current source range group is selected by setting CPSxRM configuration bit

First group of Current source currents is called

Low range and values available are

- 0.1 µA 1.2 µA and 18 µA

High range currents are:

- 9 µA 30 µA and 100 µA

Selection between the voltage references is controlled by the CPSxRM bit of the CPSxCON0 register

When using high range currents beside usual setup you must enable and properly configure FVR (Fixed voltage reference) and integrated peripheral DAC Digital-to-analog convertor that is also placed inside this chip. As you can see it is easier to use low range currents but I prefer to use high current range because it gives you better control and readout stability when measuring capacity. The capacitive sensing oscillator will continue to run as long as the module is enabled, independent of the part being in Sleep. In order for the software to determine if a frequency change has occurred, the part must be awake. However, the part does not have to be awake when the timer resource is acquiring counts. When you configure all settings and you have your CSM module running you still need to measure CSM oscillator frequency. To measure the frequency of the capacitive sensing oscillator, a fixed time base is required. Any timer resource or software loop can be used to establish the fixed time base. It is up to the end user to determine the method in which the fixed time base is generated.

The fixed time base cannot be generated by the timer resource that the capacitive sensing oscillator is clocking. For our example Timer1 is dedicated to CSM module and therefore cannot be used for fixed time base measurement.

Let us make checklist of things you need to do to measure capacitance of CPSA module pin. In this example we will use Timer1 for counting CPSA module CSM oscillator.

For measuring its frequency we will use fixed time base created by Timer3. We will make check list for using high range current with Ref- voltage from DAC module which is more complex.

1. Turn Module on with CPSAON configuration bit

2. Choose current range used with CPSARM configuration bit

3. Choose current value with CPSARNG0 and CPSARNG1 bits

4. Select channel (microcontroller pin) that is being measured with CPSACON1 configuration register

5. Dedicate Timer1 to CSM module for counting oscillations by setting TMRCS0 and TMRCS1 bits

6. Configure Timer1 as normal timer operation. Only difference is that now its clock comes from CSM module instead of system clock. Set its Interrupt, prescaler, Disable External Clock Input Synchronization and Gate operation, Turn it on...

7. Enable FVR module and set its gain amplifier settings for the reference voltage of DAC.

8. Enable DAC, Set its reference voltage to FVR and make its output value. Notice this value will be used as Ref- Voltage in CSM oscillator. In this case positive voltage reference in this oscillator REF+ will be FVR value.

9. Enable some of remaining timers to measure fixed time period in which you will read how many oscillations from CSM oscillator Timer1 has counted. For example enable Timer3 to work and enable its interrupt.

10. You are ready to go, just on every interrupt of Timer3 get value of Timer1 (dedicated to CSM module) and reset it back to zero.

If you are wondering how to make hardware implementation of Touch button to read capacity from it, it is very simple. Since this module is very sensitive any shape and size will do. Best is to make it size of top of human finger (2cmx2cm). You experiment and you will see what get you best results.

Here is picture of one such pad connects via thin trace directly to microcontroller pin.

Now you have enough understanding of Microchip mTouch CSM module to start working with it.

I also made project and tested CSM function in practise. My ruff calculations are that when i aproach with finger to capacitive pad on pcb i change capacity for about 25fF!! and it is not problem to detect with this module. :)  Readout stability and amount of controll you have over its operation is just superb :)

P.S. no i am not working for Microchip :) i just like it how this module works

havent worked with other capacitive sensing moduled but this one is grat because it is integrated in microcontroller it self..

It would be too long to explain all in details here but if you are interested to work with it or you have questions P.M. me or see this full guide with more details at


and for you to start working with mTouch i privided full C source code and project for download with full detailed explanations and video presentation of its work at


please comment because i was thrilled when i saw possibilities of this Microchip mTouch CSM module.


Sorry to say this, but I don't have any microchip controllers at home, and am missing a programmer.
Right now I read 328P and atmega32u4 manual, at least some parts of it intensely.
As you cas see in their application note, they have qTouch dedicated hardware touch devices, microcontrollers with it, and an application note explaining their principle of measurement.

Now I use cypress programmable system on chip, pSoC4, 4EUR with programming usb chip, having touch as well. But I did not cope in reading all the manuals, and cypress is not so well known among hobbyists.

I am very much interested in capacitance measurement that has the quality of reading out at least pF with some accuracy, and what I did with the arduino capacitiveTouch library was not very immune to noise. I will try with a 1nF capacitance in series to try to get rid of AC noise and put adjacent pcb traces to ground by "guardian" low output signal.

But I have to read a theory of measurement book, lent it from local university library, to get a hang what I want to measure: pure capacitance, do I have any unknown (parasitic) resistance or inductance?
I'm missing the classical bridge approach, it is not there because uncool, because you don't need it to do "touch", but as you have very impressively shown, there is more than touch.
We want to sink two isolated wires (perhaps u-turned to avoid ends in water, straightend to parallel by some weight at the end) in to a cistern to know the height of the water mirror and thus the Liter still in it. Next we want to measure soil moisture to modify some gardena microdrip watering on balcony.
And third project will be the repair of Kaercher NT361 vacuum electronics, they used electrodes reaching in the dust bin volume and, when in water, measured the current across the gate of some super sensitive thyristor. But the device was not immune to currents in the water, that my gardena dirt pump (with similar measurement of water height, but with higher currents) induced in the water.
So hopefully sooner than later I will report back what principle I used.

At the end of the month it would be nice if I coped to use either the 12bit DAC of Due or some Analog Devices (isolated) ADC's I got as samples to have some small oscilloscope with mathworks (http://www.wolfram.com/raspberry-pi/) on raspberry pi as data processing and analysis / logging instrument. Hopefully I find some more doing arduino and raspi together ;)

R/L/C - bridge measurement will be very welcomed, especially if it is to be calibrated.
For R the solution is 4 point measurement for low R's, and perhaps I cope in doing this on a scope base. If you have 4 or 8 channels for this it makes a good wire tester, bend and twist while measuring.

It gets tricky for higher frequencies, perhaps some consumer RF device could be re-used as measurement of damping or reflection.

Is there a sketch to program microchip devices with an arduino? Heretic question, I know ;)



Go Up