📖 Scenario: Imagine you are building a simple embedded system that reads temperature from a sensor. The sensor gives an analog voltage that changes with temperature. Your microcontroller can only understand digital numbers, not analog voltages.
🎯 Goal: You will create a small program that shows why an Analog-to-Digital Converter (ADC) is needed to read analog sensor values and convert them into digital numbers your microcontroller can use.
📋 What You'll Learn
Create a variable to represent an analog voltage value from a sensor
Create a variable to represent the ADC resolution (number of bits)
Write a function to convert the analog voltage to a digital number using the ADC resolution
Print the digital value to show the conversion result
💡 Why This Matters
🌍 Real World
Embedded systems often read sensors that output analog signals. ADC converts these signals to digital so microcontrollers can process them.
💼 Career
Understanding ADC is essential for embedded developers working with sensors, IoT devices, and hardware interfacing.
Progress0 / 4 steps
1
Create an analog voltage variable
Create a float variable called analog_voltage and set it to 2.5 to represent the sensor voltage in volts.
Embedded C
Hint
Use float analog_voltage = 2.5f; to store the voltage.
2
Set ADC resolution
Create an int variable called adc_resolution and set it to 10 to represent a 10-bit ADC.
Embedded C
Hint
Use int adc_resolution = 10; to set the ADC bits.
3
Write ADC conversion function
Write a function called convert_adc that takes float voltage and int resolution as inputs and returns an int digital value. Use the formula: digital_value = (voltage / 5.0) * (2^resolution - 1).
Embedded C
Hint
Use bit shift (1 << resolution) to calculate max digital value.
4
Print the ADC conversion result
Call convert_adc with analog_voltage and adc_resolution, store the result in digital_value, and print it using printf.
Embedded C
Hint
Use printf("Digital value: %d\n", digital_value); to print.
Practice
(1/5)
1. Why do microcontrollers need an ADC (Analog to Digital Converter)?
easy
A. To increase the speed of the microcontroller
B. To convert analog signals from sensors into digital values they can process
C. To store data in memory
D. To power the microcontroller
Solution
Step 1: Understand what analog signals are
Analog signals are continuous signals from the real world, like temperature or light intensity.
Microcontrollers work with digital numbers, so they need to convert analog signals into digital form.
Final Answer:
To convert analog signals from sensors into digital values they can process -> Option B
Quick Check:
ADC converts analog to digital [OK]
Hint: ADC changes real signals to digital numbers [OK]
Common Mistakes:
Thinking ADC increases speed
Confusing ADC with memory storage
Believing ADC powers the microcontroller
2. Which of the following is the correct way to declare an ADC input pin in embedded C?
easy
A. char adc_pin = 'A';
B. float adc_pin = 5.0;
C. int adc_pin = 5;
D. void adc_pin = 5;
Solution
Step 1: Identify the correct data type for pin numbers
Pin numbers are integers, so int is appropriate.
Step 2: Check each option's syntax
int adc_pin = 5; uses int with a valid integer value. Others use wrong types or invalid syntax.
Final Answer:
int adc_pin = 5; -> Option C
Quick Check:
Pin numbers use int type [OK]
Hint: Pin numbers are integers, use int type [OK]
Common Mistakes:
Using float for pin numbers
Using void as a variable type
Assigning characters to pin numbers
3. Given this code snippet reading an ADC value:
int adc_value = read_adc(3);
printf("ADC Value: %d\n", adc_value);
What will be the output if the analog input at pin 3 is 2.5V and the ADC reference voltage is 5V with 10-bit resolution?
medium
A. ADC Value: 1023
B. ADC Value: 0
C. ADC Value: 256
D. ADC Value: 512
Solution
Step 1: Understand ADC resolution and voltage mapping
10-bit ADC means values from 0 to 1023 represent 0V to 5V.
Step 2: Calculate digital value for 2.5V input
Value = (2.5 / 5.0) * 1023 = 511.5, rounded to 512.
Final Answer:
ADC Value: 512 -> Option D
Quick Check:
Half voltage gives half max ADC value [OK]
Hint: Half voltage equals half max ADC count [OK]
Common Mistakes:
Using max ADC value for half voltage
Confusing resolution bits with max value
Ignoring voltage reference
4. This code tries to read an ADC value but always prints zero:
int adc_value;
adc_value = read_adc();
printf("Value: %d\n", adc_value);
What is the likely error?
medium
A. read_adc() needs a pin number argument
B. printf format specifier is wrong
C. adc_value should be float
D. Missing semicolon after read_adc()
Solution
Step 1: Check function usage
read_adc() usually requires the ADC channel or pin number as argument.
Step 2: Identify missing argument
Code calls read_adc() without pin, so it cannot read correct value, returns zero.
Final Answer:
read_adc() needs a pin number argument -> Option A
Quick Check:
Missing pin argument causes zero reading [OK]
Hint: Always pass pin/channel to ADC read function [OK]
Common Mistakes:
Assuming default pin if none given
Thinking printf format causes zero
Using float instead of int for adc_value
5. You want to measure temperature using an analog sensor connected to ADC channel 1. The sensor outputs 0V at 0°C and 3.3V at 100°C. The ADC reference voltage is 3.3V with 12-bit resolution. How do you calculate the temperature from the ADC reading in embedded C?
hard
A. temperature = (adc_value * 100.0) / 4095.0;
B. temperature = (adc_value * 3.3) / 100.0;
C. temperature = (adc_value * 4095.0) / 100.0;
D. temperature = (adc_value * 100.0) / 3.3;
Solution
Step 1: Understand ADC resolution and voltage range
12-bit ADC max value is 4095, voltage range is 0 to 3.3V.
Step 2: Map ADC value to voltage then to temperature
Voltage = (adc_value / 4095) * 3.3V; Temperature = (Voltage / 3.3V) * 100°C.