This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Simple ADC Code

Hello,

    I am want to read Analog value from LDR sensor. I am unable to find ADC Input pin in nrf52840 Dongle datasheet but I got it in nrf52840 chip datasheet (page -576) which is - P0.31. But as a newly bid I don't know how to read analog input. Can you give/provide me the code which having an only analog pin declaration, get read value (Please don't include any interrupt base, timer base or any other properties base function).

                                    waiting for your quick support.

                                            Thank you.

   

Parents Reply Children
  • Hello haakonsh,

         Glad to hear from you,

    I found the simple code see -

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #include "nrf.h"
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "nrf_drv_adc.h"
    #include "nordic_common.h"
    #include "boards.h"
    #include "nrf_log.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "app_util_platform.h"


    static nrf_adc_value_t adc_value; /**< ADC buffer. */
    static nrf_drv_adc_channel_t m_channel_config = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_2);

    /**
    * @brief Function for main application entry.
    */
    int main(void)
    {
    UNUSED_RETURN_VALUE(NRF_LOG_INIT());
    NRF_LOG_PRINTF("ADC example\r\n");

    while (true)
    {
    nrf_drv_adc_sample_convert(&m_channel_config,&adc_value);
    NRF_LOG_PRINTF("ADC buffer: %d\r\n",adc_value);
    nrf_delay_ms(100);
    }
    }
    /** @} */

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    But I got some error for compiling it for - PCA10059. See - 

    Rebuilding ‘blinky_pca10059_mbr’ from solution ‘blinky_pca10059_mbr’ in configuration ‘Release’
    Assembling ‘thumb_crt0.s’
    Compiling ‘nrf_log_frontend.c’
    Compiling ‘nrf_log_str_formatter.c’
    Compiling ‘boards.c’
    Compiling ‘app_error.c’
    Compiling ‘app_error_handler_gcc.c’
    Compiling ‘app_error_weak.c’
    Compiling ‘app_util_platform.c’
    Compiling ‘nrf_assert.c’
    Compiling ‘nrf_atomic.c’
    Compiling ‘nrf_balloc.c’
    Compiling ‘nrf_fprintf.c’
    Compiling ‘nrf_fprintf_format.c’
    Compiling ‘nrf_memobj.c’
    Compiling ‘nrf_ringbuf.c’
    Compiling ‘nrf_strerror.c’
    Compiling ‘nrfx_atomic.c’
    Compiling ‘main.c’
    nrf_drv_adc.h: No such file or directory
    Assembling ‘ses_startup_nrf52840.s’
    Assembling ‘ses_startup_nrf_common.s’
    Build failed

       Kindly help me to figure out. 

                                      Waiting for your quick response 

  • Why don't you just start with one of the ready-made SAADC examples?

    And, as said, it really is better to start your development on a DK rather than a dongle.

    A dongle is great for deploying stuff that you know already works. But, as the name suggests, a Development Kit (DK) is the place to do your development and learning.

    How to properly post source code:

  • Think about this line:

    #include "nrf_drv_adc.h"

    You have given just the name of the file - but how does the compiler (or, strictly, the preprocessor) know where to find it?

    The problem is that it doesn't - so it says:

    nrf_drv_adc.h: No such file or directory

    The way you tell it where to find your header files is via the Include Paths setting in your Project; for SES:

      

    Note that this is standard 'C' (and C++) programming stuff - not specific to Nordic or embedded.

    And, while the detail of where to find the setting is specific to SES, the concept is common to all IDEs; eg, in Keil:

    or Eclipse:

    The setting is passed through to the command line of the underlying toolchain; eg, for GCC:

    https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

    EDIT

    For SES, see also: https://infocenter.nordicsemi.com/topic/ug_gsg_ses/UG/gsg/add_content_ses.html

    #IncludePaths 

  • thank you so much for your guidance Mr. awneil. First I want to understand the codes of all project in-depth. After learning that I will go for the DK kit.Slight smile

  • It's probably easier to understand the code by seeing it running in the DK ...

Related