0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program for Temperature Monitoring System

An Embedded C program for temperature monitoring reads sensor data using ADC, converts it to temperature, and displays or triggers alerts using printf or output pins; for example, int temp = (ADC_value * 500) / 1023; converts ADC to temperature in Celsius.
📋

Examples

InputADC_value = 512
OutputTemperature = 250°C
InputADC_value = 1023
OutputTemperature = 500°C
InputADC_value = 0
OutputTemperature = 0°C
🧠

How to Think About It

To build a temperature monitoring system, first read the analog voltage from a temperature sensor using the ADC module. Then convert this ADC value to a temperature using the sensor's scale. Finally, display the temperature or trigger an alert if it crosses a threshold.
📐

Algorithm

1
Initialize ADC and serial communication
2
Read analog value from temperature sensor
3
Convert ADC value to temperature in Celsius
4
Display temperature value
5
Check if temperature exceeds threshold and alert if needed
6
Repeat the process continuously
💻

Code

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

// Simulated ADC read function
uint16_t read_ADC() {
    return 512; // Example ADC value
}

int main() {
    uint16_t adc_value = read_ADC();
    int temperature = (adc_value * 500) / 1023; // Convert ADC to temperature (0-500°C)
    printf("Temperature = %d°C\n", temperature);
    if (temperature > 100) {
        printf("Alert: Temperature too high!\n");
    }
    return 0;
}
Output
Temperature = 250°C Alert: Temperature too high!
🔍

Dry Run

Let's trace ADC_value = 512 through the code

1

Read ADC value

adc_value = 512

2

Convert ADC to temperature

temperature = (512 * 500) / 1023 = 250

3

Print temperature

Output: Temperature = 250°C

4

Check threshold

250 > 100, so print alert

5

Print alert

Output: Alert: Temperature too high!

StepADC ValueTemperatureOutput
1512--
2512250-
3512250Temperature = 250°C
4512250Alert triggered
5512250Alert: Temperature too high!
💡

Why This Works

Step 1: Reading ADC value

The program reads the analog voltage from the temperature sensor using the ADC, which gives a digital value between 0 and 1023.

Step 2: Converting ADC to temperature

The ADC value is scaled to temperature by multiplying by 500 (max temp) and dividing by 1023 (max ADC value), giving temperature in Celsius.

Step 3: Displaying and alerting

The temperature is printed, and if it exceeds 100°C, an alert message is shown to warn about high temperature.

🔄

Alternative Approaches

Using interrupt-driven ADC reading
embedded_c
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

volatile uint16_t adc_value = 0;
bool adc_ready = false;

void ADC_ISR() {
    adc_value = 600; // Simulated interrupt ADC read
    adc_ready = true;
}

int main() {
    // Simulate ADC interrupt
    ADC_ISR();
    if (adc_ready) {
        int temperature = (adc_value * 500) / 1023;
        printf("Temperature = %d°C\n", temperature);
        if (temperature > 100) {
            printf("Alert: Temperature too high!\n");
        }
    }
    return 0;
}
This method uses interrupts for ADC reading, which is efficient but more complex to implement.
Using a sensor with digital output (e.g., I2C)
embedded_c
#include <stdio.h>

int read_temperature_sensor() {
    return 75; // Simulated digital sensor reading in Celsius
}

int main() {
    int temperature = read_temperature_sensor();
    printf("Temperature = %d°C\n", temperature);
    if (temperature > 100) {
        printf("Alert: Temperature too high!\n");
    }
    return 0;
}
This approach reads temperature directly from a digital sensor, simplifying conversion but requiring sensor-specific code.

Complexity: O(1) time, O(1) space

Time Complexity

The program runs in constant time since it performs a fixed number of operations per reading without loops.

Space Complexity

Uses constant space for variables; no dynamic memory allocation is needed.

Which Approach is Fastest?

Direct ADC reading with simple conversion is fastest; interrupt-driven or digital sensor methods add complexity but can improve responsiveness or accuracy.

ApproachTimeSpaceBest For
Simple ADC readO(1)O(1)Basic temperature monitoring
Interrupt-driven ADCO(1)O(1)Efficient real-time systems
Digital sensor readO(1)O(1)Simpler code with digital sensors
💡
Always calibrate your sensor readings to match real temperature values for accurate monitoring.
⚠️
Beginners often forget to scale the ADC value correctly, leading to wrong temperature calculations.