0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Read Temperature Sensor Using ADC in Embedded C

To read a temperature sensor using ADC in Embedded C, first initialize the ADC module, then start an ADC conversion on the sensor's input pin, and finally read the digital value from the ADC data register. Convert this digital value to temperature using the sensor's voltage-to-temperature formula.
📐

Syntax

Here is the basic pattern to read an ADC value in Embedded C:

  • Initialize ADC: Set up ADC registers and input channel.
  • Start Conversion: Trigger ADC to start converting analog input.
  • Wait for Completion: Check ADC status flag until conversion finishes.
  • Read Result: Read the digital value from ADC data register.
c
void ADC_Init() {
    // Configure ADC settings (example for generic MCU)
    ADC_CONTROL_REGISTER = ADC_ENABLE | ADC_PRESCALER_128;
    ADC_CHANNEL_SELECT = TEMP_SENSOR_CHANNEL;
}

uint16_t ADC_Read() {
    ADC_START_CONVERSION = 1;
    while(!ADC_CONVERSION_COMPLETE_FLAG);
    return ADC_DATA_REGISTER;
}
💻

Example

This example shows how to read a temperature sensor connected to ADC channel 0, convert the ADC value to voltage, then to temperature in Celsius assuming a 10-bit ADC and a sensor output of 10mV per °C.

c
#include <stdint.h>
#include <stdio.h>

#define ADC_MAX 1023
#define V_REF 5.0
#define TEMP_SENSOR_CHANNEL 0

// Mock registers and flags for demonstration
volatile uint16_t ADC_DATA_REGISTER = 0;
volatile uint8_t ADC_CONTROL_REGISTER = 0;
volatile uint8_t ADC_CHANNEL_SELECT = 0;
volatile uint8_t ADC_START_CONVERSION = 0;
volatile uint8_t ADC_CONVERSION_COMPLETE_FLAG = 0;

void ADC_Init() {
    // Initialize ADC hardware (mock)
    ADC_CONTROL_REGISTER = 1; // Enable ADC
    ADC_CHANNEL_SELECT = TEMP_SENSOR_CHANNEL;
}

uint16_t ADC_Read() {
    ADC_START_CONVERSION = 1;
    // Simulate ADC conversion delay and result
    ADC_DATA_REGISTER = 512; // Example mid-scale value
    ADC_CONVERSION_COMPLETE_FLAG = 1;
    while(!ADC_CONVERSION_COMPLETE_FLAG);
    ADC_CONVERSION_COMPLETE_FLAG = 0;
    return ADC_DATA_REGISTER;
}

float Convert_ADC_to_Temperature(uint16_t adc_value) {
    float voltage = (adc_value * V_REF) / ADC_MAX; // Convert ADC value to voltage
    float temperature_c = voltage / 0.01; // Sensor outputs 10mV per degree Celsius
    return temperature_c;
}

int main() {
    ADC_Init();
    uint16_t adc_val = ADC_Read();
    float temperature = Convert_ADC_to_Temperature(adc_val);
    printf("ADC Value: %u\n", adc_val);
    printf("Temperature: %.2f C\n", temperature);
    return 0;
}
Output
ADC Value: 512 Temperature: 250.24 C
⚠️

Common Pitfalls

  • Not initializing ADC properly: ADC must be enabled and configured before reading.
  • Wrong ADC channel: Make sure to select the correct input channel connected to the sensor.
  • Ignoring ADC resolution and reference voltage: Conversion to voltage and temperature depends on these values.
  • Not waiting for conversion complete: Reading ADC data before conversion finishes gives wrong results.
  • Incorrect sensor conversion formula: Use the sensor datasheet to convert voltage to temperature correctly.
c
/* Wrong way: Reading ADC without waiting */
uint16_t ADC_Read_Wrong() {
    ADC_START_CONVERSION = 1;
    // Missing wait for conversion complete
    return ADC_DATA_REGISTER; // May return old or invalid data
}

/* Right way: Wait for conversion complete */
uint16_t ADC_Read_Right() {
    ADC_START_CONVERSION = 1;
    while(!ADC_CONVERSION_COMPLETE_FLAG);
    ADC_CONVERSION_COMPLETE_FLAG = 0;
    return ADC_DATA_REGISTER;
}
📊

Quick Reference

Remember these key points when reading temperature sensor using ADC:

  • Initialize ADC and select correct channel.
  • Start ADC conversion and wait until it finishes.
  • Read ADC digital value from data register.
  • Convert ADC value to voltage using reference voltage and ADC resolution.
  • Convert voltage to temperature using sensor's voltage-to-temperature ratio.

Key Takeaways

Always initialize ADC and select the correct input channel before reading.
Wait for ADC conversion to complete before reading the data register.
Convert the ADC digital value to voltage using reference voltage and ADC resolution.
Use the sensor's datasheet formula to convert voltage to temperature accurately.
Common mistakes include skipping initialization, wrong channel, and ignoring conversion completion.