Arduino SAMD Board’s ADC runs way too slow!

The Analog to Digital Converter in the SAMD21 is specified to run with a clock frequency from 30 to 2,100 kHz. The Arduino Library configures the ADC clock to be the 48 MHz system clock divided by 256, or 187.5 kHz, or a clock period of 5.33 µs. The conversion time is 7 clock periods, but can be longer if the sampling time is increased beyond the minimum ½ clock period. The Arduino library sets the sampling time to the maximum 47 additional half periods (or 23 clock periods). This means the total measurement time is 30 clock periods, 160 µs.

However it is actually worse than that. The first measurement after setting the reference voltage must be discarded. Now you might say that the reference voltage has not been changed, but the analogRead function doesn’t know that so it always measures twice and uses the second. 320 µs.

The clock divider can be set to 32, giving a frequency of 1,500 kHz, or a clock period of 0.667 µs. Even if the maximum sampling time is used, that would still be 20 µs for a measurement. And with sufficient smarts in the code only one measurement would be necessary. That’s a 16 times speed improvement! I know from experimentation that the ADC does run just fine at this higher rate.

This is one advantage of bypassing the Arduino Library and accessing the hardware directly. The ADC can also be interrupt driven (as can the ADCs in any Arduino board) putting the operation in the background, potentially making the measurement time zero.