# Analog speed in SAMD boards

**URL:** <https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621>\
**Category:** 3rd Party Boards\
**Created:** [May 16, 2023, 2:59pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621 "2023-05-16T14:59:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![cschiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cschiang/32/965756_2.png) [@cschiang](https://forum.arduino.cc/u/cschiang)\
**Post date:** [May 16, 2023, 2:59pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/1 "2023-05-16T14:59:28Z")

</div>

Documentation comment that in arduino UNO analogRead() should take around 100 ms  
Ref: [analogRead() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/)

But what about other boards? Im specially interested in SAMD21 Cortex-M0+ 32bit boars (as for example MKR Zero). I try reading the documentation of the [ATSAMD21G18]( [SAM D21/DA1 Family Data Sheet (microchip.com)](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU32/ProductDocuments/DataSheets/SAM-D21-DA1-Family-Data-Sheet-DS40001882H.pdf)) but I wasnt able to find it.

Could anybody please give me a hand or tell me which factors determinate the speed?

Cheers!

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [May 16, 2023, 3:05pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/2 "2023-05-16T15:05:48Z")

</div>

Firstly, the statement there is not milliseconds:

> On ATmega based boards (UNO, Nano, Mini, Mega), it takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

Secondly, page 2 of the sheet you linked:

> One 12-bit, 350ksps Analog-to-Digital Converter (ADC) with up to 20 channels  
> • Differential and single-ended input  
> • 1/2x to 16x programmable gain stage  
> • Automatic offset and gain error compensation  
> • Oversampling and decimation in hardware to support 13-, 14-, 15- or 16-bit resolution

That should give you some ideas. Can't tell you what Arduino does with it, but it's a start.

---

<div class="post-metadata">

**Author:** ![cschiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cschiang/32/965756_2.png) [@cschiang](https://forum.arduino.cc/u/cschiang)\
**Post date:** [May 16, 2023, 3:46pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/3 "2023-05-16T15:46:23Z")

</div>

Mmm 350 ksps is around 3 us per sample. I was asking me how does that translate to real measurements! I will check.

Thanks!

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [May 16, 2023, 4:02pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/4 "2023-05-16T16:02:54Z")

</div>

To be clear, that's a "best coding effort" result that is unlikely to be attained by most codes/platforms. However, it suggests that performance should be much, much better than one can get on the Uno. How well it can do will be determined by the proficiency of the software developer, either rolling-your-own, or depending upon whatever feature set the Arduino team provides.  
Let us know what you are able to find out, or determine empirically.

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [May 16, 2023, 4:38pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/5 "2023-05-16T16:38:44Z")

</div>

analogRead in Arduino SAMD core is very slow, because of the default settings in core.  
but you can change them. see the SAMD21 datasheet.

I have

```arduino
  // changing Arduino defaults (wiring.c)
  while (ADC->STATUS.bit.SYNCBUSY == 1); // wait for synchronization
  ADC->SAMPCTRL.reg = 0x00;

```

in setup() to make analogRead faster

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [May 16, 2023, 4:58pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/6 "2023-05-16T16:58:43Z")

</div>

Lots of features described in the ADC section of the OP's linked manual, starting page 780; it remains to be seen just what is already predetermined by the core. Admittedly, I'm sure one can probably, with enough art, override everything and optimize, but realistically, that's beyond **most** general users here.

---

<div class="post-metadata">

**Author:** ![cschiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cschiang/32/965756_2.png) [@cschiang](https://forum.arduino.cc/u/cschiang)\
**Post date:** [May 16, 2023, 5:19pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/7 "2023-05-16T17:19:01Z")

</div>

Interesting! Thanks.  
How fast does it go like that?

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [May 16, 2023, 6:13pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/8 "2023-05-16T18:13:36Z")

</div>

> [@camsysca](#):
>
> One 12-bit, 350ksps Analog-to-Digital Converter (ADC) with up to 20 channels

How many multi-channel samples with required S&H time?  
What input resistance/recommended source impedance?

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [May 16, 2023, 8:45pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/9 "2023-05-16T20:45:18Z")

</div>

@DrDiettrich Not to be disrespectful, but with a prefix of Dr., I expect you can read as well as I can, and I'll be darned if I'll cut, paste, and translate a 40-page subsection of the manual for you. There is a lot of configuration available; a cursory glance tells me 350 ksamp is only doable for one channel in burst mode, but a thorough read, and testing, of the various settings will be required to tune the ADC to the needs of any particular project. The same is likely true for the processor in a Nano, for that matter - I'd bet the processor has loads of options not available in the plain vanilla Nano core available in the IDE. So, some learning and customization is the path forward.

---

<div class="post-metadata">

**Author:** ![MartinL](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martinl/32/216911_2.png) [@MartinL](https://forum.arduino.cc/u/MartinL)\
**Post date:** [May 16, 2023, 9:19pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/10 "2023-05-16T21:19:32Z")

</div>

Hi @cschiang

Here's the calculations I did awhile back for the SAMD21's ADC:

> <https://github.com/arduino/ArduinoCore-samd/issues/327>
>
> The conversion time for the Arduino Zero is currently around 426us, at 12-bit re…solution.
> 
> This value can be calculated from the SAMD21 datasheet:
> 
> \_\_1) ADC Sample Time Calculation\_\_
> 
> Take the processor clock at (approximately) 48MHz and divide by the ADC prescaler set at 512:
> 
> ADC Clock Frequency = 48MHz / 512 = 93750Hz
> 
> Therefore the ADC clock period is 10.66us (1 / 93750).
> 
> The default ADC sample time is half the ADC clock period, so normally the sample time is:
> 
> Default ADC sample time = 10.66us / 2 = 5.33us
> 
> However, in the “wiring.c” file the ADC's SAMPLEN bitfield in the SAMPCTRL register that extends the sample time, has been set to 63, (0x3F hex). This determines the number of half ADC cycles taken to complete the sample given by the formula:
> 
> Adjusted ADC sample time = (63 + 1) \* (10.66us / 2) = 341.12us
> 
> \_\_2) ADC Conversion Time Calculation\_\_
> 
> For the ADC configured for a 0.5 gain, in single ended mode, as on the Arduino Zero, the delay gain is 1.5. (Table 33-1 in the SAMD21 datasheet).
> 
> Using the formula in the datasheet, at 12-bits resolution and a delay gain of 1.5, the propagation delay or in other words the number full ADC clock cycles to complete a conversion for the ADC is:
> 
> Propagation Delay (ADC Conversion Time) = (1 + 12 / 2 + 1.5) / 93750 = 90.66us
> 
> As the propagation delay includes the sample time (half ADC clock period) at 5.33us, the total ADC conversion time is:
> 
> ADC Conversion Time = 341.12us + 90.66us – 5.33us = 426us
> 
> \_\_3) The SAMPCTRL register and ADC prescaler\_\_
> 
> The issue is that the SAMPCTRL register's SAMPLEN bitfield has been set at a maximum value of 63, this allows the maximum input source resistance, but at the expense of a hugely increased sample time.
> 
> In the electrical characteristics of the SAMD21 datasheet, the criteria for setting the sample time at 12-bit resolution is given by the formula:
> 
> T(sample) \>= (R(source) + R(sample)) \* C(sample) \* 9.02
> 
> where R and C sample are characteristics of the ADC input (taken from the SAMD21 datasheet):
> R(sample) = 3.5kOhms
> C(sample) = 3.5pF
> R(source) = source resistance
> T(sample) = sample time (usually half ADC clock period)
> 
> Using this equation, even if the SAMPCTRL register were to be set to 0, giving an ADC sample time of 5.33us, this would allow a maximum source resistance of:
> 
> R(source) = 5.33us / (3.5pF \* 9.02) – 3.5kOhms = 165331 Ohms
> 
> The resultant ADC conversion time of 90.66us, is more than a four fold decrease on the current value.
> 
> Furthermore, decreasing the ADC clock prescaler to say 128, instead of 512:
> 
> ADC Sample Time = (1 / ( 2 \* 48MHz / 128)) = 1.33us
> 
> ADC Conversion Time = 8.5 \* 2.66us = 22.67us
> 
> In practice the however the conversion time takes around just less than 30us, as the analogRead() function enables and disables the ADC each time it's called. (Using register manipulation it does take around 23us).
> 
> The maximum source resistance would be:
> 
> R(source) = 1.33us / (3.5pF \* 9.02) – 3.5kOhms = 38628 Ohms 
> 
> In my opinion, the ADC conversion time could be vastly reduced to around 30us without any noticeable effect for the vast majority of Arduino Zero users, unless of course they're using some voltage divider with large resistor values.

Section 1 calculates the sample delay extention SAMPCTRL register, this allows the sample time to extended to a specific value. Arduino max this out to 64 ADC half clock cycles.

Section 2 calculates the total ADC conversion time.

Section 3 covers the source resistance calculations. (This equation can be found the SAMD21's "Electrical Characteristics" section of the datasheet).

Using the _analogRead()_ function settings the ADC conversion time turns out to be around 430us, however this can be doubled, since _analogRead()_ actually performs two ADC reads, this is to account for any change to the reference voltage.

Using the ADC peripheral's regsiters, it's possible to change the ADC clock prescaler and sample control to alter the sample rate. However, at a faster rate the source impedance must be low enough to charge the ADC's sample capacitor sufficiently quickly.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [May 16, 2023, 9:32pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/11 "2023-05-16T21:32:49Z")

</div>

> [@camsysca](#):
>
> you can read

Yes, of course, but I have no personal interest in SAM architecture. I only wanted to point out details that affect effective analog speed of every ADC.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [May 16, 2023, 9:44pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/12 "2023-05-16T21:44:37Z")

</div>

Yes, this is all in the domain of application of the processor to the job at hand, and what can or can not be done to tune the process within the limitations of the Arduino IDE, or beyond it. There's even a chance that this processor isn't as good as the one used in the Nano, it's not a foregone conclusion. I think we're both just trying to say the same thing, differently. But the OP may or may not wish to go down that rabbit hole, or any other; he needs to think about what his application needs, then research what options will meet that need. For all we know, he will want to do MHz sampling, and all this talk has been for naught.

---

<div class="post-metadata">

**Author:** ![cschiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cschiang/32/965756_2.png) [@cschiang](https://forum.arduino.cc/u/cschiang)\
**Post date:** [May 17, 2023, 4:36am UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/13 "2023-05-17T04:36:22Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![cschiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cschiang/32/965756_2.png) [@cschiang](https://forum.arduino.cc/u/cschiang)\
**Post date:** [May 17, 2023, 12:41pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/14 "2023-05-17T12:41:24Z")

</div>

I thought that will be usefully for anybody looking for the effect of these changes: [ATmega ADC tutorial | Open Music Labs](http://www.openmusiclabs.com/learning/digital/atmega-adc/)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 13, 2023, 12:42pm UTC](https://forum.arduino.cc/t/analog-speed-in-samd-boards/1127621/15 "2023-11-13T12:42:14Z")

</div>

Closed on November 13, 2023, 12:42pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
