What is Hysteresis?
Hysteresis is a phenomenon where the output of a system depends not only on its current input but also on its past inputs. It introduces a delay or lag in the system's response to changes in input, which is widely used in control systems to prevent frequent toggling.
Why Use Hysteresis?
- Stability: Reduces noise-induced oscillations in systems.
- Improved Performance: Helps maintain consistent operation.
- Practical Applications: Thermostats, motor controllers, and limit switches often use hysteresis.
How Does Hysteresis Work?
Example: Temperature Control
Consider a heater controlled by a thermostat. The system turns the heater ON when the temperature falls below 18°C and OFF when it exceeds 22°C.
Here:
- 18°C and 22°C form the hysteresis band.
- This prevents the heater from frequently toggling between ON and OFF when the temperature fluctuates near a single threshold.
Visualizing Hysteresis
Hysteresis Diagram
Below is a simple hysteresis diagram illustrating the behavior of the sample code:
/**************************************************************************************************
* Example of a hysteresis
* The hysteresis takes a percentage as an input.
* It outputs a level between 0 and 4.
*************************************************************************************************/
/* Hysteresis diagram
*
* level
* ^
* |
* 4_|. . . . . . . . . . . . . . . . . . .____________
* | | |
* | v ^
* | | |
* 3_|. . . . . . . . . . . . . .__________|__|
* | | | . .
* | v ^ . .
* | | | . .
* 2_|. . . . . . . . __________|__| . .
* | | | . . . .
* | v ^ . . . .
* | | | . . . .
* 1_|. . .___________|__| . . . .
* | | | . . . . . .
* | v ^ . . . . . .
* | | | . . . . . .
* 0_|_____|__|______________________________________| percent
* | | | | | | | | |
* 10 15 35 40 60 65 85 90 100
*/
#include <stdio.h>
#define NB_THRESHOLDS 4
// Struct to define the hysteresis thresholds and levels
struct threshold {
unsigned int low;
unsigned int high;
unsigned int level;
};
// Define thresholds
const struct threshold thresholds[NB_THRESHOLDS] = {
{.low = 10, .high = 15, .level = 1},
{.low = 35, .high = 40, .level = 2},
{.low = 60, .high = 65, .level = 3},
{.low = 85, .high = 90, .level = 4},
};
// Function to calculate the hysteresis level based on input percentage
unsigned int get_hysteresis_level(unsigned int percentage, unsigned int *current_level) {
for (int i = 0; i < NB_THRESHOLDS; i++) {
if (percentage >= thresholds[i].low && percentage <= thresholds[i].high) {
*current_level = thresholds[i].level;
return *current_level;
}
}
// If outside all thresholds, return the previous level
return *current_level;
}
int main() {
unsigned int current_level = 0; // Initial level
unsigned int percentages[] = {5, 12, 14, 20, 36, 39, 45, 61, 63, 70, 86, 88, 95};
size_t size = sizeof(percentages) / sizeof(percentages[0]);
printf("Percentage | Current Level\n");
printf("--------------------------\n");
for (size_t i = 0; i < size; i++) {
unsigned int new_level = get_hysteresis_level(percentages[i], ¤t_level);
printf(" %3u | %u\n", percentages[i], new_level);
}
return 0;
}
Explanation of the Code
- Struct for Thresholds:
struct thresholddefines a range (lowtohigh) and an associatedlevel.
- Threshold Definition:
thresholds[]defines the hysteresis bands and corresponding levels.
- Hysteresis Logic:
- The
get_hysteresis_levelfunction checks if the input percentage falls within any of the defined ranges. - If so, it updates the
current_levelto the appropriatelevel. - If the input percentage is outside all ranges, the function retains the previous
current_level.
- Simulation:
- The
percentages[]array provides a set of sample inputs to demonstrate the hysteresis behavior. - The
mainfunction iterates through these inputs, calculates the new level, and displays it.
Output Example
Percentage | Current Level
5 | 0
12 | 1
14 | 1
20 | 1
36 | 2
39 | 2
45 | 2
61 | 3
63 | 3
70 | 3
86 | 4
88 | 4
95 | 4
This output demonstrates how the hysteresis mechanism smoothly transitions between levels without abrupt changes, maintaining stability.
Conclusion
Hysteresis is a simple yet powerful concept to add stability to systems prone to rapid fluctuations. By implementing a hysteresis band in control systems, you can prevent unnecessary toggling, improve performance, and reduce wear on hardware.
Practical Applications
- HVAC Systems: Maintain comfortable room temperature.
- Motor Control: Prevent unnecessary motor toggling.
- Limit Switches: Add robustness to mechanical systems.