Bird
Raised Fist0
Embedded Cprogramming~20 mins

Single channel ADC reading in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ADC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this ADC reading code?

Consider the following embedded C code snippet that reads from a single ADC channel and stores the result in a variable. What will be the value of adc_value after execution?

Embedded C
volatile uint16_t adc_value = 0;
void read_adc(void) {
    ADC->CHSELR = ADC_CHSELR_CHSEL0; // Select channel 0
    ADC->CR |= ADC_CR_ADSTART;       // Start conversion
    while (!(ADC->ISR & ADC_ISR_EOC)); // Wait for conversion complete
    adc_value = ADC->DR;             // Read data register
}
Aadc_value is always zero because ADC is not started
Badc_value contains the 12-bit ADC conversion result from channel 0
Cadc_value contains a random value due to missing conversion start
Dadc_value contains the previous conversion result, not updated
Attempts:
2 left
💡 Hint

Check if the ADC conversion is started and if the code waits for completion before reading.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes single channel ADC reading?

In embedded systems, what does single channel ADC reading mean?

AReading digital inputs instead of analog
BReading multiple ADC channels simultaneously
CUsing ADC in differential mode with two channels combined
DReading analog input from one specific ADC channel at a time
Attempts:
2 left
💡 Hint

Focus on the meaning of 'single channel' in ADC context.

🔧 Debug
advanced
2:30remaining
Why does this ADC reading code hang in the while loop?

Analyze the following code snippet. Why does the program get stuck in the while loop?

Embedded C
ADC->CHSELR = ADC_CHSELR_CHSEL1; // Select channel 1
ADC->CR |= ADC_CR_ADSTART;         // Start conversion
while (!(ADC->ISR & ADC_ISR_EOC)); // Wait for conversion complete
uint16_t val = ADC->DR;
AThe ADC is not enabled before starting conversion, so EOC flag never sets
BThe channel selection register is incorrect, causing conversion failure
CThe data register is read before conversion starts
DThe interrupt flag is cleared too early, causing infinite wait
Attempts:
2 left
💡 Hint

Check if ADC peripheral is enabled before starting conversion.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this ADC reading code?

Identify the option that corrects the syntax error in the following code snippet:

uint16_t read_adc() {
    ADC->CHSELR = ADC_CHSELR_CHSEL2
    ADC->CR |= ADC_CR_ADSTART;
    while (!(ADC->ISR & ADC_ISR_EOC));
    return ADC->DR;
}
AAdd a semicolon after the channel selection line
BReplace ADC_CHSELR_CHSEL2 with ADC_CHSELR_CHSEL3
CChange return type to void
DRemove the while loop
Attempts:
2 left
💡 Hint

Look for missing punctuation that causes syntax errors.

🚀 Application
expert
3:00remaining
How many ADC readings are stored after this code runs?

Given this code that reads ADC channel 0 five times and stores results in an array, how many valid ADC readings does the array contain?

uint16_t adc_results[5];
for (int i = 0; i < 5; i++) {
    ADC->CHSELR = ADC_CHSELR_CHSEL0;
    ADC->CR |= ADC_CR_ADSTART;
    while (!(ADC->ISR & ADC_ISR_EOC));
    adc_results[i] = ADC->DR;
}
AOnly 1 valid reading, repeated 5 times
B0 readings because ADC is not enabled
C5 valid ADC readings, one per loop iteration
DUndefined number due to missing delay between readings
Attempts:
2 left
💡 Hint

Consider the loop and waiting for conversion complete each time.

Practice

(1/5)
1. What does a single channel ADC do in embedded C programming?
easy
A. It reads one analog input and converts it to a digital number.
B. It writes digital data to an analog output.
C. It reads multiple analog inputs simultaneously.
D. It converts digital signals to analog signals.

Solution

  1. Step 1: Understand ADC function

    An ADC (Analog to Digital Converter) converts analog signals to digital numbers.
  2. Step 2: Identify single channel meaning

    Single channel means it reads only one analog input at a time.
  3. Final Answer:

    It reads one analog input and converts it to a digital number. -> Option A
  4. Quick Check:

    Single channel ADC = One analog input read [OK]
Hint: Single channel ADC reads one input only [OK]
Common Mistakes:
  • Confusing ADC with DAC
  • Thinking ADC reads multiple inputs at once
  • Mixing analog output with input reading
2. Which of the following is the correct way to start an ADC conversion in embedded C?
easy
A. ADC_Stop();
B. ADC_StartConversion();
C. ADC_Read();
D. ADC_Init();

Solution

  1. Step 1: Identify function to start ADC

    Starting ADC conversion usually requires a function like ADC_StartConversion().
  2. Step 2: Differentiate other functions

    ADC_Read() reads the value, ADC_Init() initializes hardware, ADC_Stop() stops ADC.
  3. Final Answer:

    ADC_StartConversion(); -> Option B
  4. Quick Check:

    Start ADC = ADC_StartConversion() [OK]
Hint: Start ADC with ADC_StartConversion() [OK]
Common Mistakes:
  • Using ADC_Read() to start conversion
  • Confusing initialization with start
  • Calling ADC_Stop() instead of start
3. What will be the output of this code snippet if the ADC reads a value of 512?
int adcValue = ADC_Read();
printf("ADC Value: %d\n", adcValue);
medium
A. ADC Value: 0
B. ADC Value: 1023
C. ADC Value: 512
D. ADC Value: -512

Solution

  1. Step 1: Understand ADC_Read() output

    ADC_Read() returns the current digital value from ADC, here given as 512.
  2. Step 2: Print the value

    printf prints the integer value stored in adcValue, which is 512.
  3. Final Answer:

    ADC Value: 512 -> Option C
  4. Quick Check:

    ADC_Read() = 512 printed as is [OK]
Hint: ADC_Read() returns current ADC number [OK]
Common Mistakes:
  • Assuming ADC_Read() returns max value always
  • Confusing negative values in ADC reading
  • Printing wrong variable or format
4. Identify the error in this ADC reading code:
ADC_Init();
int value = ADC_Read();
ADC_StartConversion();
printf("Value: %d", value);
medium
A. Variable 'value' should be float
B. ADC_Init() is missing
C. printf format specifier is wrong
D. ADC_StartConversion() should be called before ADC_Read()

Solution

  1. Step 1: Check ADC function order

    ADC_StartConversion() must be called before ADC_Read() to start conversion.
  2. Step 2: Analyze code sequence

    Here, ADC_Read() is called before starting conversion, so it reads old or invalid data.
  3. Final Answer:

    ADC_StartConversion() should be called before ADC_Read() -> Option D
  4. Quick Check:

    Start conversion before reading ADC [OK]
Hint: Start ADC conversion before reading value [OK]
Common Mistakes:
  • Reading ADC before starting conversion
  • Ignoring initialization step
  • Wrong printf format for int
5. You want to read an analog sensor value using a single channel ADC and convert it to voltage (0-5V) with 10-bit resolution. Which code snippet correctly calculates the voltage?
int adcValue = ADC_Read();
float voltage = ?;
hard
A. voltage = (adcValue / 1023.0) * 5.0;
B. voltage = (adcValue * 5) / 1023;
C. voltage = adcValue * 5.0 / 1024.0;
D. voltage = (adcValue / 1024) * 5;

Solution

  1. Step 1: Understand ADC resolution and voltage range

    10-bit ADC means values from 0 to 1023 represent 0 to 5 volts.
  2. Step 2: Calculate voltage correctly

    Voltage = (adcValue / 1023.0) * 5.0; uses floating division and full scale 1023.
  3. Final Answer:

    voltage = (adcValue / 1023.0) * 5.0; -> Option A
  4. Quick Check:

    Voltage = ADC value / max * 5V [OK]
Hint: Divide by 1023.0 for 10-bit ADC voltage calc [OK]
Common Mistakes:
  • Dividing by 1024 instead of 1023
  • Using integer division instead of float
  • Multiplying before dividing causing wrong result
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard