Sensor reading through ADC (temperature, light) in Embedded C - Time & Space Complexity
When reading sensors using ADC, it is important to know how the time to get data changes as we read more samples.
We want to see how the program's work grows when reading multiple sensor values.
Analyze the time complexity of the following code snippet.
// Read multiple sensor values using ADC
void readSensors(int *buffer, int count) {
for (int i = 0; i < count; i++) {
buffer[i] = readADC(); // Read one sensor value
}
}
int readADC() {
// Simulate ADC reading delay
return 42; // Dummy sensor value
}
This code reads sensor values one by one and stores them in a buffer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calls
readADC()repeatedly. - How many times: Exactly
counttimes, once per sensor reading.
Each additional sensor reading adds one more call to readADC(), so work grows steadily.
| Input Size (count) | Approx. Operations |
|---|---|
| 10 | 10 calls to readADC() |
| 100 | 100 calls to readADC() |
| 1000 | 1000 calls to readADC() |
Pattern observation: The total work grows directly with the number of readings.
Time Complexity: O(n)
This means if you double the number of sensor readings, the time to complete also roughly doubles.
[X] Wrong: "Reading multiple sensors happens instantly and does not add time."
[OK] Correct: Each sensor reading takes time, so more readings mean more total time.
Understanding how sensor reading time grows helps you design efficient embedded programs and explain your code clearly in interviews.
"What if we read sensors in parallel instead of one by one? How would the time complexity change?"
Practice
ADC converts:Solution
Step 1: Understand ADC function
ADC changes continuous analog signals from sensors into digital numbers.Step 2: Match function to options
Only Analog sensor signals into digital values the microcontroller can process correctly describes converting analog sensor signals to digital values.Final Answer:
Analog sensor signals into digital values the microcontroller can process -> Option AQuick Check:
ADC converts analog to digital [OK]
- Confusing ADC with DAC (Digital-to-Analog Converter)
- Thinking ADC outputs temperature directly
- Assuming ADC converts digital to analog
ADC_StartConversion(____);
Solution
Step 1: Identify correct channel constant
Embedded C ADC functions usually use predefined constants like ADC_Channel_3 for clarity.Step 2: Check options for proper syntax
ADC_Channel_3 uses the correct constant; others are either raw numbers or strings, which are incorrect.Final Answer:
ADC_Channel_3 -> Option DQuick Check:
Use predefined channel constants [OK]
- Passing channel number as string
- Using raw integers without constants
- Using undefined variable names
int adc_value = ADC_Read(ADC_Channel_1);
float voltage = (adc_value / 1023.0) * 5.0;
float temperature = (voltage - 0.5) * 100;
printf("Temp: %.1f C\n", temperature);What will be the output if
adc_value is 216?Solution
Step 1: Compute voltage from adc_value
voltage = (216 / 1023.0) * 5.0 ≈ 1.055 VStep 2: Compute temperature from voltage
temperature = (1.055 - 0.5) * 100 = 55.5 °CStep 3: Format output with printf
%.1f formats 55.5 as "55.5", so output is "Temp: 55.5 C"Final Answer:
Temp: 55.5 C -> Option CQuick Check:
(adc / 1023.0 * 5.0 - 0.5) * 100 ≈ 55.5 [OK]
- Using integer division instead of float
- Forgetting to subtract 0.5V before multiplying
- Mixing up ADC max value (1023) and voltage reference
int adc_val = ADC_Read(ADC_Channel_2);
float voltage = adc_val / 1023 * 3.3;
printf("Voltage: %.2f V\n", voltage);Solution
Step 1: Analyze division operation
adc_val and 1023 are integers, so adc_val / 1023 does integer division, which truncates to 0 if adc_val < 1023.Step 2: Effect on voltage calculation
Integer division results in 0 or 1, so voltage becomes 0 or 3.3 incorrectly.Final Answer:
Integer division causes voltage to be zero -> Option AQuick Check:
Use float division to avoid zero voltage [OK]
- Forgetting to cast to float
- Assuming integer division works for floats
- Using wrong printf format specifier
int temp_adc = ADC_Read(ADC_Channel_1); int light_adc = ADC_Read(ADC_Channel_2); float temp_voltage = (float)temp_adc / 1023 * 3.3; float light_voltage = (float)light_adc / 1023 * 3.3;
Solution
Step 1: Check ADC reading and conversion
ADC_Read called separately for each channel, storing in int variables.Step 2: Voltage conversion uses float cast and divides by 1023 (max ADC value for 10-bit), multiplied by 3.3V reference.
This is correct for 10-bit ADC.Final Answer:
Correct as is, both readings converted properly -> Option BQuick Check:
Separate reads and float division with 1023 max value [OK]
- Dividing by 1024 instead of 1023
- Not initializing ADC before reading
- Trying to read both sensors with one ADC_Read call
Ready to prove it?
Pass the test and this pattern moves unknown
