Stellaris LM4F120 LaunchPad Capsence calibration

From Texas Instruments Wiki
Jump to: navigation, search

LaunchPad Resource Portal  LaunchPadRocket.png

This wiki is open and can be edited by all!

More information available @

LaunchPad Banner.jpg

Calibration of capacitive sensors

Note: Early releases of the capsense software for Stellaris LaunchPad suggest using 30ohm Resistors. This is incorrect. The software provided is pre-calibrated for 200k resistors.

This tutorial was written to help demonstrate how the "capsense" software example on the Stellaris LaunchPad can be recalibrated for better sensing accuracy. This tutorial assumes you are familiar with the capsense library

Calibration Process

Alter the cap touch program

Alter the cap touch program (or write a new one) that allows you to see the raw capacitive sensor measurements. The following snippet of code shows one way to accomplish this.

    UARTprintf("Sensor Calibration: \n");

        // Take raw capacitance measurements from each of the elements on the
        // capsense booster-pack.
        TI_CAPT_Raw(&g_sSensorWheel, ulWheelCapValues);
        TI_CAPT_Raw(&g_sMiddleButton, ulMiddleValue);

        // Print the measurements to the UART in the following order:
        // Up, Right, Down, Left, Center
        for(ulIndex = 0; ulIndex < 4; ulIndex++)
            UARTprintf("%4d ", ulWheelCapValues[ulIndex]);

        UARTprintf("%4d ", ulMiddleValue[0]);
        // Write over the previous values in the next iteration.

Re-compile, re-program

Re-compile, re-program and take a look at the sensor data coming back over the UART. Your output should look something like this. (Note: By default, the capsense app communicates at 9600 baud instead of 115,200 baud):

9976 10056 10154 9993 10082

The exact numbers will vary with your choice of resistor values as well as the number of oscillations used for each measurement of one sensor. These values were taken with 1 Mohm resistors, and an oscillation number of 10.

Record Calibration Data

Touch one of the directional right on the little white arrow. Compare what all four numbers look like before the press, and during the press. For instance, if you press "Up" you might get results like this.

9976 10056 10154  9993 10082
During press:
10376 10136 10154 10063 10082
 800    80     0    70     0

Repeat for each button, recording the data as you go. The difference value for the button you are pressing directly represents the "maximum response" value for that element. The difference values for the adjacent buttons effectively represents the lowest "interesting" response value for those elements.

Modify CTS_structure.c

Enter appropriate values into the ek-lm4f120xl/drivers/CTS_structure.c file. This file is where information about each element on the board is kept. For each element tested, replace the "ulMaxResponse" field with your measured maximum response value. Also replace the "ulThreshold" field of each element with a number slightly lower than the lowest "minimum response" observed for that element. For example, the updated structure for the "up" button in this example might look something like this:

// Volume up button on PE2
const tCapTouchElement g_sVolumeUpElement =

Re-compile and reprogram

Removing the calibration code then re-compile and re-program to see the results of your calibration. If all went well, you should see improved accuracy and performance.

Notes on Resistor and Oscillation Number Selection

The LM4F120 has no internal pin-oscillator hardware, so it requires the use of external resistors to take measurements from the capacitive sensors on the CAPSENSE booster-pack. The resistors must be chosen such that the charge and discharge times of the capacitive sensors on the booster-pack are reasonably slow compared to the chip's system clock. Also, they should be chosen such that multiple charges and discharges can be measured for each sensor (improves noise tolerance) without taking up too much time (hurts response time). The Stellaris team recommends using resistors of at least 200 kohm, and less than 5 Mohm.

The capacitive sensing method used by the Stellaris Launchpad essentially involves charging and discharging a capacitor a set number of times, and measuring the amount of time (in system clocks cycles) required to complete these "oscillations". The more oscillations performed, the more noise-tolerant the overall system is, however, an excessive number of samples could hurt the system's response time. Finding a balance between these two system-level effects will improve the performance of the booster-pack. If you are using one of the recommended resistor values, the Stellaris team recommends that you sample each element at least 10 times for basic noise tolerance, but sample numbers into the hundreds are possible without large performance losses. The number of oscillations is controlled on a sensor-by-sensor (as opposed to element-by-element) in the ek-lm4f120xl/drivers/CTS_structure.c file as "ulNumSamples".

Common problems

Raw capacitance values change rapidly, making sensors difficult or impossible to calibrate

Some noise is expected in capacitive sensor measurements, as long as your calibration tests allow your threshold values to be above your noise floor, you shouldn't notice much (if any) performance problems with the application itself. If the noise is of greater magnitude than this though, try increasing the number of samples taken in the problem sensor. This effectively averages the capacitance measurements over time, which should help with random noise problems.

One or more of my elements is giving a "wildly" inconsistent Raw Capacitance value

If you have one element that is giving raw capacitance values that are orders of magnitude different from the other sensors, or seems to alternate between "hypersensitive" and "non-responsive" you may have connectivity issues between the LaunchPad and the booster-pack. Try re-seating the booster pack to see if the problems go away.

Beware, the most firmly attached booster-pack is not always the best connected electrically. If your launchpad has the standard male headers on top that it was originally shipped with, you may notice small cones of solder at the base of each lead. Internal testing be the Stellaris team has shown that if the plastic on the cap-sense booster pack comes in contact with these cones, the electrical characteristics of the connection can change significantly. This sometimes resulted in apparent disconnects, or unusual fluctuations in measured capacitance. Pulling the booster pack away from these solder cones be even a millimeter solved these problems instantly.