Bird
Raised Fist0
Embedded Cprogramming~20 mins

ADC conversion process (sample and hold) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ADC Sample and Hold Master
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 sample and hold simulation code?

Consider this embedded C code simulating an ADC sample and hold process. What will be printed?

Embedded C
#include <stdio.h>

int main() {
    int analog_signal = 512; // Simulated analog input (10-bit ADC max 1023)
    int sample_and_hold = 0;
    int digital_value = 0;

    // Sample phase
    sample_and_hold = analog_signal;

    // Hold phase (conversion simulated)
    digital_value = sample_and_hold;

    printf("Digital value: %d\n", digital_value);
    return 0;
}
ADigital value: 512
BCompilation error
CDigital value: 1023
DDigital value: 0
Attempts:
2 left
💡 Hint

Think about what the sample and hold phase does to the analog signal before conversion.

❓ Predict Output
intermediate
2:00remaining
What error occurs in this ADC sample and hold code?

Analyze this code snippet simulating ADC sample and hold. What error will it produce?

Embedded C
#include <stdio.h>

int main() {
    int *sample_and_hold = NULL;
    int analog_signal = 256;

    // Sample phase
    *sample_and_hold = analog_signal; // Attempt to store value

    printf("Value: %d\n", *sample_and_hold);
    return 0;
}
AUndefined behavior but prints 0
BCompilation error: dereferencing NULL pointer not allowed
CPrints: Value: 256
DSegmentation fault (runtime error)
Attempts:
2 left
💡 Hint

What happens if you try to write to a NULL pointer?

🧠 Conceptual
advanced
1:30remaining
Which statement correctly describes the sample and hold phase in ADC?

Choose the correct description of the sample and hold phase in an ADC conversion process.

AIt captures the analog input voltage and holds it steady during conversion.
BIt converts the analog signal directly into a digital value instantly.
CIt amplifies the analog signal before conversion.
DIt filters out noise from the digital output after conversion.
Attempts:
2 left
💡 Hint

Think about why the ADC needs a stable input during conversion.

❓ Predict Output
advanced
2:00remaining
What is the output of this ADC sample and hold timing simulation?

Given this code simulating ADC sample and hold timing, what will be the output?

Embedded C
#include <stdio.h>
#include <stdbool.h>

int main() {
    int analog_signal = 700;
    int sample_and_hold = 0;
    bool sample_phase = true;

    if (sample_phase) {
        sample_and_hold = analog_signal;
        sample_phase = false;
    }

    // Hold phase
    int digital_value = sample_and_hold;

    printf("Digital value: %d\n", digital_value);
    return 0;
}
ADigital value: 0
BDigital value: 700
CDigital value: 1
DCompilation error
Attempts:
2 left
💡 Hint

Check what happens during the sample phase before hold.

❓ Predict Output
expert
2:30remaining
What is the value of 'digital_value' after this ADC sample and hold code runs?

Analyze this embedded C code simulating an ADC sample and hold with noise. What is the final value of digital_value?

Embedded C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(0);
    int analog_signal = 400;
    int sample_and_hold = 0;
    int noise = 0;

    // Sample phase with noise
    noise = rand() % 5 - 2; // Noise between -2 and +2
    sample_and_hold = analog_signal + noise;

    // Hold phase
    int digital_value = sample_and_hold;

    printf("Digital value: %d\n", digital_value);
    return 0;
}
ADigital value: 402
BDigital value: 400
CDigital value: 399
DDigital value: 403
Attempts:
2 left
💡 Hint

Check the random noise generated with srand(0) and rand() % 5 - 2.

Practice

(1/5)
1.

What is the main purpose of the sample and hold stage in an ADC conversion process?

easy
A. To amplify the analog signal before conversion
B. To convert the analog signal directly to digital bits
C. To capture and hold the analog voltage steady during conversion
D. To reset the ADC hardware before starting conversion

Solution

  1. Step 1: Understand the role of sample and hold

    The sample and hold stage captures the analog voltage at a specific moment and holds it steady.
  2. Step 2: Differentiate from other ADC stages

    Conversion to digital bits happens after holding; amplification or reset are separate steps.
  3. Final Answer:

    To capture and hold the analog voltage steady during conversion -> Option C
  4. Quick Check:

    Sample and hold = capture and hold voltage [OK]
Hint: Sample and hold freezes input voltage for accurate conversion [OK]
Common Mistakes:
  • Confusing sample and hold with conversion step
  • Thinking sample and hold amplifies signal
  • Assuming sample and hold resets ADC
2.

Which of the following is the correct way to start ADC sampling in embedded C?

ADC_Start();
easy
A. ADC_Start(); // Starts sampling and conversion immediately
B. ADC_Start; // Missing parentheses causes error
C. ADC_Start[]; // Incorrect syntax for function call
D. Start_ADC(); // Wrong function name

Solution

  1. Step 1: Identify correct function call syntax

    Functions in C require parentheses even if no arguments are passed.
  2. Step 2: Check each option's syntax

    ADC_Start(); // Starts sampling and conversion immediately uses correct syntax with parentheses; others have syntax errors or wrong names.
  3. Final Answer:

    ADC_Start(); // Starts sampling and conversion immediately -> Option A
  4. Quick Check:

    Function call needs parentheses () [OK]
Hint: Function calls always need parentheses () in C [OK]
Common Mistakes:
  • Omitting parentheses when calling functions
  • Using brackets [] instead of parentheses
  • Using incorrect function names
3.

Consider the following embedded C code snippet for ADC conversion:

int adc_value = 0;
ADC_Start();
while(!ADC_ConversionComplete()) {}
adc_value = ADC_Read();
printf("%d", adc_value);

What will be the output if the analog input voltage corresponds to digital value 512?

medium
A. 0
B. 512
C. 1023
D. Conversion error

Solution

  1. Step 1: Analyze the ADC process in code

    ADC_Start() begins sampling; the while loop waits until conversion is done; ADC_Read() reads the digital value.
  2. Step 2: Understand the analog input mapping

    If analog input corresponds to digital 512, ADC_Read() returns 512, which is printed.
  3. Final Answer:

    512 -> Option B
  4. Quick Check:

    ADC_Read() returns converted digital value [OK]
Hint: Wait for conversion complete before reading ADC value [OK]
Common Mistakes:
  • Reading ADC value before conversion completes
  • Assuming ADC_Read() returns zero by default
  • Confusing max ADC value with current reading
4.

Identify the error in this ADC conversion code snippet:

ADC_Start();
if(ADC_ConversionComplete()) {
    int value = ADC_Read();
    printf("%d", value);
}

What is the problem?

medium
A. Missing semicolon after ADC_Start()
B. No error, code is correct
C. Incorrect function name ADC_Read()
D. Using if instead of while to wait for conversion

Solution

  1. Step 1: Check how conversion completion is handled

    The code uses if to check conversion complete, which may be false if conversion not done yet.
  2. Step 2: Explain why while loop is needed

    Using while waits until conversion finishes; if may skip reading if conversion incomplete.
  3. Final Answer:

    Using if instead of while to wait for conversion -> Option D
  4. Quick Check:

    Wait loop needed to ensure conversion done [OK]
Hint: Use while loop to wait for ADC conversion completion [OK]
Common Mistakes:
  • Using if instead of while to wait
  • Assuming conversion is instant
  • Ignoring need to wait before reading
5.

You want to read an analog sensor value using ADC with sample and hold. Which sequence of steps is correct in embedded C?

1. Start sampling
2. Wait for sample and hold to stabilize
3. Start conversion
4. Wait for conversion complete
5. Read digital value

Which option correctly orders these steps?

hard
A. 1, 2, 3, 4, 5
B. 3, 1, 2, 5, 4
C. 1, 3, 2, 4, 5
D. 2, 1, 3, 5, 4

Solution

  1. Step 1: Understand sample and hold timing

    First, start sampling to capture the analog signal, then wait for it to stabilize (hold).
  2. Step 2: Start conversion after hold stabilizes, then wait for conversion to finish, and finally read value

    This ensures accurate digital reading of the stable analog input.
  3. Final Answer:

    1, 2, 3, 4, 5 -> Option A
  4. Quick Check:

    Sample, hold, convert, wait, read = 1,2,3,4,5 [OK]
Hint: Sample and hold before conversion, then wait and read [OK]
Common Mistakes:
  • Starting conversion before sample stabilizes
  • Reading value before conversion completes
  • Mixing order of steps
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