# 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

• ### How to interface a Nordic Thingy:52 using Amazon Alexa

Posted 2018-01-24 01:05:26 by gamnes
• ### Difference between nRF52840 Engineering A and B reference designs

Posted 2018-01-15 12:27:08 by Martin Børs-Lind
• ### [For Hire] Expert development services of custom Hardware devices | IoT solutions | Mobile Apps

Posted 2018-01-15 09:08:42 by Ilya Surinsky
• ### Rust bindings to nrf52 series

Posted 2018-01-12 23:23:07 by Coleman McFarland
• ### Show And Tell: Poly - Building an RC Airplane The Hard Way

Posted 2018-01-05 01:17:57 by Daniel Veilleux

## Recent questions

• ### Can BLE 4.0 devices support Numeric comparison?

Posted 2018-01-24 00:29:20 by KV
• ### Periodic (~4s) current blip in Low Power mode NRF52832 SDK V12.3

Posted 2018-01-23 23:55:52 by koniho
• ### nrfutil, nRF Connect: Thingy52 DFU fails with message "FW version failure (Code = 5)"

Posted 2018-01-23 18:06:26 by Till
• ### nrf_sniffer_2.0.0-beta-1_51296aa/PCA10040/windows 7 not working

Posted 2018-01-23 18:03:32 by BramPeeters
• ### How send message to all from peripheral to peripheral in the multiperipheral example?

Posted 2018-01-23 17:20:07 by Renato Silva