Hi,
Does nRF52832 use normal ADC input to detect battery level (ex. AIN0, AIN1...)? or nRF52832 have other method to do this job?
Thank you,
Chianglin
Hi,
Does nRF52832 use normal ADC input to detect battery level (ex. AIN0, AIN1...)? or nRF52832 have other method to do this job?
Thank you,
Chianglin
Hi,
The SAADC on the nRF52 has a direct connection to VDD, so you do not need to use any of the external AIN pins if the battery is directly connected to VDD on the nRF. This is selected by setting the PSEL register to VDD.
Note that measuring the battery voltage like this has a few disadvantages. For instance, the measurement operation will affect the measurement, since it is part of the circuit. You can minimize that effect by following these guidelines, but that requires you do use an external pin (AIN0,...) to do it. Even his approach gives quite course results though, so if you need very accurate battery data, the best option may be to use a dedicated battery gauge IC.
Hi,
If I don't need get a accurate battery level, then I can direct connect battery with Vcc pin. Does it correct?
How to set PSEL register?
Would you please give me a sample code?
Thank you
Hi,
Please refer to the Proximity Application example in SDK 15.3. There you can see how the SAADC driver is configured for VDD measurement in line 283-297 of <SDK15.3>\examples\ble_peripheral\ble_app_proximity\main.c. You can refer to the other ADC related code in the file to see a complete example of battery measurement (and even how to use the standard Bluetooth battery service if that is relevant).
Hi,
Thank you for your explain.
VDD is define in "H7" and "A7" pin of nRF52832, it is used as power input of MCU. Does it also can detect battery level?
Thank you,
chianglin said:VDD is define in "H7" and "A7" pin of nRF52832, it is used as power input of MCU. Does it also can detect battery level?
Yes, but only if you have connected VDD directly to the battery, as mentionned in my initial reply.
Hi,
If I want to use two ADC at the same time, one for "ADC Sensor input", and the other one for "Battery level detect". How can I modify following source code?
void saadc_init(void) { ret_code_t err_code; // ---------- Initialize ADC for Pressure-Sensor ---------------- // 變更 ADC 的 bit 數 nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG; saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT; nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); // 變更 ADC 倍率 & 參考電壓 channel_config.gain = NRF_SAADC_GAIN1_4; channel_config.reference = NRF_SAADC_REFERENCE_VDD4; err_code = nrf_drv_saadc_init(&saadc_config, saadc_Sensor_callback); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(0, &channel_config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); // ---------- Initialize ADC for Battery detect ---------------- #if 1 err_code = nrf_drv_saadc_init(NULL, saadc_Battery_event_handler); APP_ERROR_CHECK(err_code); nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD); err_code = nrf_drv_saadc_channel_init(0, &config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[0], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[1], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); #endif }
System will crash when "second nrf_drv_saadc_init() execute".
Thank you
Hi,
chianglin said:System will crash when "second nrf_drv_saadc_init() execute".
That is expected. You should only initialize the driver once unless you disable it in between (for instance to save power). So you should remove the second call to nrf_drv_saadc_init(), and use a common event handler for both. If you sample either one or the other, you will know which it is based on what you just sampled. If you sample both at the same time, you will know based on the order. (sampling both at the same time may not be that useful since you probably don't need battery information as often as you need sensor input).
chianglin said:If I want to use two ADC at the same time, one for "ADC Sensor input", and the other one for "Battery level detect". How can I modify following source code?
To sample both channels in consecutive order, just enable the second channel as well, and make sure the result buffer is a multiple of the number of channels. If you want to sample either one or the other, and both at a low rate, then it is most power-efficient to configure sampling of the first channel (sensor), do that, and then uninitialized the SAADC. Then do the same when it is time to sample the other channel (VDD).
Hi,
chianglin said:System will crash when "second nrf_drv_saadc_init() execute".
That is expected. You should only initialize the driver once unless you disable it in between (for instance to save power). So you should remove the second call to nrf_drv_saadc_init(), and use a common event handler for both. If you sample either one or the other, you will know which it is based on what you just sampled. If you sample both at the same time, you will know based on the order. (sampling both at the same time may not be that useful since you probably don't need battery information as often as you need sensor input).
chianglin said:If I want to use two ADC at the same time, one for "ADC Sensor input", and the other one for "Battery level detect". How can I modify following source code?
To sample both channels in consecutive order, just enable the second channel as well, and make sure the result buffer is a multiple of the number of channels. If you want to sample either one or the other, and both at a low rate, then it is most power-efficient to configure sampling of the first channel (sensor), do that, and then uninitialized the SAADC. Then do the same when it is time to sample the other channel (VDD).
Hi,
According to your instructions, does follow source code is correct?
void saadc_init(void) { ret_code_t err_code; // Modify ADC resolution nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG; saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT; // Initialize ADC err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback); APP_ERROR_CHECK(err_code); // ---------- Initialize ADC for Pressure-Sensor ---------------- nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); channel_config.gain = NRF_SAADC_GAIN1_4; channel_config.reference = NRF_SAADC_REFERENCE_VDD4; err_code = nrf_drv_saadc_channel_init(0, &channel_config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); // ---------- Initialize ADC for Battery detect ---------------- nrf_saadc_channel_config_t config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD); err_code = nrf_drv_saadc_channel_init(1, &config); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[0], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_battery_adc_buf[1], SAADC_SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); }
How can I modify saadc_callback() function to read two different ADC channel?
Would you please give me a “saadc_callback()" sample code?
Thank you
Hi,
Yes, I can try to put together a simple example. But then it would be good if you can clarify a few things first:
Hi,
Thank you for your support.
About your question.
1. As your description of previous mail, the sampling rate of battery don't need very fast, but 糸he sampling frequency of the sensor is as fast as possible.
2. Yes, one/both should be sampled regularly.
Thank you again.
Hi,
If you need a consistent sampling frequency for the sensor, then the simplest is to sample the battery also every time. This is demonstrated by this minimal modification to the SDK 15.3 SAADC example:
/** * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into a Nordic * Semiconductor ASA integrated circuit in a product or a software update for * such product, must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * 3. Neither the name of Nordic Semiconductor ASA nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * 4. This software, with or without modification, must only be used with a * Nordic Semiconductor ASA integrated circuit. * * 5. Any software provided in binary form under this license must not be reverse * engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ /** @file * @defgroup nrf_adc_example main.c * @{ * @ingroup nrf_adc_example * @brief ADC Example Application main file. * * This file contains the source code for a sample application using ADC. * * @image html example_board_setup_a.jpg "Use board setup A for this example." */ #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> #include "nrf.h" #include "nrf_drv_saadc.h" #include "nrf_drv_ppi.h" #include "nrf_drv_timer.h" #include "boards.h" #include "app_error.h" #include "nrf_delay.h" #include "app_util_platform.h" #include "nrf_pwr_mgmt.h" #include "nrf_log.h" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" #define SAMPLES_IN_BUFFER 2 // Number of channels in this case volatile uint8_t state = 1; static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0); static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER]; static nrf_ppi_channel_t m_ppi_channel; static uint32_t m_adc_evt_counter; void timer_handler(nrf_timer_event_t event_type, void * p_context) { } void saadc_sampling_event_init(void) { ret_code_t err_code; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32; err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler); APP_ERROR_CHECK(err_code); /* setup m_timer for compare event every 400ms */ uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 400); nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false); nrf_drv_timer_enable(&m_timer); uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0); uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get(); /* setup ppi channel so that timer compare event is triggering sample task in SAADC */ err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_task_addr); APP_ERROR_CHECK(err_code); } void saadc_sampling_event_enable(void) { ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel); APP_ERROR_CHECK(err_code); } void saadc_callback(nrf_drv_saadc_evt_t const * p_event) { if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { ret_code_t err_code; err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); int i; NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter); for (i = 0; i < SAMPLES_IN_BUFFER; i++) { if (i == 0) { NRF_LOG_INFO("Sensor reading: %d", p_event->data.done.p_buffer[i]); } else if (i == 1) { NRF_LOG_INFO("Battery reading: %d", p_event->data.done.p_buffer[i]); } else { NRF_LOG_ERROR("Use only two samples per buffer with this setup."); } } m_adc_evt_counter++; } } void saadc_init(void) { ret_code_t err_code; // Init SAADC driver err_code = nrf_drv_saadc_init(NULL, saadc_callback); APP_ERROR_CHECK(err_code); // Init sensor channel nrf_saadc_channel_config_t channel_config_sensor = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); err_code = nrf_drv_saadc_channel_init(0, &channel_config_sensor); APP_ERROR_CHECK(err_code); // Init battery channel nrf_saadc_channel_config_t channel_config_battery = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD); err_code = nrf_drv_saadc_channel_init(1, &channel_config_battery); APP_ERROR_CHECK(err_code); // Start conversion err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); } /** * @brief Function for main application entry. */ int main(void) { uint32_t err_code = NRF_LOG_INIT(NULL); APP_ERROR_CHECK(err_code); NRF_LOG_DEFAULT_BACKENDS_INIT(); ret_code_t ret_code = nrf_pwr_mgmt_init(); APP_ERROR_CHECK(ret_code); saadc_init(); saadc_sampling_event_init(); saadc_sampling_event_enable(); NRF_LOG_INFO("SAADC HAL simple example started."); while (1) { nrf_pwr_mgmt_run(); NRF_LOG_FLUSH(); } } /** @} */
Running the application () gives you this log output:
<info> app: SAADC HAL simple example started. <info> app: ADC event number: 0 <info> app: Sensor reading: 6 <info> app: Battery reading: 626 <info> app: ADC event number: 1 <info> app: Sensor reading: 19 <info> app: Battery reading: 626 <info> app: ADC event number: 2 <info> app: Sensor reading: 22 <info> app: Battery reading: 627 <info> app: ADC event number: 3 ...
Note that you can increase the sampling frequency by adjusting line 97, where you can also replace nrf_drv_timer_ms_to_ticks() with nrf_drv_timer_us_to_ticks() if you need to provide the sampling interval in microseconds.
Alternatively, you can sample only the sensor most of the time, then us an app timer to reconfigure the saadc to sample the battery once in a while, before reverting to sampling only the sensor. This will probably be a more power-efficient way (at least if you don't sample the battery often), but will potentially give you a short pause in the sensor sampling which may or may not be acceptable.