Embedded C Program for Temperature Monitoring System
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
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace ADC_value = 512 through the code
Read ADC value
adc_value = 512
Convert ADC to temperature
temperature = (512 * 500) / 1023 = 250
Print temperature
Output: Temperature = 250°C
Check threshold
250 > 100, so print alert
Print alert
Output: Alert: Temperature too high!
| Step | ADC Value | Temperature | Output |
|---|---|---|---|
| 1 | 512 | - | - |
| 2 | 512 | 250 | - |
| 3 | 512 | 250 | Temperature = 250°C |
| 4 | 512 | 250 | Alert triggered |
| 5 | 512 | 250 | Alert: 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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple ADC read | O(1) | O(1) | Basic temperature monitoring |
| Interrupt-driven ADC | O(1) | O(1) | Efficient real-time systems |
| Digital sensor read | O(1) | O(1) | Simpler code with digital sensors |