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

Not able to read ADC on nrf5340

Hello, 

I have been trying to get the ADC working but for some reason, the adc_read function throws an error of invalid argument ( -EINVAL (-22)). The adc_channel_setup function returns 0.

I'm using SDK v1.3.0 and given below is my code and config: 

void main(void)
{
	struct device *dev;
	struct device *adc;
	bool led_is_on = true;
	int ret;
	s16_t buffer;
	const struct adc_sequence_options options = { 0, NULL, 0 };
	
	const struct adc_channel_cfg ch_cfg = {
    .gain = ADC_GAIN_1_6,
	// .differential = 0,
    .reference = ADC_REF_INTERNAL,
    .acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40),
    .channel_id = 0,
	#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
	.input_positive   = NRF_SAADC_NS_BASE,
#endif
	};

	const struct adc_sequence seq = {
		.options = &options,
		.channels = BIT(0),
		.buffer = &buffer,
		.buffer_size = sizeof(buffer),
		.resolution = 14,
		.oversampling = 4,
		.calibrate = false,
	};



	adc = device_get_binding(DT_LABEL(DT_ALIAS(adc_0)));
	if (!adc) {
		printk("adc device not found\n");
		return;
	}
	else {
		printk("ADC has been bound\n");
	}


	int err = adc_channel_setup(adc, &ch_cfg);
	if (err) {
		printk("failed to setup ADC channel -- %d\n", err);
		return;
	}
	else{
		printk("ADC setup is done!\n");
	}


	while (1) {

		int ret = adc_read(adc, &seq);
		printk("ADC %u , %i , %i\n", 0, ret, buffer);
		k_sleep(K_MSEC(1000));
	}
}

Project config file:

CONFIG_GPIO=y
CONFIG_ADC=y
CONFIG_CONSOLE=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_ADC_ASYNC=y

overlay file:

&adc {

  status = "okay";

};

Some help would be much appreciated.

Thank you!

Related