# Adaptive Kalman filtering for better buttons-slider capacitive sensing

Capacitive touch interfaces have become increasingly popular for button and slider interfaces used for input control. The performance of a slider or button under changing environmental conditions is important for final implementation and can vary based on the use case.

Software algorithms are used to process the sensor data to combat the various interferences that can affect the expected processed results. This article describes an adaptive filtering technique that can be used to improve sensor performance under changing environmental conditions, such as noise, moisture and temperature drift.

A slider is a series of touch buttons cascaded together with overlapping transitions and designed specifically to track finger position and movement. A slider is designed to detect the finger position as accurately as is necessary for an application.

An individual button detects the presence of a finger touch or the absence of a touch. Buttons and sliders can be used to detect simple single-touch motions or specific gestures, such as multiple taps and/or sliding touch for more sophisticated user control. (*Silicon Labs offers several 8-bit and 32-bit microcontrollers (MCUs) with capacitive sensing capabilities including the 8-bit C8051F99x, C8051F8xx, C80517xx and 32-bit Precision32 SiM3U1xx MCUs.* ).

**Signal conditioning for capacitive touch sensing in sliders**

The dynamic behavior of the signal output from the touch sensor is described as the sum of a touch delta and a baseline. Combined, these two states create the output from the capacitive sensing block in the microcontroller.

The Kalman filter is an approach that is presented as an optimized filter of the sensor data. This filtering method has advantages over simple low-pass filters, which can be limited by delayed response times. Also, a low-pass filter does not track the baseline, and this must be done separately.

The Kalman filter monitors the two state variables based on the dynamic model of the touch delta and the baseline. The final position output on the slider benefits from further filtering using dejitter and deglitching algorithms to smooth out the data under noisy conditions. The following block diagram in **Figure 1 ** shows the data flow from input into the sensor to positional output, computing the position of the finger on the slider.

**Figure 1. Data flow from raw data to position output along a slider.**

A state-space model is a method of describing estimation and control problems. It shows how a new state X (i+1) is modeled as a linear combination of both the previous state X (i) and some process noise U (i). A measurement equation describes the way the process measurements or observations Y (i) are derived from the internal state X (i).

These two equations are referred to as the process model and the measurement model and serve as the origin for all linear estimation methods, such as the Kalman filter. The state space model is often used to solve the black box problem, which is to determine the internal states of a linear system given access only to the system’s outputs. The following equation provides an example of a process and measurement model. A is the state space matrix; G is the input transfer function, and H is the output transfer function in **Equation 1 below.**

*X (i+1) =AX (i) +GU (i) * **Process model**

*Y (i) =H (i) X (i)* ** Measurement model**

**Equation 1. State-space model**

The Kalman filter is an optimized estimation method for analyzing the behavior of dynamic systems with noisy measurement data. The filter estimates and predicts the behavior of the next output based on the model and then uses the measured value to try to correct it.

It is essentially a set of mathematical equations that implement a predictor-corrector type estimation that is optimal in the sense that it minimizes the estimated error covariance when some presumed conditions are met. This filter has been extensively researched for navigation type applications but can be applied to many other types of problems.

The Kalman filter addresses the general problem of trying to estimate the state of a discrete time-controlled process that is governed by the linear stochastic difference equation. The process noise covariance and measurement noise covariance change and are updated with each measurement.

The system model is of the form:

The matrix holds the coefficients for the two state variables for the touch delta and the baseline, and the state variables are touch delta capacitance, **x(k) _{1} ** , and baseline capacitance,

**x(k)**.

_{2}The system model can be derived from the dynamic equation of the form: *dx/dt=f(x).*

Consider the dynamics of the touch capacitance delta, **x(k) _{1} ** , as a first-order filter of the following form:

Note that** τ** is the first-order time constant. Replacing *dx/dt * with the discrete form where T is the sample rate, the equation is derived into the format of the system model above in **Equation 2** .

Next, consider the baseline capacitance, **x(k) _{2} ** . The dynamics of this are such that there is no rate of change, and the filter can only shift the baseline values using the disturbance covariance.

Therefore, the filter matrix , such that the process model becomes the following:

The measurement output model is simply the sum of xk_{1} +xk_{2} , which is the touch delta and the baseline.

The diagram in **Figure 2 ** is helpful to gain an understanding of the predictor-corrector update and flow of the states in the filter, describing which equations are used and updated. Note that this sequence is done for every channel used as a button in the slider.

On the following page are the descriptions of the equations that make up the Kalman filter.

**PREDICT: Time Update**

- Prediction of the next state variables:

This equation represents the system model, which includes model error.

2. Predict the next error covariance matrix:

Ck is the covariance matrix of the estimation error. Cprek is the predicted value. W is the model error covariance. **Ф ** is the model.

**CORRECT: Measurement Update**

- Compute the Kalman Gain:

Use the predicted values to obtain K, beginning the estimate update.

K is the output Kalman gain. Dk is a 1×2 matrix [1 1] for the output matrix to sum the two states. Q is the measurement error covariance, which is a scalar.

2. Update the estimate with the measurement, Zk, and use K to obtain an update to the estimate:

The updated estimate for the state variables is the predicted value + Kalman Gain multiplied by the difference between the measured and predicted values.

3. Update the Error Covariance Matrix:

The error covariance matrix is the predicted value subtracting the Kalman gain multiplied by the predicted value.

4. Update the state variable:

Update the state variable as the predicted output is corrected, then iterate back to the prediction of the next state variables.

Xk=Xk_1

There must be an initial estimate of the confidence levels for the error covariance matrix, Ck, and for the state variables, Xk. For the confidence level, a smaller value gives less variance. The model disturbance covariance matrix, W, is important for tuning the filter. If these values are not tuned correctly, it will change the behavior of the filter drastically. These values affect how fast the filter will respond to the disturbance on the state variables.

For the touch delta, a high value is required so that the touch delta is very responsive. For the baseline, a very small value is necessary since it is expected that the baseline will remain fairly stable and constant. If water suddenly appears on the sensor, this may cause a sudden shift in the baseline, and, if the disturbance value for the baseline is too low, the filter will not be able to immediately adjust for the sudden shift.

Some intelligence is required to quickly catch these scenarios to update the covariance values based on measured conditions. Q is the noise covariance scalar, which is the noise on the output measurement.

The output of the Kalman filter is Xk for each sensor in the slider. The touch delta values from Xk are then sent into the centroid algorithm to track the position along the slider.

After reviewing the equations for the Kalman filter, it becomes evident that a simple low-pass filter with baseline adjustments and updates is simpler to understand and implement. For basic use cases, this approach may be appropriate. The advantage of the Kalman filter is that it is responsive and can track the output in uncertain environmental conditions based on a self-contained model. The disadvantage is that tuning the filter can be difficult in some use cases.

**Centroid Algorithm**

The centroid algorithm is used to compute the position along the slider. The filtered touch delta data, xk_{1} , is sent into the centroid algorithm. The centroid algorithm scales appropriately to the observed touch delta.

The centroid equation for a three-button slider is the following:

In this equation, x1, x2 and x3 represent the touch delta value for channels 1, 2 and 3. Bin1, bin2 and bin3 are scaling coefficients. The numerator is multiplied by a bin number, which is an incremental value along a linear scale. The bin number should be scaled large enough to give some granularity since decimal numbers will not be used.

If the sum of the touch deltas for all buttons in the slider is less than a predetermined threshold, then the algorithm keeps the centroid at zero. Otherwise, the centroid output is computed. This threshold is determined based on the touch strength of a valid touch, which will change based on the button size and the overlay.

The user-adjustable parameters are the following:

- Number of Channels in the Slider
- Bin Numbers: These may depend on the granularity. They divide the bins into whole numbers or make the scaling correlate to something in the end application.
- Sum Threshold: This value is the sum of the touch deltas on the entire slider, which is used to determine whether a valid touch is occurring.

**Results Using Adaptive Filtering**

**Figure 3 ** shows the touch delta output from three slider buttons with a 2.88 mm overlay. The initial baseline is subtracted off from the output capacitance measurement. The data shows the same test sequence under different environmental conditions. The test sequence is as follows:

- Fast Double Tap on Button 1, 2 and then 3
- Fast Slide Left to Right, then Right to Left
- Long Touch on Button 1, 2 and then 3
- Slow Slide Left to Right, then Right to Left

The first test sequence was performed under normal conditions with no external noise or water sources. The second sequence was performed with a fluorescent light 2 cm away from the slider. The noise is seen in the data when the light is turned on. The third test sequence was performed with water on the finger and the fluorescent light 2 cm away from the slider. Some baseline shifting is evident, as well as noise, particularly in Sequence 3.

**Figure 3. Capacitance Output in Counts for Button 1, 2 and 3 vs. Time under Different Environmental Conditions**

The raw data from the sensor goes through the Kalman filter and then through the centroid algorithm. After obtaining the results from the centroid algorithm, the data goes through a final deglitch and de-jitter routine.

The deglitch routine looks for erroneous flickering where the output bounces within one or two time-steps from above a set threshold to the zero position and then back above a set threshold or vice versa.

A simple way to reduce the shaking appearance of the output due to jittery fingers or noise is to compare the last two values and only update the next value if it is greater than a set threshold.

**Figure 4: Centroid Position Output on a Three Channel Slider (Normal, Fluorescent Light Noise, Noise and Water)**

**Figure 4 ** shows the final centroid position output along a three-button slider after the input data has been filtered with the Kalman filter and deglitch/de-jitter routines. The vertical axis shows the position along the slider, and the horizontal axis shows time. In all three test cases, the position signals along the slider are recoverable.

**Conclusions**

With proper tuning, the Kalman filter provides a viable signal conditioning approach for dealing with uncertain environmental conditions affecting capacitive-sensing buttons and sliders. Implementing this kind of solution poses both advantages and disadvantages compared to the simpler approach of using a first-order, low-pass filter with separate baseline tracking.

The Kalman filter is an adaptive technique that corrects the input data and helps to correlate it to the model, which can provide continuous feedback into the system without delayed response. This technique can be used to improve the performance of buttons and sliders, and it also can be applied to many different types of problems to analyze the behavior of dynamic systems.

The Kalman filter provides a very powerful filtering and tracking technique in the presence of noise and uncertainty, and it can be used to solve other problems beyond interface navigation, where it is typically applied.

**Kimberly Tuck** is a systems & applications engineer specializing in microcontroller-based design of touch sensors, accelerometer and MEMS based devices at **Silicon Laboratories.** * Previously employed at Freescale Semiconductor and Zyvex Corp. she received her enginnering degree from the University of Waterloo.*

A version of this article originally appeared on Embedded.com’s system publication, EDN Magazine. http://www.edn.com/design/analog/4413345/Using-adaptive-filtering-to-enhance-capacitive-sensing-of-buttons-sliders