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

using more than 2 Analog pins in nrf52840DK

How to read values from 5 Analog pins at the same time

I know how to read from one Analog Pin, but i don't know how to access 5 analog pins at the same time and display the values in the putty terminal

Below i have written the code for displaying values from one flex sensor, connected  to AIN2

/**

 */

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"





/* Create an empty handler and pass this handler in the saadc initialization function
  > Normally this handler deals with the adc events but we are using blocking mode
  > In blocking mode the functions are called and the processor waits for the adc to finish taking samples from the respective channels
  > Event handler will not be called in this method
*/

void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
{
 // Empty handler function
}




// Create a function which configures the adc input pins and channels as well as the mode of operation of adc

void saadc_init(void)
{
	// A variable to hold the error code
  ret_code_t err_code;

  // Create a config struct and assign it default values along with the Pin number for ADC Input
  // Configure the input as Single Ended(One Pin Reading)
  // Make sure you allocate the right pin.
  nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);

  // Initialize the saadc 
  // first parameter is for configuring the adc resolution and other features, we will see in future tutorial
  //on how to work with it. right now just pass a simple null value
  err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
  APP_ERROR_CHECK(err_code);

// Initialize the Channel which will be connected to that specific pin.
  err_code = nrfx_saadc_channel_init(0, &channel_config);
  APP_ERROR_CHECK(err_code);

  

}



// A function which will initialize the Log module for us
void log_init(void)
{
	// check if any error occurred during its initialization
  APP_ERROR_CHECK(NRF_LOG_INIT(NULL));

	// Initialize the log backends module
  NRF_LOG_DEFAULT_BACKENDS_INIT();

}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
	
	// call the log initialization function
  log_init();

// call the saadc initialization function created above
  saadc_init();

// a struct to hold 16-bit value, create a variable of this type because our input resolution may vary from 8 bit to 14 bits depending on our configurations
// this variable holds the adc sample value
  nrf_saadc_value_t adc_val;


// Print a simple msg that everything started without any error
  NRF_LOG_INFO("Application Started!!!");


   
// Inifinite loop
    while (1)
    {
    const float VCC = 3.6; 
    const float R_DIV = 47500.0;
    
		// a blocking function which will be called and the processor waits until the value is read
		// the sample value read is in 2's complement and is automatically converted once retrieved
		// first parameter is for the adc input channel 
		// second parameter is to pass the address of the variable in which we store our adc sample value
      nrfx_saadc_sample_convert(0, &adc_val);

		// print this value using nrf log : here %d represents the integer value 
      NRF_LOG_INFO("Sample value Read: %d", adc_val);

      float flexV = adc_val * VCC / 1023.0;
      float flexR = R_DIV * (VCC / adc_val - 1.0);

      NRF_LOG_INFO("resistance of sensor:" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(flexR))
		
		// use nrf log and float marker to show the floating point values on the log
		// calculate the voltage by this: input_sample * 3.6 / 2^n (where n = 8 or 10 or 12 or 14 depending on our configuration for resolution in bits)
     
       
	   // give 500ms delay 
       nrf_delay_ms(500);
     
    }
}


/** @} */

Please tell me how to declare the 5 analog pins and get the values 

Don't hesitate to tell, how to change the code or add anything to code.

Parents
  • Hello,

    I suggest that you take a look at the saadc example from the SDK. 

    It will by default only read one pin as well, but try to add another channel. I fail to see where you trigger the sampling in your project snippet. 

    However, from the saadc example, try adding a new channel by duplicating these lines:

        nrf_saadc_channel_config_t channel_config =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);

    Once you have two channels working, try to add 3 more. 

    Best regards,

    Edvin

  • Sorry for late reply, i was busy during these days.

    if i insert the code in the main code,then these errors are comming

    Building ‘saadc_pca10056’ from solution ‘saadc_pca10056’ in configuration ‘Release’
    Compiling ‘main.c’
    redefinition of 'channel_config'
    previous definition of 'channel_config' was here
    Build failed

    My code(modified with the one you said):

    /**
    
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include "nrf.h"
    #include "nrf_drv_saadc.h"
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    #include "boards.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "app_util_platform.h"
    #include "nrf_pwr_mgmt.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    /* Create an empty handler and pass this handler in the saadc initialization function
      > Normally this handler deals with the adc events but we are using blocking mode
      > In blocking mode the functions are called and the processor waits for the adc to finish taking samples from the respective channels
      > Event handler will not be called in this method
    */
    
    void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
    {
     // Empty handler function
    }
    
    
    
    
    // Create a function which configures the adc input pins and channels as well as the mode of operation of adc
    
    void saadc_init(void)
    {
    	// A variable to hold the error code
      ret_code_t err_code;
    
      // Create a config struct and assign it default values along with the Pin number for ADC Input
      // Configure the input as Single Ended(One Pin Reading)
      // Make sure you allocate the right pin.
      nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
      nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
      // Initialize the saadc 
      // first parameter is for configuring the adc resolution and other features, we will see in future tutorial
      //on how to work with it. right now just pass a simple null value
      err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
      APP_ERROR_CHECK(err_code);
      err_code = nrf_drv_saadc_channel_init(0, &channel_config);
      APP_ERROR_CHECK(err_code);
    // Initialize the Channel which will be connected to that specific pin.
      err_code = nrfx_saadc_channel_init(0, &channel_config);
      APP_ERROR_CHECK(err_code);
    
      
    
    }
    
    
    
    // A function which will initialize the Log module for us
    void log_init(void)
    {
    	// check if any error occurred during its initialization
      APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    
    	// Initialize the log backends module
      NRF_LOG_DEFAULT_BACKENDS_INIT();
    
    }
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
    	
    	// call the log initialization function
      log_init();
    
    // call the saadc initialization function created above
      saadc_init();
    
    // a struct to hold 16-bit value, create a variable of this type because our input resolution may vary from 8 bit to 14 bits depending on our configurations
    // this variable holds the adc sample value
      nrf_saadc_value_t adc_val;
    
    
    // Print a simple msg that everything started without any error
      NRF_LOG_INFO("Application Started!!!");
    
    
       
    // Inifinite loop
        while (1)
        {
        const float VCC = 3.6; 
        const float R_DIV = 47500.0;
        
    		// a blocking function which will be called and the processor waits until the value is read
    		// the sample value read is in 2's complement and is automatically converted once retrieved
    		// first parameter is for the adc input channel 
    		// second parameter is to pass the address of the variable in which we store our adc sample value
          nrfx_saadc_sample_convert(0, &adc_val);
    
    		// print this value using nrf log : here %d represents the integer value 
          NRF_LOG_INFO("Sample value Read: %d", adc_val);
    
          float flexV = adc_val * VCC / 1023.0;
          float flexR = R_DIV * (VCC / adc_val - 1.0);
    
          NRF_LOG_INFO("resistance of sensor:" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(flexR))
    		
    		// use nrf log and float marker to show the floating point values on the log
    		// calculate the voltage by this: input_sample * 3.6 / 2^n (where n = 8 or 10 or 12 or 14 depending on our configuration for resolution in bits)
         
           
    	   // give 500ms delay 
           nrf_delay_ms(500);
         
        }
    }
    
    
    /** @} */

    The original code, i got is from this video.

    https://www.youtube.com/watch?v=5slaFFj2SI4

    can you please watch the video, and tell me where to add the code for other flex sensors, correctly.

Reply
  • Sorry for late reply, i was busy during these days.

    if i insert the code in the main code,then these errors are comming

    Building ‘saadc_pca10056’ from solution ‘saadc_pca10056’ in configuration ‘Release’
    Compiling ‘main.c’
    redefinition of 'channel_config'
    previous definition of 'channel_config' was here
    Build failed

    My code(modified with the one you said):

    /**
    
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include "nrf.h"
    #include "nrf_drv_saadc.h"
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    #include "boards.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "app_util_platform.h"
    #include "nrf_pwr_mgmt.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    /* Create an empty handler and pass this handler in the saadc initialization function
      > Normally this handler deals with the adc events but we are using blocking mode
      > In blocking mode the functions are called and the processor waits for the adc to finish taking samples from the respective channels
      > Event handler will not be called in this method
    */
    
    void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
    {
     // Empty handler function
    }
    
    
    
    
    // Create a function which configures the adc input pins and channels as well as the mode of operation of adc
    
    void saadc_init(void)
    {
    	// A variable to hold the error code
      ret_code_t err_code;
    
      // Create a config struct and assign it default values along with the Pin number for ADC Input
      // Configure the input as Single Ended(One Pin Reading)
      // Make sure you allocate the right pin.
      nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
      nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
      // Initialize the saadc 
      // first parameter is for configuring the adc resolution and other features, we will see in future tutorial
      //on how to work with it. right now just pass a simple null value
      err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
      APP_ERROR_CHECK(err_code);
      err_code = nrf_drv_saadc_channel_init(0, &channel_config);
      APP_ERROR_CHECK(err_code);
    // Initialize the Channel which will be connected to that specific pin.
      err_code = nrfx_saadc_channel_init(0, &channel_config);
      APP_ERROR_CHECK(err_code);
    
      
    
    }
    
    
    
    // A function which will initialize the Log module for us
    void log_init(void)
    {
    	// check if any error occurred during its initialization
      APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    
    	// Initialize the log backends module
      NRF_LOG_DEFAULT_BACKENDS_INIT();
    
    }
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
    	
    	// call the log initialization function
      log_init();
    
    // call the saadc initialization function created above
      saadc_init();
    
    // a struct to hold 16-bit value, create a variable of this type because our input resolution may vary from 8 bit to 14 bits depending on our configurations
    // this variable holds the adc sample value
      nrf_saadc_value_t adc_val;
    
    
    // Print a simple msg that everything started without any error
      NRF_LOG_INFO("Application Started!!!");
    
    
       
    // Inifinite loop
        while (1)
        {
        const float VCC = 3.6; 
        const float R_DIV = 47500.0;
        
    		// a blocking function which will be called and the processor waits until the value is read
    		// the sample value read is in 2's complement and is automatically converted once retrieved
    		// first parameter is for the adc input channel 
    		// second parameter is to pass the address of the variable in which we store our adc sample value
          nrfx_saadc_sample_convert(0, &adc_val);
    
    		// print this value using nrf log : here %d represents the integer value 
          NRF_LOG_INFO("Sample value Read: %d", adc_val);
    
          float flexV = adc_val * VCC / 1023.0;
          float flexR = R_DIV * (VCC / adc_val - 1.0);
    
          NRF_LOG_INFO("resistance of sensor:" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(flexR))
    		
    		// use nrf log and float marker to show the floating point values on the log
    		// calculate the voltage by this: input_sample * 3.6 / 2^n (where n = 8 or 10 or 12 or 14 depending on our configuration for resolution in bits)
         
           
    	   // give 500ms delay 
           nrf_delay_ms(500);
         
        }
    }
    
    
    /** @} */

    The original code, i got is from this video.

    https://www.youtube.com/watch?v=5slaFFj2SI4

    can you please watch the video, and tell me where to add the code for other flex sensors, correctly.

Children
  • Hello saver,

    Edvin is currently out of office, and I will be proceeding with this case in his absence.

    saver said:

    if i insert the code in the main code,then these errors are comming

    Building ‘saadc_pca10056’ from solution ‘saadc_pca10056’ in configuration ‘Release’
    Compiling ‘main.c’
    redefinition of 'channel_config'
    previous definition of 'channel_config' was here
    Build failed

    In line 49 of the code posted in your previous comment you are redefining your channel_config variable from line 48.
    Please change the name of your second channel configuration, so that they do not match exactly.

    Try this, and let me know if it resolves your issue.

    Best regards,
    Karl

Related