Single channel ADC reading in Embedded C - Time & Space Complexity
We want to understand how the time to read from an ADC changes as we take more readings.
How does the number of readings affect the total time spent?
Analyze the time complexity of the following code snippet.
int readADC(int channel) {
// Start ADC conversion
ADC_START_CONVERSION(channel);
// Wait until conversion is done
while(!ADC_CONVERSION_COMPLETE());
// Return the ADC value
return ADC_GET_VALUE();
}
int readMultiple(int channel, int times) {
int sum = 0;
for(int i = 0; i < times; i++) {
sum += readADC(channel);
}
return sum / times;
}
This code reads an analog value from one ADC channel multiple times and averages the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop in
readMultiplecallsreadADCrepeatedly. - How many times: Exactly
timestimes, which is the input size.
Each additional reading adds one full ADC read cycle, so time grows directly with the number of readings.
| Input Size (times) | Approx. Operations |
|---|---|
| 10 | 10 ADC reads |
| 100 | 100 ADC reads |
| 1000 | 1000 ADC reads |
Pattern observation: Doubling the number of readings doubles the total time.
Time Complexity: O(n)
This means the total time grows linearly with the number of ADC readings requested.
[X] Wrong: "Reading multiple times is just as fast as reading once because the ADC is fast."
[OK] Correct: Each ADC read takes time to start, convert, and finish, so multiple reads add up in time.
Understanding how repeated hardware reads affect time helps you write efficient embedded code and explain your reasoning clearly.
"What if we read from multiple ADC channels instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand ADC function
An ADC (Analog to Digital Converter) converts analog signals to digital numbers.Step 2: Identify single channel meaning
Single channel means it reads only one analog input at a time.Final Answer:
It reads one analog input and converts it to a digital number. -> Option AQuick Check:
Single channel ADC = One analog input read [OK]
- Confusing ADC with DAC
- Thinking ADC reads multiple inputs at once
- Mixing analog output with input reading
Solution
Step 1: Identify function to start ADC
Starting ADC conversion usually requires a function like ADC_StartConversion().Step 2: Differentiate other functions
ADC_Read() reads the value, ADC_Init() initializes hardware, ADC_Stop() stops ADC.Final Answer:
ADC_StartConversion(); -> Option BQuick Check:
Start ADC = ADC_StartConversion() [OK]
- Using ADC_Read() to start conversion
- Confusing initialization with start
- Calling ADC_Stop() instead of start
int adcValue = ADC_Read();
printf("ADC Value: %d\n", adcValue);Solution
Step 1: Understand ADC_Read() output
ADC_Read() returns the current digital value from ADC, here given as 512.Step 2: Print the value
printf prints the integer value stored in adcValue, which is 512.Final Answer:
ADC Value: 512 -> Option CQuick Check:
ADC_Read() = 512 printed as is [OK]
- Assuming ADC_Read() returns max value always
- Confusing negative values in ADC reading
- Printing wrong variable or format
ADC_Init();
int value = ADC_Read();
ADC_StartConversion();
printf("Value: %d", value);Solution
Step 1: Check ADC function order
ADC_StartConversion() must be called before ADC_Read() to start conversion.Step 2: Analyze code sequence
Here, ADC_Read() is called before starting conversion, so it reads old or invalid data.Final Answer:
ADC_StartConversion() should be called before ADC_Read() -> Option DQuick Check:
Start conversion before reading ADC [OK]
- Reading ADC before starting conversion
- Ignoring initialization step
- Wrong printf format for int
int adcValue = ADC_Read(); float voltage = ?;
Solution
Step 1: Understand ADC resolution and voltage range
10-bit ADC means values from 0 to 1023 represent 0 to 5 volts.Step 2: Calculate voltage correctly
Voltage = (adcValue / 1023.0) * 5.0; uses floating division and full scale 1023.Final Answer:
voltage = (adcValue / 1023.0) * 5.0; -> Option AQuick Check:
Voltage = ADC value / max * 5V [OK]
- Dividing by 1024 instead of 1023
- Using integer division instead of float
- Multiplying before dividing causing wrong result
Ready to prove it?
Pass the test and this pattern moves unknown
