Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Using the ADC module on the nRF52

December 5, 2023

Using the ADC Module on the nRF52

The nRF52 is a powerful microcontroller that boasts a range of features, including an Analog-to-Digital Converter (ADC) module. This module allows you to measure analog signals and convert them into digital values that can be processed by your code.

In this blog post, we will explore how to use the ADC module on the nRF52 and provide examples and explanations to help you get started.

Setting up the ADC module

Before we start using the ADC module, we need to set it up. First, make sure you have the necessary SDK and IDE installed on your machine. Once done, follow these steps:

Step 1: Initialize the ADC

To begin, let’s include the necessary headers and initialize the ADC module:

1
2
3
4
5
6
7
#include <nrfx.h>
#include <nrfx_adc.h>

void adc_init() {
    nrfx_adc_config_t adc_config = NRFX_ADC_DEFAULT_CONFIG;
    nrfx_adc_init(&adc_config, NULL);
}

Make sure to call the adc_init() function in your main setup routine.

Step 2: Configure the ADC channel

Next, we need to configure the ADC channel we wish to use. Each nRF52 chip has a different number of ADC channels available, so consult your chip’s datasheet to determine which channel to use. As an example, let’s configure channel 0:

1
2
3
4
void adc_configure_channel() {
    nrf_adc_channel_config_t channel_config = NRF_ADC_DEFAULT_CHANNEL_CONFIG_SE(ADC_CONFIG_PSEL_AnalogInput0);
    nrfx_adc_channel_init(0, &channel_config);
}

In this example, we’re using the single-ended input mode on Analog Input 0.

Step 3: Start the ADC

We’re almost there! Now, let’s start the ADC so that it can begin converting analog signals:

1
2
3
void adc_start() {
    nrfx_adc_start(NULL, NULL);
}

Step 4: Read the ADC value

Finally, we can read the ADC value using the nrfx_adc_sample_convert() function:

1
2
3
4
5
uint16_t adc_read() {
    uint16_t result = 0;
    nrfx_adc_sample_convert(0, &result);
    return result;
}

This function will return a 16-bit value representing the digital conversion of the analog signal.

Example usage

Now that we have everything set up, let’s look at an example usage scenario. Suppose we want to measure the voltage of a potentiometer connected to Analog Input 0. We can modify the code above as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>

int main(void) {
    adc_init();
    adc_configure_channel();
    adc_start();

    while (1) {
        uint16_t adc_value = adc_read();
        float voltage = adc_value * 3.3 / 4095;  // Assuming 12-bit resolution and 3.3V reference voltage
        printf("Voltage: %.2fV\n", voltage);
    }
}

In this example, we read the ADC value inside an infinite loop and calculate the corresponding voltage by scaling it between 0 and 3.3 volts.

Closing thoughts

Using the ADC module on the nRF52 is a powerful way to measure analog signals in your projects. By following the steps outlined in this blog post and studying the provided example, you should now have a good understanding of how to use the ADC module effectively.

Remember to consult the nRF52 reference manual and datasheets for more advanced features and configurations available for the ADC module.

Happy coding!


➡️ Implementing Bluetooth Low Energy (BLE) on the nRF52


⬅️ Exploring PWM functionality on the nRF52


Go back to Posts.