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

Continuous periodic nRF52840 ADC sampling with Zephyr

Hello, guys.

We are using Zephyr RTOS (NCS v1.4.2) with nRF52840 development kit.

The idea is to have continuous ADC sampling on two ADC channels. So far, I was able to properly initialize the ADC device, and get one single ADC read with the following piece of code:

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

ret = adc_read(adc_dev, &sequence);
if (ret) {
    printk("adc_read() failed with code %d\n", ret);
}

There is an issue even with this piece of code because, when I call adc_read(adc_dev, &sequence) function, only the first value of m_sample_buffer is updated, the rest of the m_sample_buffer values remain 0. What I am missing here?

Other than that, is there any way that we configure ADC device to do periodic sampling in the background without our intervention?

Thanks in advance for your time and efforts.

Sincerely,

Bojan.

Parents
  • Thank you for the feedback, Bojan. We are happy to help Slight smile I have requested the case to become public but you need to approve it.

    Einar

  • Hello, Einar.

    I hope you are doing well. There is one strange thing I noticed recently while playing with ADC.

    It can happen that the ADC samples in buffer swap the position:

    As you can see above, I am sampling only two ADC channels. My configuration is similar like I found in your unofficial SAADC example:

    - There are two sampling buffers, both of size 2 (to store one ADC sample of each channel).

    In my particular use case, I am playing with ADC limits (both HIGH and LOW). This is working fine. For both ADC channels, I was able to set the limits and to get appropriate feedback through NRFX_SAADC_EVT_LIMIT in event_handler() function.

    The trouble sometimes comes when I want to read and process ADC samples. As you can see from the image above, the order of ADC samples in sampling buffer swaps only once, at the very beginning, and remains wrong for the rest of the application runtime.

    ADC sampling frequency when sample swapping occurs is equal to 1KHz. The time my application needs to read and process ADC samples is in order of 10-20us so I would not say this is an issue. The swapping occurs even when I just want to read ADC samples without any further data processing.

    When I slow down ADC sampling rate to 200Hz, let's say, the swapping of ADC samples does not happen.

    Do you have any idea what I am doing wrong?

    Thanks in advance for your time and efforts!

    Cheers!

    Bojan

    P.S. I did not find nrfx_saadc_v2.h in latest NCS v1.5.0

Reply
  • Hello, Einar.

    I hope you are doing well. There is one strange thing I noticed recently while playing with ADC.

    It can happen that the ADC samples in buffer swap the position:

    As you can see above, I am sampling only two ADC channels. My configuration is similar like I found in your unofficial SAADC example:

    - There are two sampling buffers, both of size 2 (to store one ADC sample of each channel).

    In my particular use case, I am playing with ADC limits (both HIGH and LOW). This is working fine. For both ADC channels, I was able to set the limits and to get appropriate feedback through NRFX_SAADC_EVT_LIMIT in event_handler() function.

    The trouble sometimes comes when I want to read and process ADC samples. As you can see from the image above, the order of ADC samples in sampling buffer swaps only once, at the very beginning, and remains wrong for the rest of the application runtime.

    ADC sampling frequency when sample swapping occurs is equal to 1KHz. The time my application needs to read and process ADC samples is in order of 10-20us so I would not say this is an issue. The swapping occurs even when I just want to read ADC samples without any further data processing.

    When I slow down ADC sampling rate to 200Hz, let's say, the swapping of ADC samples does not happen.

    Do you have any idea what I am doing wrong?

    Thanks in advance for your time and efforts!

    Cheers!

    Bojan

    P.S. I did not find nrfx_saadc_v2.h in latest NCS v1.5.0

Children
Related