nRF5 SDK  v13.0.0-1.alpha
Choose documentation:
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
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 uses the COMP module, which 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 is eight.

The driver uses the following resources:

  • Two instances of timer. Make sure to edit the driver configuration file to specify which timers will be used in the solution.
  • Three PPI channels.
  • The COMP module.

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;
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 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: 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.