Attempting to get ADC to work

I am trying to get a very simple ADC reading to print to the console using the nRF5340DK. I have a voltage divider with a photoresistor and a regular resistor, and I am trying to pass the output voltage as an analog input to P0.04. I am using nRF Connect in VSCode, version v2023.4.179. I am using the onboard VDD (3V) and GND for the circuit. I have enabled ADC in the prj.conf file. I will attach my main.c file to this. When I build and flash the code to the board, I get error -22 from adc_read. I would greatly appreciate any advice to solve this issue. Thank you!

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/util.h>
#define INPUT_PIN 0
#define ADC_NODE DT_NODELABEL(adc)
static const struct device * dev = DEVICE_DT_GET(ADC_NODE);
#define ADC_RESOLUTION 10
#define ADC_GAIN ADC_GAIN_1
#define ADC_REFERENCE ADC_REF_INTERNAL
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME_DEFAULT
#define ADC_CHANNEL 0
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hi, I will have a look at it. 

    Can you post your prj.conf and any overlay file?

    Is this the error you see:

    Fullscreen
    1
    2
    <err> adc_nrfx_saadc: Channel 0 not configured
    read failed -22
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Regards

    Runar

  • Hi Runar,

    Thank you for replying! All I can say is that the adc_read function returns error -22. I have attached the prj.conf file. I have not used any overlay file.3365.prj.conf

  • Hi all,

    I was able to resolve my own issue by learning more about devicetree overlays. I simply used the zephyr adc example and wrote my own devicetree overlay because the adc example does not contain one for the nRF5340. I will attach it here as well. Runar, thank you for your help, my issue is resolved.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    / {
    zephyr,user {
    io-channels = <&adc 0>, <&adc 1>;
    };
    };
    &adc {
    #address-cells = <1>;
    #size-cells = <0>;
    channel@0 {
    reg = <0>;
    zephyr,gain = "ADC_GAIN_1_6";
    zephyr,reference = "ADC_REF_INTERNAL";
    zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
    zephyr,input-positive = <NRF_ADC_AIN0>; /* P0.04 */
    zephyr,resolution = <12>;
    };
    channel@1 {
    reg = <1>;
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Glad to hear your problem is solved and thanks for posting the fix

    Regards

    Runar