0
0
Power-electronicsHow-ToBeginner · 3 min read

How to Read Potentiometer Using ADC in Embedded C

To read a potentiometer using ADC in Embedded C, configure the ADC module to read the analog voltage from the potentiometer pin, start the conversion, and then read the digital value from the ADC data register. This value represents the potentiometer position as a number proportional to the voltage.
📐

Syntax

Here is the basic syntax to read an analog value from a potentiometer using ADC in Embedded C:

  • ADC_Init(); - Initialize the ADC hardware.
  • ADC_StartConversion(channel); - Start ADC conversion on the specified channel connected to the potentiometer.
  • ADC_WaitForConversion(); - Wait until the conversion is complete.
  • value = ADC_ReadData(); - Read the digital value representing the analog voltage.
c
void ADC_Init(void);
void ADC_StartConversion(uint8_t channel);
void ADC_WaitForConversion(void);
uint16_t ADC_ReadData(void);
💻

Example

This example shows how to read a potentiometer connected to ADC channel 0 and print the digital value.

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

// Mock functions for ADC hardware (replace with your MCU's ADC functions)
void ADC_Init(void) {
    // Initialize ADC hardware
}

void ADC_StartConversion(uint8_t channel) {
    // Start ADC conversion on given channel
}

void ADC_WaitForConversion(void) {
    // Wait until ADC conversion is done
}

uint16_t ADC_ReadData(void) {
    // Return ADC conversion result (0-1023 for 10-bit ADC)
    return 512; // Example fixed value for demonstration
}

int main(void) {
    uint16_t potValue;
    ADC_Init();

    while(1) {
        ADC_StartConversion(0); // Read from channel 0
        ADC_WaitForConversion();
        potValue = ADC_ReadData();

        printf("Potentiometer ADC Value: %u\n", potValue);

        // Add delay here if needed
    }

    return 0;
}
Output
Potentiometer ADC Value: 512 Potentiometer ADC Value: 512 Potentiometer ADC Value: 512 ...
⚠️

Common Pitfalls

  • Not initializing ADC: Forgetting to initialize the ADC module causes no readings.
  • Wrong ADC channel: Reading from a channel not connected to the potentiometer returns invalid data.
  • Not waiting for conversion: Reading ADC data before conversion completes gives incorrect values.
  • Ignoring voltage reference: ADC values depend on reference voltage; ensure it matches your system.
c
/* Wrong way: Reading ADC data immediately after start */
ADC_StartConversion(0);
uint16_t val = ADC_ReadData(); // May read old or invalid data

/* Right way: Wait for conversion before reading */
ADC_StartConversion(0);
ADC_WaitForConversion();
uint16_t val_correct = ADC_ReadData();
📊

Quick Reference

Remember these tips when reading a potentiometer with ADC:

  • Initialize ADC before use.
  • Use correct ADC channel connected to potentiometer.
  • Wait for conversion to finish before reading data.
  • Understand ADC resolution and voltage reference for accurate readings.

Key Takeaways

Always initialize the ADC module before starting conversions.
Select the correct ADC channel connected to the potentiometer.
Wait for ADC conversion to complete before reading the value.
ADC value represents the potentiometer position as a digital number proportional to voltage.
Check your microcontroller's ADC resolution and reference voltage for accurate readings.