nRF52840 ADC configurations and implementation

Hi.
I'm using nRF Connect SDK 2.6.1.
I'm developing my own board based on nRF52840 SoC. So, I created an overlay file from nRF52840dk device tree. I could configure and implement SHT40, LIS3DH and a hall effect sensor successfully.
Now, I'm trying to configure and implement "AIN2" ADC channel to read the voltage of battery supply.
Before, I would assumed that reading an ADC channel be a very simple work as my previous experiments with other MCUs. But I was unsuccessful after 3 days spending for testing several configurations and available examples. The result of all configurations and tests are same.
Please, help me.

I explain my latest configurations step by step:


1. Configure ADC channel by using Devicetree Visual Editor:



2. Add <#address-cells> and <#size-cells> properties to adc:

&adc {
    #address-cells = <1>;
    #size-cells = <0>;
    channel@2 {
        reg = <2>;
        zephyr,gain = "ADC_GAIN_1_4";
        zephyr,reference = "ADC_REF_VDD_1_4";
        zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
        zephyr,resolution = <12>;
    };
};

zephyr,user {
    io-channels = <&adc 2>;
};

3. Add below structure as global in main.c:

static const struct adc_dt_spec adc_chan2 = ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);

4. Add below checking:

if (!adc_chan2.dev)
{
    printk("Error: ADC device not found\n");
    return;
}

5. Add below configuration:

if (adc_channel_setup(adc_chan2.dev, &adc_chan2.channel_cfg))
{
    printk("Error: Failed to configure ADC channel\n");
    return;
}
6. Add below reading process. I read ADC channel every 2 seconds:
uint16_t   raw_value;
if (adc_read_async(adc_chan2.dev, &adc_chan2.channel_cfg, &raw_value) == 0)
{
    // Convert raw ADC value to voltage (assuming VDD = 3.3V)
    float voltage = (raw_value * 3.3) / 4096.0;
    printk("AIN2 Voltage: %.2f V\n", voltage);
}
else
{
    printk("Error: ADC read failed\n");
}

7. Build the code successfully:
8. Debug


I'm so confused.
Why debug shows: <err> adc_nrfx_saadc: Channel 1 not configured !?
I'm using Channel 2!!!
Parents Reply
  • Hi again.
    I could found my mistake, finally,
    I had been mixed several online sample configurations and implementations without notice to their NCS version.
    So, I searched between my SDK samples and found "\zephyr\samples\drivers\adc" sample.
    I could use it easily and for future I decided at first search in SDK samples and then if I was unsuccess, search for the solution by internet.

Children
No Data
Related