Bird
Raised Fist0
Embedded Cprogramming~10 mins

Sensor reading through ADC (temperature, light) in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Sensor reading through ADC (temperature, light)
Start
↓
Initialize ADC
↓
Select Sensor Channel
↓
Start ADC Conversion
↓
Wait for Conversion Complete
↓
Read ADC Value
↓
Convert ADC Value to Physical Unit
↓
Use or Display Sensor Data
↓
End or Repeat
This flow shows how a microcontroller reads sensor data by initializing ADC, selecting the sensor channel, converting analog signals to digital, and then using the converted value.
Execution Sample
Embedded C
void readSensors() {
  ADC_Init();
  ADC_SelectChannel(TEMP_SENSOR_CHANNEL);
  ADC_StartConversion();
  while(!ADC_ConversionComplete());
  int tempValue = ADC_Read();
  float temperature = convertToTemperature(tempValue);
}
This code reads temperature sensor data by starting ADC conversion and converting the raw ADC value to temperature.
Execution Table
StepActionADC StateADC ValueTemperature ValueNotes
1ADC_Init()ADC ready--ADC hardware initialized
2ADC_SelectChannel(TEMP_SENSOR_CHANNEL)Channel set to TEMP_SENSOR_CHANNEL--Sensor channel selected
3ADC_StartConversion()Conversion started--ADC begins analog to digital conversion
4while(!ADC_ConversionComplete())Waiting--Loop waits until conversion done
5ADC_ConversionComplete() returns trueConversion complete--Conversion finished
6ADC_Read()Idle512-Raw ADC value read (example: 512)
7convertToTemperature(512)Idle51225.0Raw value converted to 25.0°C
8Use temperature valueIdle51225.0Temperature can be displayed or used
9End or repeatIdle51225.0Process ends or loops for next reading
💡 Process ends after reading and converting sensor data
Variable Tracker
VariableStartAfter Step 6After Step 7Final
ADC StateNot initializedIdleIdleIdle
ADC Value-512512512
Temperature Value--25.025.0
Key Moments - 3 Insights
Why do we wait in a loop after starting ADC conversion?
Because ADC conversion takes time, the loop waits until ADC_ConversionComplete() returns true, ensuring the ADC value is ready before reading (see execution_table step 4 and 5).
What does the raw ADC value represent?
The raw ADC value is a number proportional to the sensor's analog voltage, which must be converted to a meaningful unit like temperature (see execution_table step 6 and 7).
Why do we select a sensor channel before starting conversion?
Because ADC can read multiple sensors, selecting the channel tells ADC which sensor's signal to convert (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the ADC Value read at step 6?
A25.0
B512
C0
D-1
💡 Hint
Check the 'ADC Value' column at step 6 in the execution_table.
At which step does the ADC conversion complete?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for when ADC_ConversionComplete() returns true in the execution_table.
If the ADC value was 1023 instead of 512, what would change in the variable_tracker?
ATemperature Value would be higher than 25.0
BADC State would change to 'Busy'
CADC Value would stay 512
DTemperature Value would be lower than 25.0
💡 Hint
Higher ADC raw value means higher sensor reading, check 'Temperature Value' in variable_tracker.
Concept Snapshot
Sensor reading via ADC:
1. Initialize ADC hardware.
2. Select sensor channel.
3. Start ADC conversion.
4. Wait until conversion completes.
5. Read raw ADC value.
6. Convert raw value to physical unit (e.g., °C).
7. Use or display the sensor data.
Full Transcript
This visual execution trace shows how a microcontroller reads sensor data using ADC. First, the ADC hardware is initialized. Then, the sensor channel is selected to tell the ADC which sensor to read. The ADC conversion starts, and the program waits in a loop until the conversion is complete. After completion, the raw ADC value is read. This raw value is a number representing the sensor's analog voltage. It is then converted to a physical unit like temperature in degrees Celsius. Finally, the converted sensor data can be used or displayed. Variables like ADC state, ADC value, and temperature value change step-by-step as shown in the tables. Key moments clarify why waiting is needed, what the raw ADC value means, and why channel selection is important. The quiz questions help check understanding of these steps.

Practice

(1/5)
1. What is the main purpose of using an ADC (Analog-to-Digital Converter) in embedded systems when reading sensors like temperature or light?
ADC converts:
easy
A. Analog sensor signals into digital values the microcontroller can process
B. Digital signals into analog voltages for sensors
C. Temperature readings directly into Celsius values
D. Light intensity into sound signals

Solution

  1. Step 1: Understand ADC function

    ADC changes continuous analog signals from sensors into digital numbers.
  2. 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.
  3. Final Answer:

    Analog sensor signals into digital values the microcontroller can process -> Option A
  4. Quick Check:

    ADC converts analog to digital [OK]
Hint: Remember: ADC means Analog to Digital Converter [OK]
Common Mistakes:
  • Confusing ADC with DAC (Digital-to-Analog Converter)
  • Thinking ADC outputs temperature directly
  • Assuming ADC converts digital to analog
2. Which of the following is the correct way to start an ADC conversion on channel 3 in embedded C?
ADC_StartConversion(____);
easy
A. 3
B. "3"
C. channel3
D. ADC_Channel_3

Solution

  1. Step 1: Identify correct channel constant

    Embedded C ADC functions usually use predefined constants like ADC_Channel_3 for clarity.
  2. Step 2: Check options for proper syntax

    ADC_Channel_3 uses the correct constant; others are either raw numbers or strings, which are incorrect.
  3. Final Answer:

    ADC_Channel_3 -> Option D
  4. Quick Check:

    Use predefined channel constants [OK]
Hint: Use ADC_Channel_x constants, not raw numbers or strings [OK]
Common Mistakes:
  • Passing channel number as string
  • Using raw integers without constants
  • Using undefined variable names
3. Given this code snippet reading ADC value from a temperature sensor:
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?
medium
A. Temp: 60.0 C
B. Temp: 55.0 C
C. Temp: 55.5 C
D. Temp: 50.0 C

Solution

  1. Step 1: Compute voltage from adc_value

    voltage = (216 / 1023.0) * 5.0 ≈ 1.055 V
  2. Step 2: Compute temperature from voltage

    temperature = (1.055 - 0.5) * 100 = 55.5 °C
  3. Step 3: Format output with printf

    %.1f formats 55.5 as "55.5", so output is "Temp: 55.5 C"
  4. Final Answer:

    Temp: 55.5 C -> Option C
  5. Quick Check:

    (adc / 1023.0 * 5.0 - 0.5) * 100 ≈ 55.5 [OK]
Hint: Calculate voltage first, then apply temperature formula [OK]
Common Mistakes:
  • Using integer division instead of float
  • Forgetting to subtract 0.5V before multiplying
  • Mixing up ADC max value (1023) and voltage reference
4. Identify the error in this code snippet intended to read light sensor value and print voltage:
int adc_val = ADC_Read(ADC_Channel_2);
float voltage = adc_val / 1023 * 3.3;
printf("Voltage: %.2f V\n", voltage);
medium
A. Integer division causes voltage to be zero
B. printf format specifier should be %d instead of %f
C. Incorrect ADC channel used for light sensor
D. Missing semicolon after ADC_Read call

Solution

  1. 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.
  2. Step 2: Effect on voltage calculation

    Integer division results in 0 or 1, so voltage becomes 0 or 3.3 incorrectly.
  3. Final Answer:

    Integer division causes voltage to be zero -> Option A
  4. Quick Check:

    Use float division to avoid zero voltage [OK]
Hint: Cast numerator or denominator to float before division [OK]
Common Mistakes:
  • Forgetting to cast to float
  • Assuming integer division works for floats
  • Using wrong printf format specifier
5. You want to read both temperature and light sensors using ADC channels 1 and 2 respectively. Which code snippet correctly reads both sensors and converts their ADC values to voltages assuming 10-bit ADC and 3.3V reference?
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;
hard
A. Should divide by 1024 instead of 1023 for voltage calculation
B. Correct as is, both readings converted properly
C. Missing ADC initialization before reading sensors
D. ADC_Read should be called only once for both sensors

Solution

  1. Step 1: Check ADC reading and conversion

    ADC_Read called separately for each channel, storing in int variables.
  2. 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.
  3. Final Answer:

    Correct as is, both readings converted properly -> Option B
  4. Quick Check:

    Separate reads and float division with 1023 max value [OK]
Hint: Cast ADC value to float before division for accurate voltage [OK]
Common Mistakes:
  • Dividing by 1024 instead of 1023
  • Not initializing ADC before reading
  • Trying to read both sensors with one ADC_Read call
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