📖 Scenario: You are working with a microcontroller that reads analog signals from a sensor. The sensor output is connected to one ADC (Analog to Digital Converter) channel. You want to read the sensor value and display it.
🎯 Goal: Build a simple embedded C program that initializes the ADC, reads a single channel value, and prints the result.
📋 What You'll Learn
Create a variable to store the ADC reading
Create a configuration variable for the ADC channel number
Write code to read the ADC value from the configured channel
Print the ADC reading value
💡 Why This Matters
🌍 Real World
Reading sensor data from analog inputs is common in embedded systems like temperature sensors, light sensors, or potentiometers.
💼 Career
Embedded developers often need to read ADC values to process sensor inputs and control hardware.
Progress0 / 4 steps
1
Create a variable to store the ADC reading
Create an integer variable called adc_value and set it to 0 to store the ADC reading.
Embedded C
Hint
Use int adc_value = 0; to create the variable.
2
Create a configuration variable for the ADC channel
Create an integer variable called adc_channel and set it to 3 to select ADC channel 3.
Embedded C
Hint
Use int adc_channel = 3; to set the channel.
3
Read the ADC value from the configured channel
Write a line of code that calls the function read_adc(adc_channel) and stores the result in adc_value.
Embedded C
Hint
Use adc_value = read_adc(adc_channel); to read the ADC.
4
Print the ADC reading value
Write a line of code that prints the text "ADC Reading: " followed by the value of adc_value using printf.
Embedded C
Hint
Use printf("ADC Reading: %d\n", adc_value); to print the value.
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
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 A
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
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 B
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
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 C
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
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 D
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.