Getting analog values from different channels using SAADC example

Hello ,

I want to read around 6 analog pins, not necessarily simultaneously. I could afford some delay in between them. i am using nrf52832 dev kit with sdk 15. How do i modify the SAADC example to achieve the same because the example only reads 1 channel. i did found this thread https://devzone.nordicsemi.com/f/nordic-q-a/5561/adc-from-various-pins/19481#19481 which looks similar but i was unable to open the GitHub link provided in the thread. Can you please direct me to the relevant examples. 

Thank you!

Parents Reply
  • Hello.

    I am fetching only 5 values in the call back event . T, T1, T2, P1, P2 . Then I just split the 16bit values into two 8 bit values . That's why : value [0-9]. 

    I see. My bad.

    Are you sure that you are using the correct pins? Note that the AIN0-AIN4 are not the same as P0.00-P0.04.

    AIN0: P0.02
    AIN1: P0.03
    AIN2: P0.04
    AIN3: P0.05
    AIN4: P0.28

    Which means that your setup is:

    Ch.0: P0.28
    Ch.1: P0.03
    Ch.2: P0.04
    Ch.3: P0.05
    Ch.4: P0.02

    A couple of mentions regarding your main loop (for(;;)):

    I see that you call nrf_drv_saadc_sample(), and then nrf_delay_ms(2000);

    nrf_drv_saadc_sample() will do one sample. Note that you need SAMPLES_IN_BUFFER samples before you get the saadc_callback() callback. This means that you will only get this callback every 10th second, while you are sending the data every 2nd second. Not a problem in itself, but you should know that you only update the values in value[0-9] every 5th nrf_drv_saadc_sample();

    Another problem may be the nrf_delay_ms(2000). I am surprised if this works well. nrf_delay_ms(n) will keep the CPU running for n ms, without doing anything, blocking the softdevice, and drawing quite a bit of current. Hence, neither the softdevice nor the current consumption are particularly found of this function.

    You can expect to see disconnects from the device you are connected to, and that this project will reset when you are connected, because the softdevice is missing time critical events.

    Instead of using nrf_delay_ms, you should use a timer with a timeout callback. There are two different approaches to this:

    1: You can use the app_timer like it is used in the SDK\examples\ble_peripheral\ble_app_hrs example. Here the timer is used to update the battery service (note that it is only emulating battery values).

    2: You can use the timer together with ppi to trigger the nrf_drv_saadc_sample() calls, like it is done in the SDK\examples\peripheral\saadc example. 

    Then you can use the saadc callback function to set a flag telling the main loop that you should send a notification. Something like this:

    #include "something.h"
    ...
    
    static volatile bool m_send_data = false;
    
    static void saadc_callback(...)
    {
        ...
        m_send_data = true;
    }
    
    static void saadc_init(...)
    {
        ...
    }
    
    static void saadc_start(...)
    {
        ...
    }
    
    int main(void)
    {
        ...
        for(;;)
        {
            if (m_send_data)
            {
                m_send_data = false;
                ble_nus_data_send(...);
            }
            idle_state_handle();
        }
    }

    Best regards,

    Edvin

Children