How to read the value from ADC

mr.miyagi gravatar image

asked 2013-08-29 08:38:10 +0100

updated 2013-09-02 07:47:33 +0100


i want to use the adc, i use the code from here https://devzone.nordicsemi.com/index.... and it works fine. But i don#t know how to handle the ->Result when i got 5 ADC Channels to use. It would be great if some one can tell me how to use ->Result and how to confert it to some variables

best regards Nils

edit retag flag offensive close delete report spam


7 answers

Sort by ┬╗ oldest newest most voted
olha gravatar image

answered 2013-08-30 09:56:33 +0100

updated 2013-09-02 07:47:33 +0100

The ADC will measure one input at a time, so depending on which input was selected in PSEL when you started the measurement, the RESULT when you get the END event will be the result for this particular input. No special handling of this should be needed; if you need to measure 5 different inputs, you'll just have to cycle through them.

edit flag offensive delete publish link more

some ADC examples are now available here on Gibhub https://github.com/NordicSemiconductor/nrf51-ADC-examples

Stefan Birnir Sverrisson ( 2014-06-03 20:35:16 +0100 )editconvert to answer
Bouillou gravatar image

answered 2013-08-29 08:51:11 +0100

Here is an example of ADC reading value :

/**@brief ADC interrupt handler.
 * @details  This function will fetch the conversion result from the ADC, convert the value into
 *           percentage and send it to peer.
void ADC_IRQHandler(void)
    if (NRF_ADC->EVENTS_END != 0)
        uint8_t     adc_result;
        uint16_t    batt_lvl_in_milli_volts;
        uint8_t     batt_lvl_formatted;         
        uint8_t     percentage_batt_lvl;
        uint32_t    err_code;

        NRF_ADC->EVENTS_END     = 0;
        adc_result              = NRF_ADC->RESULT;
        NRF_ADC->TASKS_STOP     = 1;

        batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) ;//+ DIODE_FWD_VOLT_DROP_MILLIVOLTS;
        percentage_batt_lvl     = battery_level_in_percent(batt_lvl_in_milli_volts);

              batt_lvl_formatted = batt_lvl_in_milli_volts/100;

              if (batt_lvl_formatted != m_bas.battery_level_last)
                    uint16_t len = sizeof(uint8_t);

                    // Save new battery value
                    m_bas.battery_level_last = batt_lvl_formatted;

                    // Update database
                    err_code = sd_ble_gatts_value_set(m_bas.battery_level_handles.value_handle,
        err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl);
        if (
            (err_code != NRF_SUCCESS)
            (err_code != NRF_ERROR_INVALID_STATE)
            (err_code != BLE_ERROR_NO_TX_BUFFERS)
            (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)

/**@brief Function to make the ADC start a battery level conversion.
static void adc_start(void)
    uint32_t err_code;

    // Configure ADC
    NRF_ADC->CONFIG     = (ADC_CONFIG_RES_8bit                        << ADC_CONFIG_RES_Pos)     |
                          (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos)  |
                          (ADC_CONFIG_REFSEL_VBG                      << ADC_CONFIG_REFSEL_Pos)  |
                          (ADC_CONFIG_PSEL_Disabled                   << ADC_CONFIG_PSEL_Pos)    |
                          (ADC_CONFIG_EXTREFSEL_None                  << ADC_CONFIG_EXTREFSEL_Pos);

    // Enable ADC interrupt
    err_code = sd_nvic_ClearPendingIRQ(ADC_IRQn);

    err_code = sd_nvic_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_LOW);

    err_code = sd_nvic_EnableIRQ(ADC_IRQn);

    NRF_ADC->EVENTS_END  = 0;    // Stop any running conversions.
    NRF_ADC->TASKS_START = 1;       
edit flag offensive delete publish link more


for this example no need to connect any of analog pin to VDD or VCC??

KETAN_PATEL ( 2017-10-11 09:04:44 +0100 )editconvert to answer
stefanbirnir gravatar image

answered 2013-09-12 12:12:00 +0100

Hi Nils

Sorry for my late entrance into this thread. I have attached an ADC example code that is perhaps more relevant for your purpose. It samples from two different ADC input pins and stores the result in two different variables. It is tested with the nRF51822 development kit and SDK 4.3


edit flag offensive delete publish link more


Hi, thank you for your help, but my code works now, after trying soem changing of my code :) Thanks

Nils Minor ( 2013-09-12 13:40:22 +0100 )editconvert to answer

the link is broken,is the zip available somewhere else please?

Wayne ( 2014-05-27 14:20:33 +0100 )editconvert to answer

Stefan's solution worked very well. I just implemented it with no troubles. Thank you very much.

jonf ( 2014-10-02 21:53:04 +0100 )editconvert to answer

You are welcome. Please vote for the answers that you find helpful.

Stefan Birnir Sverrisson ( 2014-10-03 09:24:11 +0100 )editconvert to answer
mr.miyagi gravatar image

answered 2013-09-02 07:47:30 +0100

jcowfer.ds gravatar image

updated 2015-03-17 15:32:22 +0100

Ahh ok thank you

edit flag offensive delete publish link more
mr.miyagi gravatar image

answered 2013-08-29 14:18:35 +0100

Hi, thanks for the answer.

Mhh i don't get it with this code. You also handle only one adc mesurement (adc_result), but i need to handle 5 mesurements from the ADCs but i don#t understand how to use ->ADC_RESULT

thank you, Nils

edit flag offensive delete publish link more
mr.miyagi gravatar image

answered 2013-08-30 08:09:36 +0100

Can anyone explain to me how to handle the RESULT pls?

thanks Nils

edit flag offensive delete publish link more
olha gravatar image

answered 2013-08-30 09:59:32 +0100

kalauffenburger gravatar image

updated 2015-06-01 00:59:48 +0100

You won't normally get a quicker answer by posting several replies (that aren't actual replies, but just bumping) to your own question. We try to tend to all questions here as we have time, but some days are more busy than others. Posting a complete, clear question, with detailed description on what you need to do, what exactly is the problem, and what you've already tried is the best way to get a quick reply.

edit flag offensive delete publish link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower


Asked: 2013-08-29 08:38:10 +0100

Seen: 9,482 times

Last updated: sep. 02 '13