# 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

• ### Estudando Projetos do SDK 10 para nRF5x com Eclipse Oxygen (Portuguese)

Posted 2017-11-12 00:08:55 by Carlos Delfino
• ### Configurando o Eclipse Oxygen para Desenvolvimento com nRF5x (Portuguese)

Posted 2017-11-10 21:15:47 by Carlos Delfino
• ### How to use Git for embedded software development

Posted 2017-11-06 13:21:55 by Yaniv Nis
• ### Thingy:52 based Weather Station

Posted 2017-10-29 22:31:15 by Krzysztof Szewczyk
• ### How to build continuous integration and delivery process for embedded SW development

Posted 2017-10-03 11:01:29 by Yaniv Nis

## Recent questions

• ### PB-ADV & PB-GATT node development using nordic mesh SDK

Posted 2017-11-18 11:29:37 by vikrant8051

Posted 2017-11-18 05:39:49 by Jeff
• ### SES 3.3 nRF52DK Blinky - basic newbie issues

Posted 2017-11-18 04:57:58 by Jeff
• ### How to map a gap event to a physical device?

Posted 2017-11-18 04:25:42 by MichaelNordicUser
• ### Show a big array using nrf_log module

Posted 2017-11-18 03:10:34 by KaiserYamate