<?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>ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/80796/adc---first-read-is-always-wrong</link><description>Hi, 
 
 I have been playing around with the nRF9160 DK trying to get it reading a range of sensors, so far with success, however I have found an issue which has stumped me. I am using ADC0 to read a voltage. Although this is intended for a pressure transducer</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 09 Mar 2022 12:50:49 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/80796/adc---first-read-is-always-wrong" /><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/357156?ContentTypeID=1</link><pubDate>Wed, 09 Mar 2022 12:50:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2cb7270b-7eca-4dc3-9ba5-d145044f71b3</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;I am happy to hear that, Tero!&lt;br /&gt;&lt;br /&gt;Please do not hesitate to open another ticket if you should encounter any other issues or questions in the future.&lt;br /&gt;&lt;br /&gt;Good luck with your development!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/357148?ContentTypeID=1</link><pubDate>Wed, 09 Mar 2022 12:27:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4df69621-f9af-477e-b717-4af2538da5fd</guid><dc:creator>anicare-tero</dc:creator><description>&lt;p&gt;Hi Karl,&lt;/p&gt;
&lt;p&gt;Thanks for helping, I&amp;#39;m fine now without the offset calibration.&lt;/p&gt;
&lt;p&gt;BR,&lt;br /&gt;Tero&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/357139?ContentTypeID=1</link><pubDate>Wed, 09 Mar 2022 12:08:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3817d895-e1a1-4547-bb91-00fa1dcb330c</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again, Tero and Damien&lt;br /&gt;&lt;br /&gt;I just wanted to let you know that I have been unable to reproduce the unexpected sample / buffer shift behavior without the offset calibration present, which aligns with my previous conclusion that this is an artifact of the CALIBRATEOFFSET description in the nRF9160&amp;#39;s Product Specification.&lt;br /&gt;&lt;br /&gt;Tero, If you are still seeing unexplained samples appear in your buffer, please open a separate ticket for this issue so that we may investigate that separately.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/348560?ContentTypeID=1</link><pubDate>Wed, 19 Jan 2022 15:17:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:07f4a73e-b0a5-46a3-bda7-2f885047d5cd</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again,&lt;br /&gt;&lt;br /&gt;Thank you for elaborating and providing the snippet, Damien.&lt;br /&gt;I will attempt to replicate the behavior on my end using the provided code and NCS version.&lt;/p&gt;
[quote user="ziirou"]I just noticed that when I have&amp;nbsp;&lt;strong&gt;CONFIG_LOG_MODE_MINIMAL enabled&lt;/strong&gt;, ADC readings are giving incorrect values. Usually I test our devices with minimal logging &lt;strong&gt;disabled &lt;/strong&gt;(CONFIG_LOG_MODE_IMMEDIATE enabled) and there it is working properly.[/quote]
&lt;p&gt;If the loggings are processed in-place/immediately it could generate a delay akin the one mentioned by Damien. However, I find it strange that my minimal example did not behave the same way when I tried to replicate this earlier. In my minimal application, the calibration was the only source of incorrect / unexpected samples - without any delay or timer interference, and it only ever happened directly following the calibration, unlike what Damien describes.&lt;/p&gt;
[quote user="ziirou"]&lt;p&gt;I removed the &lt;strong&gt;CALIBRATEOFFSET&lt;/strong&gt; calling and now it is working with minimal logging &lt;strong&gt;enabled&lt;/strong&gt; too.&lt;/p&gt;
&lt;p&gt;Have you (&lt;a href="https://devzone.nordicsemi.com/members/damol"&gt;DamoL&lt;/a&gt; and &lt;a href="https://devzone.nordicsemi.com/members/karl-ylvisaker"&gt;Karl Ylvisaker&lt;/a&gt;) tested the ADC without any calibration there?&lt;/p&gt;[/quote]
&lt;p&gt;I did try without calibration in my minimal example as well, in which case I never saw any incorrect / unexpected samples being placed in the buffer, which is what lead me to the conclusion that this was &amp;#39;as expected&amp;#39; when the calibration was initiated without the SAADC first being stopped, as (poorly) described in the Product Specification.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/348282?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 12:47:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f32ec367-e090-411d-818c-9ab636796424</guid><dc:creator>anicare-tero</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I just noticed that when I have&amp;nbsp;&lt;strong&gt;CONFIG_LOG_MODE_MINIMAL enabled&lt;/strong&gt;, ADC readings are giving incorrect values. Usually I test our devices with minimal logging &lt;strong&gt;disabled &lt;/strong&gt;(CONFIG_LOG_MODE_IMMEDIATE enabled) and there it is working properly.&lt;/p&gt;
&lt;p&gt;I removed the &lt;strong&gt;CALIBRATEOFFSET&lt;/strong&gt; calling and now it is working with minimal logging &lt;strong&gt;enabled&lt;/strong&gt; too.&lt;/p&gt;
&lt;p&gt;Have you (&lt;a href="https://devzone.nordicsemi.com/members/damol"&gt;DamoL&lt;/a&gt; and &lt;a href="https://devzone.nordicsemi.com/members/karl-ylvisaker"&gt;Karl Ylvisaker&lt;/a&gt;) tested the ADC without any calibration there?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Tero&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/348036?ContentTypeID=1</link><pubDate>Mon, 17 Jan 2022 12:45:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d166fec-bfbb-4316-bdaf-3745adce28af</guid><dc:creator>DamoL</dc:creator><description>&lt;p&gt;Hi Karl,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for the update.&amp;nbsp;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80796/adc---first-read-is-always-wrong/347440#347440"]Damien, could you confirm whether you meant that the unexpected sample happens periodically without the added kprint delay, or only during startup of the SAADC peripheral?[/quote]
&lt;p&gt;When I have the printk() it only happened the first time I tried reading. Without the printk() it happened fairly often. I don&amp;#39;t have a copy of the original code as I was just playing around with it and trying out the libraries, etc. But it was essentially exactly what was in the original question of this post, with added printk() before reading the buffer.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;main.c would look a bit like this;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;drivers/adc.h&amp;gt;
#include &amp;lt;hal/nrf_saadc.h&amp;gt;

#define ADC_RESOLUTION 12
#define ADC_GAIN ADC_GAIN_1_4
#define ADC_REFERENCE ADC_REF_VDD_1_4
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)
#define ADC_1ST_CHANNEL_ID 0  
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0

#define BUFFER_SIZE 1
/* ADC Struct */
static struct device *adc_dev;

static uint16_t m_sample_buffer[BUFFER_SIZE];

static struct adc_channel_cfg channel_cfg = {
	.gain 				= ADC_GAIN,
	.reference 			= ADC_REFERENCE,
	.acquisition_time 	= ADC_ACQUISITION_TIME,
	.differential 		= 0,
	.channel_id			= ADC_1ST_CHANNEL_INPUT ,
};

static int adc_sample(void)
{
	int ret;

		struct adc_sequence sequence = {
		.channels    = BIT(channel_cfg.channel_id),
		.buffer      = &amp;amp;m_sample_buffer,
		.buffer_size = sizeof(m_sample_buffer),
		.resolution  = ADC_RESOLUTION,
	};

	if (!adc_dev) {
		return -1;
	}

	ret = adc_read(adc_dev, &amp;amp;sequence);
	if (ret) {
        printk(&amp;quot;adc_read() failed with code %d\n&amp;quot;, ret);
	}

	for (int i = 0; i &amp;lt; BUFFER_SIZE; i++) {
                printk(&amp;quot;ADC raw value: %d\n&amp;quot;, m_sample_buffer[i]);
				uint32_t mV = (m_sample_buffer[i] *1000)*3/4096;	//n*1000*Reference (3V) / 12bit adc
				printk(&amp;quot;ADC mV after Divider: %u\n&amp;quot;, mV);
				mV = mV *2;
				printk(&amp;quot;Voltage Sensed: %u\n&amp;quot;, mV);
	}

	return ret;
}

int adc_value=0;

static void button_handler(uint32_t button_states, uint32_t has_changed)
{	
	if (has_changed &amp;amp; button_states &amp;amp;
		BIT(0)) {
		    adc_value = adc_sample();
		    printk(&amp;quot;Value - %i&amp;quot;, adc_value);
	}
}

void main(void)
{
	int err=0;
    adc_dev = device_get_binding(&amp;quot;ADC_0&amp;quot;);
	if (!adc_dev) {
        printk(&amp;quot;device_get_binding ADC_0 failed\n&amp;quot;);
    } 
    NRF_SAADC_NS-&amp;gt;TASKS_CALIBRATEOFFSET=1;
    err = adc_channel_setup(adc_dev, &amp;amp;channel_cfg);
    if (err) {
	    printk(&amp;quot;Error in adc setup: %d\n&amp;quot;, err);
	}
	
#if defined(CONFIG_DK_LIBRARY)
	dk_buttons_init(button_handler);
#endif
	
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;As for the versio, it would have been V1.6.1 .&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Damien&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/347440?ContentTypeID=1</link><pubDate>Wed, 12 Jan 2022 16:10:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6efb54ed-94b5-4c12-bbdb-8294b48e1d74</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again,&amp;nbsp;Tero and Damien&lt;br /&gt;&lt;br /&gt;It seems that the incorrect sample does indeed stem from the calibrateoffset procedure - at least in my minimal NCS ADC test application.&lt;br /&gt;&lt;br /&gt;Damien, could you confirm whether you meant that the unexpected sample happens periodically without the added kprint delay, or only during startup of the SAADC peripheral? If it is periodically, could you possibly send me the stripped down version of the project (only adc parts needed) that you tested with along with which NCS version you used when you saw this behavior?&lt;br /&gt;&lt;br /&gt;Tero, does the previous discussion not explain the similar issue you saw/are seeing?&lt;br /&gt;If your issue now diverges from this issue, please open a separate ticket where you detail your issue.&lt;br /&gt;&lt;br /&gt;In any case, I thought I should let you both know that I have made an internal ticket with the SAADC driver developers to have the adc_nrfx_saadc driver&amp;#39;s implementation of the errata 86 workaround reviewed, and to have it re-implemented to instead exactly match the workaround described in the errata 86 documentation.&lt;br /&gt;&lt;br /&gt;I have also opened an internal request to have this elaborated on and clarified in the nRF9160&amp;#39;s SAADC Product Specification section, akin to that of the nRF5340&amp;#39;s SAADC section or better.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/347232?ContentTypeID=1</link><pubDate>Tue, 11 Jan 2022 15:39:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1aa365f1-30ce-47bf-81c7-12b99b0767fc</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again Tero and Damien,&lt;br /&gt;&lt;br /&gt;Thank you for your extreme patience with this issue. I have been out of office for some time, but now I am back in office and will resume work with this investigation.&lt;br /&gt;&lt;br /&gt;A colleague has conducted some tests on this in my absence and come to the conclusion that the incorrect sample is as expected as per the 9160&amp;#39;s SAADC documentation, which reads:&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;em&gt;&lt;span&gt;The ADC has a temperature dependent offset. If the ADC is to operate over a large temperature range, we recommend running CALIBRATEOFFSET at regular intervals. The CALIBRATEDONE event will be fired when the calibration has been completed. Note that the DONE and RESULTDONE events will also be generated.&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;em&gt;&lt;span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Which implies that a junk DONE and RESULTDONE event will be returned following a successful calibration. I will have to test this some more, to see if this might have been what is causing the behavior you have seen on your end as well. I have also noted that the implementation of the Errata 86 workaround in the adc_nrfx_saadc.c driver does not exactly follow the workaround detailed in the mentioned Errata 86 (namely, it does not wait for the appropriate events to be generated), which I am unclear on whether has played into this issue.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/344305?ContentTypeID=1</link><pubDate>Mon, 20 Dec 2021 10:21:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ceb6b03-afa6-4a48-a40a-77dc9173e708</guid><dc:creator>anicare-tero</dc:creator><description>&lt;p&gt;Hi &lt;a href="https://devzone.nordicsemi.com/members/karl-ylvisaker"&gt;Karl Ylvisaker&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;Do you have any updates on this?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Tero&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/337289?ContentTypeID=1</link><pubDate>Wed, 03 Nov 2021 13:50:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:973f0e3c-6aec-4c94-9b67-a7fe5cb0b9d0</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again, Damien&lt;/p&gt;
[quote user="DamoL"]Apologies I have been away for a few days.[/quote]
&lt;p&gt;No need to apologize - we will continue this whenever you have the chance.&lt;/p&gt;
[quote user="DamoL"]I have tried your suggestion. But do not see a change in behaviour. My current workaround is just using printk() between adc_read() and looking at the buffer.&amp;nbsp;[/quote][quote user="DamoL"]For some reason this gives enough time for the buffer to be filled. If I comment out that line it reads correctly about 30% of the time.[/quote]
&lt;p&gt;Thank you for trying this, and updating us on the results. I find this very strange. I will need to look deeper into this, and once I have done so I will escalate it to the SAADC driver&amp;#39;s developer team so that they may examine my findings and patch it.&amp;nbsp;I will update you&amp;nbsp;as soon as I have got anything to share.&lt;br /&gt;&lt;br /&gt;Thank you for bringing this to our attention!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/337028?ContentTypeID=1</link><pubDate>Tue, 02 Nov 2021 09:58:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e7f29c5-8280-44a0-ae10-b97f3391c773</guid><dc:creator>DamoL</dc:creator><description>&lt;p&gt;Hi Karl,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Apologies I have been away for a few days. I have tried your suggestion. But do not see a change in behaviour. My current workaround is just using printk() between adc_read() and looking at the buffer.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre class="ui-code" data-mode="text"&gt;ret = adc_read(dev, &amp;amp;sequence);
	if (ret) {
        printk(&amp;quot;adc_read() failed with code %d\n&amp;quot;, ret);
	}
	else{

		for (int i = 0; i &amp;lt; BUFFER_SIZE; i++) {
					printk(&amp;quot;Reading ADC\n&amp;quot;);    //THIS ADDS SUFFICIENT DELAY
					uint32_t mV = (m_sample_buffer[i] *1000)*3/4096;	//n*1000*Reference (3V) / 12bit adc
					printk(&amp;quot;ADC raw value: %d\n&amp;quot;, m_sample_buffer[i]);
					printk(&amp;quot;ADC mV after Divider: %u\n&amp;quot;, mV);
					mV = mV *2;
					printk(&amp;quot;Voltage Sensed: %u\n&amp;quot;, mV);
		}
	}&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;For some reason this gives enough time for the buffer to be filled. If I comment out that line it reads correctly about 30% of the time.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Thanks,&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Damien&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/336435?ContentTypeID=1</link><pubDate>Thu, 28 Oct 2021 11:26:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c48ee2e7-5559-4cdf-8fb9-def2502fe6ce</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again Damien and Tero,&lt;br /&gt;&lt;br /&gt;As mentioned I previously had the nRF5 SDK in mind when discussing this issue earlier. Looking into the NCS&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/main/drivers/adc/adc_nrfx_saadc.c#L378-L380"&gt;SAADC&amp;#39;s irq handler it is apparent that the CALIBRATEDONE event is indeed cleared before the main context ever sees it&lt;/a&gt;. While this explains how the application is getting stuck waiting for the event, it does not unambiguously explain why you saw a change in the behavior after triggering the stop tasks before and after the calibrate done triggering.&lt;br /&gt;&lt;br /&gt;Could you try to add a delay in between your call to trigger calibration and the first sampling, to see if this then gives the SAADC sufficient time to finish calibration and the STOP task, before it is started again?&lt;br /&gt;If it is the Errata 86 at play, the driver&amp;#39;s implementation of the workaround should take care of it as long as it is given time to finish before the first call.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/336269?ContentTypeID=1</link><pubDate>Wed, 27 Oct 2021 14:47:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c236d34-f8ca-4445-bda6-944468d95c85</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again Damien and Tero,&lt;br /&gt;&lt;br /&gt;Thank you for your patience with this.&lt;/p&gt;
[quote user="ziirou"]We have had the similar problem like this one. I tried what&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/members/karl-ylvisaker"&gt;Karl Ylvisaker&lt;/a&gt;&amp;nbsp;suggested and it gave some errors on missing arguments. So I did it like this:[/quote]
&lt;p&gt;Yes, I wrote the section with the nRF5 SDK in mind, my mistake.&lt;/p&gt;
[quote user="ziirou"]Earlier I had it like below and it was working but I&amp;#39;m not sure if I did it at the right way.[/quote]
&lt;p&gt;I am not sure that this will work, since the program counter will move on immediately after having triggered the task to cleared the event - regardless of the event actually having happened.&lt;/p&gt;
[quote user="DamoL"]As Tero said, it does get stuck on the NRF_SAADC_EVENT_CALIBRATEDONE line. I tried what Tero had previously and that didn&amp;#39;t work for me either.&amp;nbsp;[/quote]
&lt;p&gt;I just created a minimal NCS ADC example to test this on my end as well, and I notice that the same thing - the CALIBRATEDONE event never seems to occur. I am not immediately sure why this is happening - perhaps the calibrate done event is cleared somewhere else with a higher priority, so that the section here never sees the event, or similar.&lt;br /&gt;&lt;br /&gt;I will have a look into what is happening behind the scenes here tomorrow.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/335761?ContentTypeID=1</link><pubDate>Mon, 25 Oct 2021 13:16:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7f935eb-67ba-4bd0-837a-f975ff105a19</guid><dc:creator>DamoL</dc:creator><description>&lt;p&gt;Hi Karl and Tero.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As Tero said, it does get stuck on the NRF_SAADC_EVENT_CALIBRATEDONE line. I tried what Tero had previously and that didn&amp;#39;t work for me either.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Damien&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/335645?ContentTypeID=1</link><pubDate>Mon, 25 Oct 2021 07:47:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e81f4e21-7b0b-4d02-9a61-04b0d68d4d33</guid><dc:creator>anicare-tero</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;We have had the similar problem like this one. I tried what&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/members/karl-ylvisaker"&gt;Karl Ylvisaker&lt;/a&gt;&amp;nbsp;suggested and it gave some errors on missing arguments. So I did it like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;...
	LOG_INF(&amp;quot;nrf_saadc_task_trigger NRF_SAADC_TASK_STOP&amp;quot;);
	nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
	LOG_INF(&amp;quot;nrf_saadc_event_check NRF_SAADC_EVENT_STOPPED&amp;quot;);
	while(!nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_STOPPED));
	LOG_INF(&amp;quot;nrf_saadc_task_trigger NRF_SAADC_TASK_CALIBRATEOFFSET&amp;quot;);
	nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_CALIBRATEOFFSET);
	LOG_INF(&amp;quot;nrf_saadc_event_check NRF_SAADC_EVENT_CALIBRATEDONE&amp;quot;);
	while(!nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE));
	LOG_INF(&amp;quot;nrf_saadc_task_trigger NRF_SAADC_TASK_STOP&amp;quot;);
	nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
	LOG_INF(&amp;quot;nrf_saadc_event_check NRF_SAADC_EVENT_STOPPED&amp;quot;);
	while(!nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_STOPPED));
	LOG_INF(&amp;quot;done&amp;quot;);
...&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And it&amp;#39;s getting stuck on&amp;nbsp;NRF_SAADC_EVENT_CALIBRATEDONE.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[00:00:01.773,071] [1B][0m&amp;lt;inf&amp;gt; adc_control: nrf_saadc_task_trigger NRF_SAADC_TASK_STOP[1B][0m
[00:00:01.781,768] [1B][0m&amp;lt;inf&amp;gt; adc_control: nrf_saadc_event_check NRF_SAADC_EVENT_STOPPED[1B][0m
[00:00:01.790,740] [1B][0m&amp;lt;inf&amp;gt; adc_control: nrf_saadc_task_trigger NRF_SAADC_TASK_CALIBRATEOFFSET[1B][0m
[00:00:01.800,506] [1B][0m&amp;lt;inf&amp;gt; adc_control: nrf_saadc_event_check NRF_SAADC_EVENT_CALIBRATEDONE[1B][0m&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Earlier I had it like below and it was working but I&amp;#39;m not sure if I did it at the right way.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;...
	nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_CALIBRATEOFFSET);
	nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
	nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
	nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_STOPPED);
	nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
...&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I hope we could solve this issue.&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Tero&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/335598?ContentTypeID=1</link><pubDate>Sun, 24 Oct 2021 18:22:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ad0d569f-c1f0-426a-9138-cbec83565a87</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again, Damien&lt;/p&gt;
[quote user="DamoL"]Thanks for the Reply.[/quote]
&lt;p&gt;No problem at all, I am happy to help!&lt;br /&gt;Thank you for clarifying with the diagram and updating us on your findings.&lt;/p&gt;
[quote user="DamoL"]I followed the Errata 86 document, and added the below into my code on setup.&amp;nbsp;[/quote]
&lt;p&gt;This is almost correct, except you should not write to the events but instead wait for them to be generated as a result of the triggered task.&lt;br /&gt;So, you should only write to the TASKS registers to trigger them, and not write to the EVENTS registers but instead wait for them to be set on their own as a result of the triggered TASK finishing. You should also add a STOP task trigger before the calibrate offset task, as shown in the workaround.&lt;br /&gt;The result could look similar to this:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;..
    NRF_SAADC_NS-&amp;gt;TASKS_STOP = 1;
    while(!nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED));
    NRF_SAADC_NS-&amp;gt;TASKS_CALIBRATEOFFSET=1;
    while(!nrf_saadc_event_check(EVENTS_CALIBRATEDONE));
	NRF_SAADC_NS-&amp;gt;TASKS_STOP=1;
	while(!nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED));
..&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And then you can proceed to start the SAADC and its regular operation afterwards.&lt;br /&gt;You may of course replace the busy waits with something more productive, or have the CPU go into a SYSTEM_ON sleep / idling to reduce power, if you intend on calibrating the SAADC somewhat frequently.&lt;br /&gt;&lt;br /&gt;Could you try it with this change, and see if the non-debug mode works as when stepping through the code?&lt;br /&gt;I look forward to hearing what you observe with these changes implemented!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/335212?ContentTypeID=1</link><pubDate>Thu, 21 Oct 2021 07:59:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7589e582-0434-4ab0-87a5-fd0a0d60424f</guid><dc:creator>DamoL</dc:creator><description>&lt;p&gt;Hello Karl,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I followed the Errata 86 document, and added the below into my code on setup.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;if (!adc_dev) {
        printk(&amp;quot;device_get_binding ADC_0 failed\n&amp;quot;);
    }
    NRF_SAADC_NS-&amp;gt;TASKS_CALIBRATEOFFSET=1;
	NRF_SAADC_NS-&amp;gt;EVENTS_CALIBRATEDONE=1;
	NRF_SAADC_NS-&amp;gt;TASKS_STOP=1;
	NRF_SAADC_NS-&amp;gt;EVENTS_STOPPED=1;
	NRF_SAADC_NS-&amp;gt;TASKS_START=1;
    err = adc_channel_setup(adc_dev, &amp;amp;channel_cfg);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure if this is exactly what I should have done, but it almost worked.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When I step through the code, it works fine, the output on the COM port is:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ADC raw value: 3588&lt;br /&gt;ADC mV after Divider: 2626&lt;br /&gt;mV Sensed: 5252&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;However, when I just run it not in debug mode, the output is this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ADC raw value: 0&lt;/em&gt;&lt;br /&gt;&lt;em&gt;ADC mV after Divider: 2627&lt;/em&gt;&lt;br /&gt;&lt;em&gt;mV&amp;nbsp;Sensed: 5254&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So for some reason, it&amp;#39;s printing the raw value as 0, but it seems to still be converting what it should have read correctly.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I will play around a little more, but I think it&amp;#39;s pretty much there.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Damien&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/335125?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 13:36:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d7beb3e3-8644-40b0-9b5e-79908bbd24fb</guid><dc:creator>DamoL</dc:creator><description>&lt;p&gt;Hi Karl&lt;/p&gt;
&lt;p&gt;Thanks for the Reply. See diagram below.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/7723.ADC.png" /&gt;&lt;/p&gt;
&lt;p&gt;As for whether I am waiting till&amp;nbsp; offset calibration is finished, how is it possible to tell? I am allowing the program to run and then press button 1 to perform an ADC measurement. If I allow 30 seconds to pass before pressing the button, I get a wrong reading. If I allow 2 seconds and press the button twice, the second reading will be correct. So I assume it cant be offset calibration not having enough time to finish.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I will have a look at the errata - thanks for pointing that out.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Damien&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/335116?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 13:10:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f394e10d-c31c-4243-8cff-faa165956ec9</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello Damien and Dan,&lt;br /&gt;&lt;br /&gt;I concur with Dan that this definitely could be the case depending on your surrounding circuitry.&lt;br /&gt;Would it be possible for you to share with us the diagram for your connection of the chosen AIN pin to the measured voltage, and the circuitry surrounding the voltage being measured?&lt;br /&gt;You description is clear, but it would be neat to see the drawing to rule out any potential for misunderstandings there.&lt;br /&gt;&lt;br /&gt;I am not immediately seeing anything wrong in the code you&amp;#39;ve supplied.&lt;br /&gt;Could you confirm whether you&amp;#39;re waiting until the offset calibration has finished before you attempt to perform a sampling?&lt;br /&gt;&lt;br /&gt;I also know that there has been &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Ferrata_nRF52832_Rev3%2FERR%2FnRF52832%2FRev3%2Flatest%2Fanomaly_832_86.html&amp;amp;cp=4_2_1_0_1_23"&gt;an issue in the past that the offset calibration would output a junk sample under certain conditions&lt;/a&gt;, but I have never heard about this being the case on the nRF9160 before. Could you try to apply the workaround in this Errata 86 for the nRF52832, to see if that makes any difference?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/334886?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 13:04:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7798bb7a-a00a-4c64-b4e7-fc68277d8eed</guid><dc:creator>DamoL</dc:creator><description>&lt;p&gt;Hi Dan,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for the suggestion. I did wonder about capacitance on the input, but the voltage at the middle of the resistor divider is constant - meaning when I reset the device/program, it doesn&amp;#39;t drop to 0V and need recharging, its powered but a constant source. I put a 1uF cap in parallel, and used an oscilloscope to measure it voltage during a read and it doesn&amp;#39;t fluctuate at all, so I struggle to see how that can be the cause.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I realise I can do a double read and throw the first one, but I just wondered why this was in case there was something inherently wrong with my code/logic, which could become a larger issue later.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Damien&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ADC - First read is always wrong</title><link>https://devzone.nordicsemi.com/thread/334694?ContentTypeID=1</link><pubDate>Mon, 18 Oct 2021 21:02:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac32ae29-f41e-437a-8ee1-82fdbe0b7b57</guid><dc:creator>Kenya87</dc:creator><description>&lt;p&gt;I am a 20 plus year experienced hardware embedded design engineer.&amp;nbsp; When I have seen the symptoms like you described it is often caused because the ADC input has some capacitance (which is normal)... the first time you read the ADC the amount of time the capacitance is allowed to charge is too short and thus a low reading is achieved.. after two or three readings the capacitor is charged enough to have an accurate reading.&amp;nbsp; A great way to test this is to put a capacitor in parallel with the bottom (one connected to DC common often referred to as ground) resistor of your resistor divider.&amp;nbsp; This way the external capacitor you have just connected will be fully charged at the 2.5VDC level BEFORE the ADC samples.&amp;nbsp; Now the external capacitor charge can be delivered very quickly to charge the ADC capacitance and thus a correct reading can be acquired on the first attempt.&amp;nbsp; I normally use a 0.1 to 1 uF capacitor for this purpose but the value is not&amp;nbsp;very critical (I would avoid anything in the pF range as the idea is to have lots of charge in the external capacitor versus the relatively small ADC capacitance (normally about 10 to 20 pF).&lt;/p&gt;
&lt;p&gt;What if your company has already made 10 million circuit board assemblies and they don&amp;#39;t want to go back and add the extra capacitor to the design... what can you do?... two solutions that do NOT require an external capacitor are common.&lt;/p&gt;
&lt;p&gt;1) Do exactly what you stumbled upon... read twice and throw the first reading out! then use the second reading&amp;nbsp; OR&lt;/p&gt;
&lt;p&gt;2) If the ADC has a mux in front of it you can read a similar voltage on another channel first and then switch to your ADC channel as the internal capacitance charge will remain.&amp;nbsp; This only works IF you have a mux and a similar voltage on another channel (internal power rail is a common choice).&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Dan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>