Code sample for Adc in nrf5340

Hi, 

Do you have a code sample for configuring adc using the internal reference voltage (600 mV)? I tried the below code, but I do not see relevant values:

#define ADC_NODE        DT_NODELABEL(adc)
#define ADC_RESOLUTION 10
#define ADC_CHANNEL 0

int16_t sample_buffer[1];


static void setup_adc(int chan_num)
{
    const struct device *adc_dev;
    struct adc_channel_cfg channel_cfg;

    adc_dev = DEVICE_DT_GET(ADC_NODE);
    
    if (!adc_dev) {
        printk("ADC device not found\n");
        return;
    }

    channel_cfg.gain = ADC_GAIN_1;
    channel_cfg.reference = ADC_REF_INTERNAL;
    channel_cfg.acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10);
    channel_cfg.channel_id = chan_num; // Set your desired channel ID

    if (adc_channel_setup(adc_dev, &channel_cfg) < 0) {
        printk("ADC channel setup error\n");
        return;
    }

    const struct adc_sequence sequence = {
        .channels = BIT(chan_num),
        .buffer = sample_buffer,
        .buffer_size = sizeof(sample_buffer),
        .resolution = ADC_RESOLUTION,
    };

    if (adc_read(adc_dev, &sequence) < 0) {
        printk("ADC read error\n");
        return;
    }

    int32_t mv_value = sample_buffer[0];
	int32_t adc_vref = adc_ref_internal(adc_dev);
    printk("\nADC reference voltage is %d\n", adc_vref);
    printk("\nADC raw value is %d\n", mv_value);
	adc_raw_to_millivolts(adc_vref, ADC_GAIN_2, ADC_RESOLUTION, &mv_value);
	printk("ADC-voltage: %d mV\n", mv_value);

}
Parents Reply
  • Hello,

    vgandl said:
    The signal I need to measure is 1.65 V, so my reference voltage needs to be atleast that much.

    Could you elaborate on this requirement?
    In general, your reference voltage does not need to be equal to the signal you would like to sample, it only needs to be a set reference point to compare the measured signal against.

    vgandl said:
    Which adc driver in zephyr can I best use for this purpose for the nrf5340

    Your should use the default nrfx drivers when working with nordic components.

    Best regards,
    Karl

Children
No Data
Related