As mentioned before in this article there are many methods for capacity readout by microcontroller. Microchip offered us mTouch CSM module inside some of its products. In this article we will make detailed explanation of CSM module operation that will help you understand Capacitive touch CSM sensing example article.
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, capacitance measurement, 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!! That is why I made this complete guide for capacitive sensing using excellent Microchip CSM module inside some of them microcontrollers. When you finish reading this article make sure you get complete 100% working code to start your hardware and get more detailed explanation about programming details.
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. In a typical application, the capacitive sensing module is attached to a pad on a PCB, which is electrically isolated from the end user. When the end user places their finger over the PCB pad, a capacitive load is added, causing a frequency shift in the capacitive sensing module.
The capacitive sensing module requires software and at least one timer resource to determine the change in frequency. Key features of this module include:
• Analogue MUX for monitoring multiple inputs
• Capacitive sensing oscillator
• Multiple Power modes
• High power range with variable voltage references
• Multiple timer resources
• Software control
• Operation during sleep
• Acquire two samples simultaneously (when using both CSM modules)
Two identical capacitive sensing modules are implemented on the PIC16F707/PIC16LF707. The modules are named CPSA and CPSB (CPS – Capacitive Sensing). A block diagram of the capacitive sensing module is shown in Figure below.
Both of these modules CPSA and CPSB are identical so we will continue explaining its operation on CPSA.
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. Best starting point and next step is to read article featuring Microchip mTouch capacitive touch pad readout with PIC16F707 microcontroller by using integrated CSM capacitive sensing module and start using it on your hardware. In PIC16F707 mTouch capacitive touch pad readout example complete source code and detailed explanation of configuration is provided. See it as practical implementation of knowledge you have just gained by reading this article.