Help with Converting SAADC ADC Value to Millivolts in nRF52810

hi ,

I am currently developing a project using the nRF52810 as my controller and SEGGER Embedded Studio (version 5.42a) as my IDE. I have successfully integrated the Timer and SAADC peripherals into the ble_app_uart/pca10040e/s112 example project from the nRF5 SDK.

In my application, I am using the 4th SAADC channel (AIN4) to read ADC values, which correspond to the device's battery voltage. I am using a 12-bit resolution and all the default configurations of the SAADC.

My goal is to convert the ADC value into a millivolt reading. Specifically, I need to:

 Convert this ADC value into a voltage (in millivolts, mV).

I understand that the value read from the ADC can be translated into a voltage based on the reference voltage and resolution, but I would appreciate guidance on how to accurately calculate the millivolts from the ADC value using the SAADC.

I am attaching the relevant portion of my SAADC code below for your reference.

******************************************************************************************************************************************

void saadc_sampling_event_init(void)
{
ret_code_t err_code;

err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code);

nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
APP_ERROR_CHECK(err_code);

/* setup m_timer for compare event every 400ms */
uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 500);
nrf_drv_timer_extended_compare(&m_timer,
NRF_TIMER_CC_CHANNEL0,
ticks,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
false);
nrf_drv_timer_enable(&m_timer);

uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer,
NRF_TIMER_CC_CHANNEL0);
uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();

/* setup ppi channel so that timer compare event is triggering sample task in SAADC */
err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
timer_compare_event_addr,
saadc_sample_task_addr);
APP_ERROR_CHECK(err_code);
}


void saadc_sampling_event_enable(void)
{
ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);

APP_ERROR_CHECK(err_code);
}


void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;

err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);

int i,sum=0;
// NRF_LOG_INFO("ADC event number: %d\n", (int)m_adc_evt_counter);
//printf("ADC event number: %d\n", (int)m_adc_evt_counter);

for (i = 0; i < SAMPLES_IN_BUFFER; i++)
{
int adc_value = p_event->data.done.p_buffer[i];
// NRF_LOG_INFO("%d\n", p_event->data.done.p_buffer[i]);
// printf("%d\n", p_event->data.done.p_buffer[i]);
sum+=adc_value;
}

average_adc_value = sum / SAMPLES_IN_BUFFER;
NRF_LOG_INFO("average adc value : %d\n",average_adc_value);
printf("average adc value : %d\n",average_adc_value);
m_adc_evt_counter++;

}
}


void saadc_init(void)
{
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN4);

err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);

}

*******************************************************************************************************************

Project Details:

  • SDK: nRF5 SDK 17.0.2
  • IDE: SEGGER Embedded Studio 5.42a
  • Controller: nRF52810
  • Peripherals: Timer and SAADC (using default configuration for SAADC, with a 12-bit resolution)

Any help or insights on converting the ADC value to millivolts would be greatly appreciated.

Thank you for your support.

Best regards,
SILTVM

Related