<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to measure a state of a battery by using saadc example with NRF52832?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/50141/how-to-measure-a-state-of-a-battery-by-using-saadc-example-with-nrf52832</link><description>Hi everyone, I know that this topic has been mentioned before, but there are a lot of doubts and the situation about measuring a battery is still not clear. 
 1) First of all, I tried to handle an saadc example that is proposed by Nordic Semiconductors</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 23 Jul 2019 13:10:38 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/50141/how-to-measure-a-state-of-a-battery-by-using-saadc-example-with-nrf52832" /><item><title>RE: How to measure a state of a battery by using saadc example with NRF52832?</title><link>https://devzone.nordicsemi.com/thread/200071?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 13:10:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28098c80-c804-41f7-bb12-7c3c7c3159ae</guid><dc:creator>BelvitaSoft</dc:creator><description>&lt;p&gt;Ok guys, thank you for your support. Pin Analog A0 is defined as P02 on the development kit, although on the bottom of the board is written that pin P3 in A0.&lt;/p&gt;
&lt;p&gt;Thank you, for all answers. Big love!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to measure a state of a battery by using saadc example with NRF52832?</title><link>https://devzone.nordicsemi.com/thread/200047?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 12:08:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:83f3be4b-9861-472d-bfd9-beb8b25a5112</guid><dc:creator>Igor</dc:creator><description>&lt;p&gt;Try something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrfx_saadc.h&amp;quot;

#define INPUTS 1
#define TIMER  1000 // ms
#define AIN_BATTERY 0 

APP_TIMER_DEF(saadc_timer);

static nrf_saadc_value_t buffer[2][INPUTS];
static uint8_t battery_level = 0xFF;

static inline uint16_t adc_to_mv(nrf_saadc_value_t adc_result) {
    // http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52840.ps/saadc.html?cp=2_0_0_5_22_2#saadc_digital_output
    // Vresult[mV] = (adc_result * Vref[mV]) / (2^resolution * gain) = (adc_result * 600) / (2^10 * 1/6)
    return (adc_result * 3600) &amp;gt;&amp;gt; 10;
}

uint8_t get_battery_level() {
    return battery_level;
}

static void saadc_timeout_handler(void *p_context) {
    UNUSED_PARAMETER(p_context);
    APP_ERROR_CHECK(nrfx_saadc_sample());
}

void saadc_event_handler(nrfx_saadc_evt_t const *p_event) {
    if (p_event-&amp;gt;type != NRFX_SAADC_EVT_DONE)
        return;
    nrf_saadc_value_t *adc_result = p_event-&amp;gt;data.done.p_buffer;
    battery_level = battery_level_in_percent(adc_to_mv(adc_result[AIN_BATTERY]));
    APP_ERROR_CHECK(nrfx_saadc_buffer_convert(adc_result, INPUTS));
}

void saadc_init(void) {
    //  SAADC config &amp;amp; calibrate
    nrfx_saadc_config_t saadc_config = NRFX_SAADC_DEFAULT_CONFIG;
    APP_ERROR_CHECK(nrfx_saadc_init(&amp;amp;saadc_config, saadc_event_handler));
    APP_ERROR_CHECK(nrfx_saadc_calibrate_offset());
    while (nrfx_saadc_is_busy())
        sd_app_evt_wait();

    // Channels config
    nrf_saadc_channel_config_t battery_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
    APP_ERROR_CHECK(nrfx_saadc_channel_init(AIN_BATTERY, &amp;amp;battery_config));

    // double buffering
    APP_ERROR_CHECK(nrfx_saadc_buffer_convert(buffer[0], INPUTS));
    APP_ERROR_CHECK(nrfx_saadc_buffer_convert(buffer[1], INPUTS));

    // start SAADC timer
    APP_ERROR_CHECK(app_timer_create(&amp;amp;saadc_timer, APP_TIMER_MODE_REPEATED, saadc_timeout_handler));
    APP_ERROR_CHECK(app_timer_start(saadc_timer, APP_TIMER_TICKS(TIMER), NULL));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Use saadc_init() (only once!) to start measuring. Then use&amp;nbsp;get_battery_level() to get battery level in percents (0x64 = 100%).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to measure a state of a battery by using saadc example with NRF52832?</title><link>https://devzone.nordicsemi.com/thread/200042?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 11:58:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6718a697-04d8-4e2f-a863-449e95c4e54f</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It looks like you are confusing the analog input number with the Arduino Pin naming on top of the board. Please have a look at &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/47801/saadc-sample-convert-issue/189374#189374"&gt;this post&lt;/a&gt;, and follow the &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/pin.html?cp=3_1_0_3#pin_assign"&gt;pin assignments in the product specifications&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>