We use ADC to convert sensor signals like temperature or light into numbers the microcontroller can understand.
0
0
Sensor reading through ADC (temperature, light) in Embedded C
Introduction
When you want to measure room temperature with a sensor.
When you need to detect how bright a room is using a light sensor.
When controlling a fan speed based on temperature readings.
When adjusting screen brightness automatically depending on light.
When logging environmental data for weather monitoring.
Syntax
Embedded C
int readADC(int channel); // Example function to read ADC value from a given channel
The ADC converts analog voltage to a digital number.
Each sensor connects to a specific ADC channel on the microcontroller.
Examples
Reads the ADC value from channel 0, where the temperature sensor is connected.
Embedded C
int temperatureValue = readADC(0);
Reads the ADC value from channel 1, where the light sensor is connected.
Embedded C
int lightValue = readADC(1);
Converts the ADC reading to voltage assuming 10-bit ADC and 5V reference.
Embedded C
float voltage = (readADC(0) * 5.0f) / 1023;
Sample Program
This program simulates reading ADC values from temperature and light sensors, then converts those values to voltages.
Embedded C
#include <stdio.h> // Simulated ADC read function for example int readADC(int channel) { if (channel == 0) return 512; // Simulated temperature sensor value if (channel == 1) return 256; // Simulated light sensor value return 0; } int main() { int tempADC = readADC(0); int lightADC = readADC(1); // Convert ADC to voltage (assuming 10-bit ADC and 5V reference) float tempVoltage = (tempADC * 5.0f) / 1023; float lightVoltage = (lightADC * 5.0f) / 1023; printf("Temperature sensor ADC: %d\n", tempADC); printf("Temperature sensor voltage: %.2f V\n", tempVoltage); printf("Light sensor ADC: %d\n", lightADC); printf("Light sensor voltage: %.2f V\n", lightVoltage); return 0; }
OutputSuccess
Important Notes
ADC values depend on the resolution (e.g., 10-bit means values from 0 to 1023).
Always check your microcontroller's ADC reference voltage for correct conversion.
Sensor output voltage changes with temperature or light intensity, which ADC reads as numbers.
Summary
ADC converts sensor signals into digital numbers the microcontroller can use.
Each sensor connects to a specific ADC channel to read its value.
Convert ADC readings to voltage to understand sensor output better.