# nRF5 with input-multiplexer on ADC pin, optimization required

Hey, I am trying to sample from 10 analog sensors and stream the results via bluetooth. Since the nRF5 is on a board that only lets me access 5 Pins, I am using a 16:1 multiplexer connected to a single ADC pin and 4 digital-out pins to control the multiplexer (channel selection).

It works so far, but I am not sure if I am doing it correctly since there are so many different ways to make timing, adc,... work. Here is my code:

#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf.h"
#include <stdio.h>
#include "nordic_common.h"
#include "nrf_log.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"

// GENERATED CODE (Digital-out values for MUX channel selection)
const uint8_t PINS[] = {5,4,3,13};
const uint8_t NUM_PINS = 4;
const uint8_t NUM_CHANNELS = 10;

const uint32_t SET_MAP[] = {
0x0,
0x20,
0x10,
0x30,
0x8,
0x28,
0x18,
0x38,
0x2000,
0x2020
};

const uint32_t CLR_MAP[] = {
0x2038,
0x2018,
0x2028,
0x2008,
0x2030,
0x2010,
0x2020,
0x2000,
0x38,
0x18
};
// END GENERATED CODE

// Note: ADC_BUFFER_SIZE has to be equal to NUM_CHANNELS!

{
{
uint32_t i;
for (i = 0; i < p_event->data.done.size; i++)
{
}
}
}

{
ret_code_t ret_code;

APP_ERROR_CHECK(ret_code);

}

int main(void)
{
// GPIO:
// Configure pins as outputs.
for (int i = 0; i < NUM_PINS; i++)
{
nrf_gpio_cfg_output(PINS[i]);
}

UNUSED_RETURN_VALUE(NRF_LOG_INIT());

while (true)
{

for (int i = 0; i < NUM_CHANNELS; i++)
{
// Set pins for mux channel selection:
nrf_gpio_pins_clear(CLR_MAP[i]);
nrf_gpio_pins_set(SET_MAP[i]);

__SEV();
__WFE();
__WFE();

nrf_delay_ms(10);
}
nrf_delay_ms(1000);
}
}


It doesnt have to be perfect, but I would like to keep power consumption low and I need stable sampling at around 100hz (10 channels every 10ms). Also I will need to replace the UART with a Bluetooth stream and I need to switch from a nRF51 DK to a nRF52832 (which is a bit different about ADC/SAADC but I dont think its a huge problem).

1. Am I using the SEV(),WFE(),WFE() correctly?
2. Do I need a delay between the sampling of the 10 channels (in the code currently 10ms)
3. Will this approach still work when I add Bluetooth or are there going to be problems with Sleeping/Timing etc.?

Best regards Johannes

edit retag close delete

Sort by » oldest newest most voted

Hi,

1. Yes, this is how the examples in the SDK enters sleep mode.
2. A call to nrf_drv_adc_sample() will assert if the ADC is busy. You should rather check if the ADC is available using nrf_drv_adc_is_busy() before starting sampling.
3. I would not recommend using busy-wait in the main loop for ADC sampling. You should setup a timer (use RTC/App timer for low power), and trigger the ADC sampling in the callback handler. As you mention, the ADC peripheral on the nRF52832 is different from nRF51, and it use a different driver (nrf_drv_saadc). You can find a couple of examples using the SAADC on our GitHub. It should not be a problem to get the sample rate you desire along BLE communication.

Best regards,

Jørgen

more

[hide preview]

## Recent blog posts

• ### 6 Things to Know about Bluetooth Beacons

Posted 2017-09-22 08:27:00 by Rose Martin
• ### Creating a Keil project for a Bluetooth Mesh example

Posted 2017-09-19 12:08:11 by Kristian Skordal
• ### Using millis() like in Arduino.

Posted 2017-09-18 11:58:04 by schef
• ### Flashing nRF5x firmware using any dumb terminal program via standard serial COM port

Posted 2017-09-18 03:53:40 by Nguyen Hoan Hoang
• ### How to Debug the nRF52 Interrupts — Useful Tips

Posted 2017-09-13 15:15:22 by Yaniv Nis

## Recent questions

• ### Device crashes when debug flags are turned on

Posted 2017-09-24 04:09:33 by tyler
• ### nrf52832 of UART problem

Posted 2017-09-24 04:05:27 by lenli
• ### RTT Stability

Posted 2017-09-24 03:28:49 by Yatekii
• ### HardFault with GPIOTE & SD

Posted 2017-09-24 03:18:09 by Yatekii
• ### Adding a service to existing SDK example code

Posted 2017-09-24 02:45:24 by alikob