TempGuard
GitHub Repository :
TempGuard is a sophisticated temperature control system designed for industrial heating applications. It employs a unique approach by utilizing an IRFZ44N MOSFET as a temperature sensor, enabling precise temperature measurements. This innovative system, driven by a BluePill microcontroller (STM32F103C8T6), ensures heating elements operate within desired temperature ranges. Additionally, it features a user interface for seamless adjustments.
Key Features
- Overheating Prevention: TempGuard prevents heating elements from overheating and potential damage, thus extending their operational lifespan.
- Energy Efficiency: By maintaining an optimal temperature range, the system reduces energy consumption, ultimately lowering electricity bills.
- Uniform Heating: Ensures consistent temperature levels, eliminating issues associated with uneven heating, and thereby enhancing industrial processes.
- Versatility: TempGuard’s adaptability extends beyond industrial heating systems, making it suitable for a wide range of applications requiring precise temperature control.
- Robust Performance: Engineered to function reliably under extreme temperature conditions, both in hot and cold environments.
Technology Used
- IRFZ44N MOSFET as Temperature Sensor: This unconventional choice allows for accurate temperature measurements, enhancing the control of heating elements.
- BluePill Microcontroller (STM32F103C8T6): Responsible for crucial tasks including measuring MOSFET temperature values, controlling the relay board, and obtaining user-defined temperature settings.
- Relay System: Vital for regulating the heating element based on the measured temperature values.
- User Interface with Switch: Enables users to toggle between two predefined temperature settings, adding flexibility to the system.
Main Code:
#include <Arduino.h>
const int relayPin = PC13;
const int temperaturePin = PA5;
const int lowThresholdPotPin = PA0;
const int highThresholdPotPin = PA1;
const int switchPin = PC14; // Switch input pin
const int redLedPin = PB7; // PWM output pin for red
const int greenLedPin = PB6; // PWM output pin for green
const int pwmFrequency = 1000; // PWM frequency in Hz
int lowThreshold = 0;
int highThreshold = 0;
unsigned long previousMillis = 0;
int brightness = 0;
int fadeAmount = 5; // Rate of brightness change
unsigned long delayInterval = 1000; // Delay interval in milliseconds
const int numReadings = 20; // Number of temperature readings for averaging
int temperatureReadings[numReadings]; // Array to store temperature readings
int lowThresholdReadings[numReadings]; // Array to store lowThresholdPot readings
int highThresholdReadings[numReadings]; // Array to store highThresholdPot readings
int currentReading = 0; // Index of the current reading
int averageTemperature = 0; // Average temperature value
int averageLowThreshold = 0; // Average lowThresholdPot value
int averageHighThreshold = 0; // Average highThresholdPot value
bool switchState = false; // State of the switch
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Set relay pin to LOW using internal pulldown resistor
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(switchPin, INPUT_PULLDOWN); // Set switch pin as input with internal pull-down resistor
pinMode(temperaturePin, INPUT_ANALOG);
pinMode(lowThresholdPotPin, INPUT_ANALOG);
pinMode(highThresholdPotPin, INPUT_ANALOG);
Serial.begin(9600);
}
void loop() {
// Current time
unsigned long currentMillis = millis();
// Check if the delay interval has passed
if (currentMillis - previousMillis >= delayInterval) {
previousMillis = currentMillis; // Update the previous time
// Read analog values
int temperatureValue = analogRead(temperaturePin);
// Store the current readings in the arrays
temperatureReadings[currentReading] = temperatureValue;
lowThresholdReadings[currentReading] = analogRead(lowThresholdPotPin);
highThresholdReadings[currentReading] = analogRead(highThresholdPotPin);
// Move to the next reading index
currentReading++;
if (currentReading >= numReadings) {
currentReading = 0; // Reset the reading index
}
// Calculate the average temperature
averageTemperature = 0;
for (int i = 0; i < numReadings; i++) {
averageTemperature += temperatureReadings[i];
}
averageTemperature /= numReadings;
// Calculate the average lowThresholdPot value
averageLowThreshold = 0;
for (int i = 0; i < numReadings; i++) {
averageLowThreshold += lowThresholdReadings[i];
}
averageLowThreshold /= numReadings;
// Calculate the average highThresholdPot value
averageHighThreshold = 0;
for (int i = 0; i < numReadings; i++) {
averageHighThreshold += highThresholdReadings[i];
}
averageHighThreshold /= numReadings;
int mappedTemperature = map(averageTemperature, 0, 4095, 0, 100);
lowThreshold = map(averageLowThreshold, 0, 4095, 60, 100);
switchState = digitalRead(switchPin);
if (switchState) {
// Switch is in the high state, increment highThreshold by 4 points
highThreshold = map(averageHighThreshold, 0, 4095, 75, 95) + 4;
} else {
// Switch is in the low state, use highThresholdPot value as it is
highThreshold = map(averageHighThreshold, 0, 4095, 75, 95);
}
// Print values
Serial.print("Temperature Value: ");
Serial.print(mappedTemperature);
Serial.print("\tLow Threshold: ");
Serial.print(lowThreshold);
Serial.print("\tHigh Threshold: ");
Serial.print(highThreshold);
// Compare values and control relay
if (mappedTemperature > highThreshold) {
digitalWrite(relayPin, HIGH); // Turn off the relay
fadeAmount = -15; // Decrease brightness
Serial.println("\tRelay OFF");
} else if (mappedTemperature <= lowThreshold) {
digitalWrite(relayPin, LOW); // Turn on the relay
fadeAmount = 15; // Increase brightness
Serial.println("\tRelay ON");
} else {
//digitalWrite(relayPin, LOW); // Set relay pin to LOW using internal pulldown resistor
Serial.println("\t");
}
// Update LED brightness
brightness += fadeAmount;
if (brightness <= 0 || brightness >= 255) {
// Reverse the fade direction
fadeAmount = -fadeAmount;
}
if (mappedTemperature >= lowThreshold && mappedTemperature <= highThreshold) {
// Adjust brightness levels based on temperature
int brightnessRed = map(mappedTemperature, lowThreshold, highThreshold, 0, 255);
int brightnessGreen = map(mappedTemperature, lowThreshold, highThreshold, 255, 0);
analogWrite(redLedPin, brightnessRed);
analogWrite(greenLedPin, brightnessGreen);
} else {
// Use default brightness
analogWrite(redLedPin, brightness);
analogWrite(greenLedPin, 255 - brightness);
}
}
}
Struggles Faced during Development:
- Analog Prototype with OP-AMP (LM358): The initial version of the prototype relied on an analog design, utilizing an LM358 OP-AMP in hysteresis mode. However, this approach proved sensitive to Electromagnetic Interference (EMI) during field tests.
- EMI Sensitivity: The analog design was susceptible to EMI, especially from electrical noise in the grid. This led to erratic switching of temperature set values, making the system unreliable.
- Transition to Digital Design: To address the EMI sensitivity issue, a significant shift was made towards a digital approach. This involved integrating the BluePill microcontroller (STM32F103C8T6) to bring stability and robustness to the system’s operation.





