# Temperature Threshold Module

Do you want to trigger an action once the temperature crosses certain point? Then you are in need of a temperature threshold module.

## Temperature Threshold Module

In the last tutorial we took a look at temperature measurement with analog temperature modules. Not all applications require an absolute temperature value. Today we will look at temperature threshold modules, that can be used to trigger an action once a certain threshold value is crossed. To be more specific, we will take a look at the KY-028 module. While this module makes use of an NTC thermistor as well, it is not suited for measuring an absolute temperature value. The module has a potentiometer that can be used to set a temperature threshold. When the temperature rises above the threshold the digital output of the module is turned on. In our code we can react to this event and trigger an appropriate action.

In this tutorial we will build a simple temperature alarm. Once the temperature exceeds the threshold we turn on an active buzzer to warn about this temperature event. Let's get started!

## A Simple Temperature Alarm

First, we have to build up the circuit shown in the picture below. I connected an active buzzer to pin 9. To wire the KY-028 module, we need to connect the digital output D0 to a digital pin of our Arduino and the analog output A0 to an analog pin. At the beginning, we won't need the analog output. I'll show you later for what purpose it can be used. I connected the digital output to pin 8 and the analog output to pin A0. Finally, we have to connect 5V and GND to the + and G pin of the module.

What do we need to do in our code? Well, not much. The threshold is set by turning the potentiometer on the module. We don't need to take care of this in our code. The only thing that we need to do is to read the status of pin 8 and toggle the buzzer accordingly. If the temperature is above the threshold, the digital output of the module turns high. We then need to turn on the buzzer. Here is the code:

void setup() {
pinMode(8, INPUT);
pinMode(9, OUTPUT);
}

void loop() {
digitalWrite(9, HIGH);
}
else {
digitalWrite(9, LOW);
}
}

That was easy, wasn't it? Well, if you look at the result in the video below, you'll see that our solution has a problem: the output oscillates if the temperature is near the threshold. Until the temperature is clearly above the threshold, we don't hear a clear beep. We get a strange noise, because the output of the temperature threshold module toggles back and forth instead of producing a stable output level. An easy solution would be to add some delays, but we can do better. Let's see how.

## Improving the Temperature Alarm

The problem that we observed is a very common one and of course there is already a solution for it. We need to use a hysteresis. What does this mean? Well, we need separate thresholds for turning the buzzer off and on. We simply say that the sensor has to get a bit colder than our chosen threshold for turning the buzzer on before it turns off. This will prevent oscillation. Sadly, we cannot directly set this second threshold on the module, but we can implement this in software. This is the moment where analog output of the KY-028 module comes in handy. But before we can write the code, we need to understand, how to interpret the voltage on the analog output.

The NTC thermistor and the potentiometer on the KY-028 module form a voltage divider. The potentiometer is used as a variable resistor. Because we don't know the wiper position and therefore the resistance of the potentiometer, we can't calculate an absolute temperature value. For our use case, we don't need to. The voltage on the analog output decreases with rising temperature. The digital output is turned on once the voltage falls below half the supply voltage. This means the digital output is turned on when the voltage we measure on the analog output is under 2.5 V. The 2.5 V correspond to an ADC reading of 512. With this knowledge we can easily implement a hysteresis. We can pick a value slightly above 512 as our threshold for turning off the beeper. I chose 530, but you can just experiment a bit and see if you find a better fit for your use case. The code addition is quite simple: we check if the ADC reading is above 530. In case it is we turn off the buzzer. Here is the adjusted code:

void setup() {
pinMode(8, INPUT);
pinMode(9, OUTPUT);
}

void loop() {
}