0
0
Power-electronicsHow-ToBeginner · 3 min read

How to Interface Light Sensor LDR in Embedded C

To interface a light sensor LDR in embedded C, connect it as a voltage divider with a fixed resistor and read the analog voltage using the microcontroller's ADC. Then, convert the ADC value to light intensity in your code by initializing the ADC, starting conversion, and reading the result.
📐

Syntax

To read an LDR sensor, you use the microcontroller's ADC (Analog to Digital Converter) functions. The typical steps are:

  • Initialize the ADC module.
  • Select the ADC channel connected to the LDR voltage divider.
  • Start the ADC conversion.
  • Wait for conversion to complete.
  • Read the ADC value.

This value represents the light intensity as an analog voltage converted to a digital number.

c
void ADC_Init(void);
uint16_t ADC_Read(uint8_t channel);

// Example usage:
ADC_Init();
uint16_t lightValue = ADC_Read(0); // Read from ADC channel 0 connected to LDR
💻

Example

This example shows how to read an LDR sensor connected to ADC channel 0 on a generic microcontroller. It initializes the ADC, reads the sensor value, and prints it via UART.

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

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

uint16_t ADC_Read(uint8_t channel) {
    // Start ADC conversion on given channel
    // Wait for conversion complete
    // Return ADC result (0-1023 for 10-bit ADC)
    return 512; // Dummy mid value for example
}

void UART_Init(void) {
    // Initialize UART for serial output
}

void UART_Print(const char* str) {
    // Send string over UART
    printf("%s", str); // For simulation
}

int main(void) {
    ADC_Init();
    UART_Init();

    while (1) {
        uint16_t lightValue = ADC_Read(0); // Read LDR on ADC0
        char buffer[50];
        sprintf(buffer, "LDR ADC Value: %u\r\n", lightValue);
        UART_Print(buffer);
        // Add delay here in real code
        break; // Stop after one read for this example
    }

    return 0;
}
Output
LDR ADC Value: 512
⚠️

Common Pitfalls

Common mistakes when interfacing an LDR sensor include:

  • Not using a proper voltage divider with the LDR, causing incorrect voltage range.
  • Failing to initialize the ADC before reading values.
  • Reading ADC values without waiting for conversion to complete.
  • Ignoring the ADC resolution and reference voltage when converting values.

Always check your microcontroller's datasheet for ADC setup details.

c
/* Wrong way: Reading ADC without initialization and waiting */
uint16_t wrong_read() {
    // Missing ADC_Init();
    // Missing wait for conversion
    return ADC_Read(0); // May return invalid data
}

/* Right way: Initialize and wait properly */
void ADC_Init(void) {
    // Proper ADC setup
}

uint16_t right_read() {
    ADC_Init();
    // Start conversion and wait
    return ADC_Read(0);
}
📊

Quick Reference

Tips for interfacing LDR with embedded C:

  • Use a fixed resistor with LDR to form a voltage divider.
  • Connect the voltage divider output to an ADC input pin.
  • Initialize ADC before reading values.
  • Convert ADC readings to meaningful light levels if needed.
  • Use UART or other communication to output sensor data for debugging.

Key Takeaways

Connect the LDR in a voltage divider circuit to get an analog voltage for the ADC.
Always initialize and configure the ADC before reading sensor values.
Wait for ADC conversion to complete before reading the result.
Use the ADC value to estimate light intensity in your embedded program.
Check your microcontroller datasheet for ADC details and pin configuration.