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

saadc nRF52 devkit not getting readings in polled or event driven.

I am using an nRF52832 PCA10040 board, using s132_nrf52_2.0.0_softdevice.hex, and have AIN0 connected to a potentiometer similar to this. The power input pin is connected to the 2.8 Volt VDD and the out is connected to ground. If I intersect the AIN0 pin with a multimeter the DC voltage readings range from -0.063 volts to 0.050 volts, depending on the rotation of the dial on the potentiometer. I have successfully polled expected results from the potentiometer using a 4D OLED display device and the IO1 pin.

I have tried both polling, and buffer entry for gathering readings from the AIN0 pin and have not succeeded.

The following is my saadc init:

#define SAMPLES_IN_BUFFER 5
static nrf_saadc_value_t       m_buffer_pool[2][SAMPLES_IN_BUFFER];

void adc_config(void)
{
		ret_code_t err_code;
	
/////////////////////////////////////////////////////	
	
	////////////////////////////////////////////
	nrf_drv_saadc_config_t nrf_drv_saadc_config;
	nrf_drv_saadc_config.resolution = NRF_SAADC_RESOLUTION_10BIT;
	nrf_drv_saadc_config.oversample = NRF_SAADC_OVERSAMPLE_DISABLED;
	nrf_drv_saadc_config.interrupt_priority = APP_IRQ_PRIORITY_HIGH;
	////////////////////////////////////////////
	
	nrf_saadc_channel_config_t saadc_chan_conf = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);;
	saadc_chan_conf.reference = NRF_SAADC_REFERENCE_INTERNAL;
	saadc_chan_conf.resistor_p = NRF_SAADC_RESISTOR_PULLDOWN;
	////////////////////////////////////////////

// Initialize and configure ADC
err_code = nrf_drv_saadc_init(&nrf_drv_saadc_config, saadc_event_handler);
	APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_channel_init(0,&saadc_chan_conf);
	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);
}

Then I have attempted taking readings by entering an endless loop:

nrf_saadc_value_t value;
while(true){
err_code = nrf_drv_saadc_sample_convert(SAADC_CH_PSELP_PSELP_AnalogInput0,&value);
APP_ERROR_CHECK(err_code);
}

When I view the value of 'value' it always ends up with 0XFFFE +/-, and sometimes jumps down to 0x0001 +/-. If I change the settings to resistor disabled, it just hangs out bouncing around the values of 0x00A1 +/-. These reading changes do not correspond to when I rotate the dial on the potentiometer.

If I try using a buffer, I use the following simple method for triggering the reading.

    adc_config();
while(1)
{
  uint32_t err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_sample();
APP_ERROR_CHECK(err_code);
}

If i put a breakpoint on the second APP_ERROR_CHECK, and look at my buffer which I include in the adc_config() by un-commenting the buffer config line, I can see that no values in the buffer array are ever changed as it loops through taking samples. In fact, some reason the only time they are ever modified from equaling 0x0000, is strangely when the following code operates before adc_config() is ran,

APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);	
		err_code = app_timer_create(&m_timer_id, APP_TIMER_MODE_REPEATED, event_timer_handler);
		APP_ERROR_CHECK(err_code);
		err_code = app_timer_start(m_timer_id, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
    APP_ERROR_CHECK(err_code);

some reason app_time_create modifies the array values in index 1 of m_buffer_pool[2][5] and app_time_start modifies the values at index 0, which doesn't seem correct to me.

I would be incredibly happy if somebody could at least help me figure out why I can't get responsive values by doing a simple polling method on the ADC, thanks for any help you can offer.

  • This solved my problem. I set the pin for my AIN4 to have pulldown input so it would allow enough voltage through for testing. At the beginning of my code I set this.

    nrf_gpio_cfg_input(28,NRF_GPIO_PIN_PULLDOWN);
    

    I was also having problems getting enough voltage through from the VDD to ThinPotVSS(ground) because it was routed through a pin, so I used the same function on the ThinPotVSS pin as well at the beginning of my code to increase the voltage and allow input.

Related