control-on-off-histeresis

On/Off Control with Hysteresis

  • 4 min

The On/Off control, also known as two-position control or bang-bang control, is a strategy that can only command to turn on or off depending on whether the measured variable is above or below the setpoint.

It is, by far, the most widespread method in simple domestic and industrial applications: from your home thermostat to a fish tank heater or a refrigerator compressor. However, its simplicity is a double-edged sword that, if not managed well, can mechanically destroy our actuators in a matter of minutes.

The Pure On/Off Control Logic

As we saw in the previous article, to close the loop we need to calculate the Error. In pure On/Off control, the logic is binary and admits no middle ground.

If we define the error as e(t) = Setpoint - PV, the controller output u(t) would be:

That is, if we lack temperature, we turn the heater on at 100%. If we exceed it (even by 0.0001 degrees), we turn it off completely.

The Problem: Chattering

In an ideal world, this wouldn’t work. In the real world, sensors have noise. If we are trying to maintain 25.0 °C and the sensor oscillates due to electronic noise between 24.99 and 25.01 °C, the controller will command the actuator to switch every time the measurement crosses the setpoint.

This is known as chattering. If you use a mechanical relay, you will hear rapid clicking and significantly reduce its lifespan: the contacts can be damaged by the electrical arc and the mechanism by fatigue.

The Solution: Hysteresis

To avoid this disaster, we introduce a hysteresis band or differential. Instead of having a single switching point, we create two: one to turn on and another to turn off.

Hysteresis is the tendency of a system to conserve its properties in the absence of the stimulus that generated them. In control, we use it to create a “comfort zone” where the actuator does not change state.

The logic now becomes something like this:

  1. Turn on if the temperature falls below setpoint - hysteresis.
  2. Turn off if the temperature rises above setpoint + hysteresis.

Thus, if the system is at 25.0 °C with a hysteresis half-band of 0.5 °C, the heater will not turn on until it drops below 24.5 °C and will not turn off until it reaches 25.5 °C. The full band between thresholds is therefore 1 °C. We have sacrificed precision to protect our hardware.

Code Implementation with C++

Implementing this on a microcontroller is trivial, but we must be careful with the if-else structure so that it maintains the state correctly.

const float setpoint = 25.0;
const float hysteresis = 0.5;
bool relay_state = false;

void loop() {
    float current_temperature = read_sensor();

    if (current_temperature < (setpoint - hysteresis)) {
        relay_state = true;  // Force on
    } 
    else if (current_temperature > (setpoint + hysteresis)) {
        relay_state = false; // Force off
    }
    
    // If the temperature is "in the middle", relay_state does not change.
    // This is what produces the memory effect of hysteresis.
    digitalWrite(RELAY_PIN, relay_state ? HIGH : LOW);
}
Copied!

Interactive Hysteresis Simulator

To fully grasp the concept, we have prepared this simulator. You can adjust the setpoint (the desired temperature) and the hysteresis width.

Observe how increasing the hysteresis makes the relay switch far fewer times (the vertical lines in the «State» graph), but in return the actual temperature oscillates further from our target. That is the trade-off we need to tune.

Advantages and Limitations of the On/Off Model

As we have seen, the On/Off control with hysteresis is:

  • Pros: Extremely robust, easy to understand and program. It does not require complex mathematical calculations.
  • Cons: In typical applications, the variable oscillates between thresholds while the actuator switches. It does not maintain an exact value at the setpoint, but moves around it.

If your application allows this oscillation (like a room radiator), perfect. If you need precision or much smaller variation (like a drone maintaining altitude), On/Off control will not work. You need something that can “modulate” the power.