nRF5 SDK v12.1.0
Capacitive Sensor

The Capacitive Sensor driver provides support for power-efficient capacitive sensing.

The number of supported capacitive pads is equal to the number of available analog channels – 8. This driver has two different implementations, depending on whether you use it with nRF51 or nRF52:

Implementation details

Despite the two different implementations for nRF51 and nRF52, the driver uses the same API in both cases.

In the implementation for nRF51, the ADC module measures voltage on the pad that is touched. In this implementation, an additional pin is needed to charge the pad through a resistor. On every measurement, the pad is charged and then discharged using the ADC module. If the pad is touched during measurement, its capacitance is higher. This is detected by ADC which receives a greater value of the measured voltage. Therefore, to set up this solution, you must know the value of the ADC measurement when the pad is idle to decide when it is touched.

For nRF52, the COMP module is used. This module supports single capacitive touch sensing. For details, refer to Capacitive sensing.

Resource usage

You can create only one instance of this driver. Additionally, the number of channels is limited to the number of analog channels, which for both nRF51 and nRF52 is eight.

Depending on the chip, the driver utilizes the following resources:

For nRF51:

For nRF52:

Important: If the Capacitive Sensor driver is used for analog input, it cannot be used by ADC, COMP, or LPCOMP as these three modules share resources.

Initialization and starting

There are two functions that you must run before you can start working with this driver.

ret_code_t err_code;
#ifdef NRF51
csense_config.output_pin = OUTPUT_PIN;
#endif
err_code = nrf_drv_csense_init(&csense_config, csense_handler);
APP_ERROR_CHECK(err_code);
nrf_drv_csense_channels_enable(analog_input_4 | analog_input_5 | analog_input_7);
err_code = nrf_drv_csense_sample();
APP_ERROR_CHECK(err_code);

First, you must initialize the module. If you are using nRF51, the only required configuration is to provide the number of the output pin that will be used to charge the pads. For nRF52, initialize the module without specifying any additional configuration parameters.

After the module is initialized, all channels are automatically disabled. To enable the channels, you must run the function nrf_drv_csense_channels_enable. After that, you can start the measurement by calling nrf_drv_csense_sample.

NOTE: For nRF52, even if all channels are disabled, there is always one channel that remains enabled after the COMP driver initialization. This channel cannot be used for ADC or LPCOMP because of sharing resources. After deinitializing COMP, the channel is available again.

Reading values from the pads

You can now start working with the module. Every time you call the nrf_drv_csense_sample function, the conversion of all enabled channels is performed. You can read the result through a handler or at any time after the conversion. However, data is overwritten after each conversion, so it can be read only until the next conversion.

uint8_t channel;
void csense_handler(nrf_drv_csense_evt_t * p_event_struct)
{
//print result of conversion on screen
printf("Channel: %d, \t value: %d.\n", p_event_struct->analog_channel,
p_event_struct->read_value);
channel = p_event_struct->analog_channel;
}
int main(void)
{
(…)
printf("From main - channel: %d, value: %d.\n", channel,
app_capsense_button_read(channel));
(…)
}

Example

See the Capacitive Sensor Driver Example for a full example that uses Capacitive Sensor driver.


Documentation feedback | Developer Zone | Subscribe | Updated