Arduino is universal platform, lot of wrappers to hide real hardware and it is slow. For example digital write is about 100times slower than direct access to hardware.
For example for AVR Mega328p based Arduino analogRead looks like (without macros):
int analogRead(uint8_t pin)
{
uint8_t low, high;
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
// set the analog reference (high two bits of ADMUX) and select the
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
ADMUX = (analog_reference << 6) | (pin & 0x07);
// start the conversion
sbi(ADCSRA, ADSC);
// ADSC is cleared when the conversion finishes
while (bit_is_set(ADCSRA, ADSC));
// we have to read ADCL first; doing so locks both ADCL
// and ADCH until ADCH is read. reading ADCL second would
// cause the results of each conversion to be discarded,
// as ADCL and ADCH would be locked when it completed.
low = ADCL;
high = ADCH;
// combine the two bytes
return (high << 8) | low;
}
There are no extra function calls, no much extra actions and ADC is enabled in the startup code (as most of hw).
On the other side there is ARM based arduino with SAM3X8E:
uint32_t analogRead(uint32_t ulPin)
{
uint32_t ulValue = 0;
uint32_t ulChannel;
if (ulPin < A0)
ulPin += A0;
ulChannel = g_APinDescription[ulPin].ulADCChannelNumber ;
static uint32_t latestSelectedChannel = -1;
switch ( g_APinDescription[ulPin].ulAnalogChannel )
{
// Handling ADC 12 bits channels
case ADC0 :
case ADC1 :
case ADC2 :
case ADC3 :
case ADC4 :
case ADC5 :
case ADC6 :
case ADC7 :
case ADC8 :
case ADC9 :
case ADC10 :
case ADC11 :
// Enable the corresponding channel
if (adc_get_channel_status(ADC, ulChannel) != 1) {
adc_enable_channel( ADC, ulChannel );
if ( latestSelectedChannel != (uint32_t)-1 && ulChannel != latestSelectedChannel)
adc_disable_channel( ADC, latestSelectedChannel );
latestSelectedChannel = ulChannel;
g_pinStatus[ulPin] = (g_pinStatus[ulPin] & 0xF0) | PIN_STATUS_ANALOG;
}
// Start the ADC
adc_start( ADC );
// Wait for end of conversion
while ((adc_get_status(ADC) & ADC_ISR_DRDY) != ADC_ISR_DRDY)
;
// Read the value
ulValue = adc_get_latest_value(ADC);
ulValue = mapResolution(ulValue, ADC_RESOLUTION, _readResolution);
break;
// Compiler could yell because we don't handle DAC pins
default :
ulValue=0;
break;
}
return ulValue;
}
As you can see there is little bit more actions to do single measure using HAL (Hardware Abstraction Level) so you have another level of abstraction here.
If you want faster actions, you can access hw directly, or use HAL and skip unnecessary steps. It would be much faster then.
int sensorPin = A1; int ledPin = 11; int sensorValue = 0; void setup() { analogReadResolution(12); pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin,HIGH); sensorValue = analogRead(sensorPin); digitalWrite(ledPin,LOW); }