<?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>reading analog through UART</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/40164/reading-analog-through-uart</link><description>Hello 
 
 I just started learning nrf52832 DK and run few of the examples. I wanted to start developing my own application based on the examples. Right now, I want to read analog from A0 and print the reading in UART. I tried to combine the peripheral</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 08 Nov 2018 10:48:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/40164/reading-analog-through-uart" /><item><title>RE: reading analog through UART</title><link>https://devzone.nordicsemi.com/thread/156468?ContentTypeID=1</link><pubDate>Thu, 08 Nov 2018 10:48:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b946f5fd-dd2d-4356-acac-2c672b1b42d2</guid><dc:creator>awneil</dc:creator><description>[quote userid="74966" url="~/f/nordic-q-a/40164/reading-analog-through-uart/156464"]Yes, I finally figured it out[/quote]
&lt;p&gt;Excellent.&lt;/p&gt;
&lt;p&gt;So please Verify the answer:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/_5F00_Verify_2D00_answer_2D00_nordic.png" /&gt;&lt;/p&gt;
[quote userid="74966" url="~/f/nordic-q-a/40164/reading-analog-through-uart/156464"]the result from the ADC does not correspond to what I was expecting[/quote]
&lt;p&gt;That&amp;#39;s really a separate question - so would be better in a new thread.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reading analog through UART</title><link>https://devzone.nordicsemi.com/thread/156464?ContentTypeID=1</link><pubDate>Thu, 08 Nov 2018 10:30:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3651a20f-3fc0-45f8-b2eb-f43002859a28</guid><dc:creator>t_m</dc:creator><description>&lt;p&gt;Yes, I finally figured it out, except that the result from the ADC does not correspond to what I was expecting. I was trying to read voltage from a DC power source but the results do not much. If I am allowed , I would like to share my code here so that somebody can point me out what I am missing there.&lt;pre class="ui-code" data-mode="c_cpp"&gt;** @file
 * @defgroup uart_example_main main.c
 * @{
 * @ingroup uart_example
 * @brief UART Example Application main file.
 *
 * This file contains the source code for a sample application using UART.
 *
 */

#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;quot;app_uart.h&amp;quot;
#include &amp;quot;nrf_drv_saadc.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#if defined (UART_PRESENT)
#include &amp;quot;nrf_uart.h&amp;quot;
#endif
#if defined (UARTE_PRESENT)
#include &amp;quot;nrf_uarte.h&amp;quot;
#endif


//#define ENABLE_LOOPBACK_TEST  /**&amp;lt; if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#define MAX_TEST_DATA_BYTES     (15U)                /**&amp;lt; max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**&amp;lt; UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**&amp;lt; UART RX buffer size. */
#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
#define NRF_LOG_ERROR(...)                     NRF_LOG_INTERNAL_ERROR(__VA_ARGS__)
#define NRF_LOG_WARNING(...)                   NRF_LOG_INTERNAL_WARNING( __VA_ARGS__)
#define NRF_LOG_INFO(...)                      NRF_LOG_INTERNAL_INFO( __VA_ARGS__)
#define NRF_LOG_DEBUG(...)                     NRF_LOG_INTERNAL_DEBUG( __VA_ARGS__)
#define SAMPLES_IN_BUFFER 5
static uint32_t              m_adc_evt_counter;

void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event-&amp;gt;evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication);
    }
    else if (p_event-&amp;gt;evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event-&amp;gt;data.error_code);
    }
}




/**
 * @brief Function for main application entry.
 */
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_DONE)
    {
        ret_code_t err_code;

        err_code = nrf_drv_saadc_buffer_convert(p_event-&amp;gt;data.done.p_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);

        int i;
        NRF_LOG_INFO(&amp;quot;ADC event number: %d&amp;quot;, (int)m_adc_evt_counter);

        for (i = 0; i &amp;lt; SAMPLES_IN_BUFFER; i++)
        {
            NRF_LOG_INFO(&amp;quot;%d&amp;quot;, p_event-&amp;gt;data.done.p_buffer[i]);
        }
        m_adc_evt_counter++;
    }
}

void saadc_init(void)
{
    ret_code_t err_code;
    nrf_drv_saadc_config_t saadc_config;
    saadc_config.low_power_mode = true;
    saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
    saadc_config.oversample = NRF_SAADC_OVERSAMPLE_DISABLED;
    saadc_config.interrupt_priority = APP_IRQ_PRIORITY_LOW;
    nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(SAADC_CH_PSELP_PSELP_VDD);
    err_code = nrf_drv_saadc_init(&amp;amp;saadc_config, saadc_callback);
    APP_ERROR_CHECK(err_code);
    
    channel_config.pin_p  = NRF_SAADC_INPUT_AIN5;
  err_code = nrf_drv_saadc_channel_init(0, &amp;amp;channel_config);
    APP_ERROR_CHECK(err_code);
    
}


void uart_config_init(void)
{
    ret_code_t err_code;
    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
          NRF_UART_BAUDRATE_115200
      };

    APP_UART_FIFO_INIT(&amp;amp;comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);
}
int main(void)
{
    
    saadc_init();
    nrf_delay_ms(2);
    bsp_board_leds_init();
  uart_config_init();
    
    nrf_saadc_value_t AdcVal[1];
    
    //nrf_drv_saadc_sample_convert(1, &amp;amp;AdcVal[1]);

  printf(&amp;quot;\r\nStart: \r\n&amp;quot;);
    
    while(true)
    {
        nrf_drv_saadc_sample_convert(0, &amp;amp;AdcVal[0]);
        //int16_t voltage = 5*AdcVal[0]/4096;
        //printf(&amp;quot;Analog reading: %u\n\r&amp;quot;, voltage);
        printf(&amp;quot;Analog reading: %u\n\r&amp;quot;, AdcVal[0]);
        nrf_delay_ms(500);
    }

   /* while (true)
    
    {
        uint8_t cr;
        while (app_uart_get(&amp;amp;cr) != NRF_SUCCESS);
        while (app_uart_put(cr) != NRF_SUCCESS);

        if (cr == &amp;#39;q&amp;#39; || cr == &amp;#39;Q&amp;#39;)
        {
            printf(&amp;quot; \r\nExit!\r\n&amp;quot;);

            while (true)
            {
                // Do nothing.
            }
        }
    }*/

}


/** @} */

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When I set the power supply voltage to 5V, the analog reading I see in the UART is about 2000. This is equivalent to 2000*5/4096 = ~2.44Volts. Any idea?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reading analog through UART</title><link>https://devzone.nordicsemi.com/thread/156350?ContentTypeID=1</link><pubDate>Wed, 07 Nov 2018 14:58:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59dfb691-0ded-4f3d-8f5b-cdfa82f5bc14</guid><dc:creator>Bj&amp;#248;rn Kvaale</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/maranatha"&gt;t_m&lt;/a&gt; Have you figured out this issue or do you still need some help?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reading analog through UART</title><link>https://devzone.nordicsemi.com/thread/155963?ContentTypeID=1</link><pubDate>Mon, 05 Nov 2018 13:09:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41dff048-850a-4ebf-8cca-59b3a2877887</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;As noted, that&amp;#39;s a standard &amp;#39;C&amp;#39; thing - not specific to Nordic.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s generally easier if you learn &amp;#39;C&amp;#39; on a PC or similar &lt;em&gt;before&lt;/em&gt; moving on to the added complications of embedded microcontrollers and wireless comms.&lt;/p&gt;
&lt;p&gt;Here are some &amp;#39;C&amp;#39; learning &amp;amp; reference materials - including a free online textbook:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/"&gt;http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reading analog through UART</title><link>https://devzone.nordicsemi.com/thread/155956?ContentTypeID=1</link><pubDate>Mon, 05 Nov 2018 12:53:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:24199c7c-e843-4d9d-a43d-8ee28c4a385f</guid><dc:creator>t_m</dc:creator><description>&lt;p&gt;Hi owneil&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for the reply. I have limited experience with C. I did not know if those errors&amp;nbsp;mean that there are some missing .c files.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reading analog through UART</title><link>https://devzone.nordicsemi.com/thread/155949?ContentTypeID=1</link><pubDate>Mon, 05 Nov 2018 12:43:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ebf8691-5ddb-4701-ae80-c5ef46013f9d</guid><dc:creator>awneil</dc:creator><description>[quote userid="74966" url="~/f/nordic-q-a/40164/reading-analog-through-uart"]I just started learning nrf52832 DK[/quote]
&lt;p&gt;Do you have any prior &amp;#39;C&amp;#39; programming experience?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="74966" url="~/f/nordic-q-a/40164/reading-analog-through-uart"]Can somebody explain me what might cause the error[/quote]
&lt;p&gt;As the messages say you have &lt;em&gt;referred &lt;/em&gt;to those symbols, but have not &lt;em&gt;&lt;strong&gt;defined&lt;/strong&gt; &lt;/em&gt;them (the mentioned symbols are all function names - so &amp;quot;&lt;em&gt;referring&lt;/em&gt;&amp;quot; to them probably means that you have &lt;strong&gt;called&lt;/strong&gt; them).&lt;/p&gt;
&lt;p&gt;This is usually caused by including a header file (&lt;strong&gt;.h&lt;/strong&gt;) - but not adding the corresponding &lt;strong&gt;.c&lt;/strong&gt; file(s) to your project.&lt;/p&gt;
&lt;p&gt;This is a standard &amp;#39;C&amp;#39; thing - nothing specifically to do with Nordic:&amp;nbsp;&lt;a href="http://c-faq.com/decl/decldef.html"&gt;http://c-faq.com/decl/decldef.html&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;See also:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/39627/error-l6218e/153564#153564"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/39627/error-l6218e/153564#153564&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/13720/undefined-symbol"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/13720/undefined-symbol&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;and many more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>