Newbie need help coding simple temperature monitor

How do i write piece of code that monitors temperature and then if the temp increases a certain amount say 10 degrees within say 4 seconds it creates an action (display an alert)?

Im making a device that alerts when temperatures increase rapidly for my pc.

any help is appreciated

Is Google broken today?

1 Like

Ive spent far to much time googling and if I had the answer i wouldn't be here, is there something i'm missing? care to enlighten me?

What have you found so far? Why is that not working for you? What specific things do you have which are different to the examples online?

well I know how to set up the temp monitor its just the significant increase of heat over a time frame im having trouble with and I haven't found an example that deals with this

So you need to remember the temperature 4 seconds ago. The simple way to do this is to only take a reading once every 4 seconds. Then you compare the current reading to the previous reading.

The more complex method is to store several readings over time. Say twice a second. Then you need to remember 7 of them to be able to refer back to the 4-seconds-ago reading. Use a ring buffer for this.

MorganS:
So you need to remember the temperature 4 seconds ago. The simple way to do this is to only take a reading once every 4 seconds. Then you compare the current reading to the previous reading.

So would i use a delay and then an if variable?

Another way to do this is with a pair of low-pass filters.

You take periodic readings at a known rate. Say, on every .1 seconds. Let's put this in a variable named t.

You have two filters t1 and t2.

t1 = t1 * .75 + t * .25;
t2 = t2 * .95 + t * .05;

Both filters will track the temperature, but t2 will track it more slowly than t1. If the difference between the two exceeds and amount, that's when you can trigger your alarm.

As to exactly what those multipliers should be, and what the divergence will be should a triggering event happen ... that's something you may need to experiment with and maybe do some math. Excel can be handy.

wa5te:
So would i use a delay and then an if variable?

If that is the ONLY thing that the Arduino is doing, then yes, use a delay. If you want it to do other things, then look at the example (on your computer) called BlinkWithoutDelay.

wa5te:
So would i use a delay and then an if variable?

Typically, we try not to use delays. It's ok for simple things, but iff you want your ardunino doing more than one thing at once, it's a problem.

// suffix names with the unit of meansure, where applicable

unsigned long lastReadingMs;
const unsigned long readingDelayMs = 4000; // 4 second s
float lastReadingDeg;

void setup() {
  lastReadingDeg = read_the_sensor_degrees();
  lastReadingMs = millis();
}

void loop() {
  if(millis() - lastReadingMs >= readingDelayMs) {
    float deg = read_the_sensor_degrees();
    lastReadingMs = millis();

    if(deg - lastReadingDeg > 4 || deg - lastReadingDeg < -4) {
      // do something here. light a lamp. Whatever.
    }

    lastReadingDeg = deg;
  }
}

float read_the_sensor_degrees() {
  // talk to hardware, convert the result to a value in degrees
}

Thanks! I still don't fully understand how this code works. Im using a TMP36 temp sensor will this make any difference to my code?
Im still trying to get my head around the language.

this is my temperature increase alert project for a pc. It works by measuring the temp increased by 110 percent and then delays 5 seconds and if the temp gets higher it triggers. Its super crude so how would you refine this?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int tempPin = A0;
int temp;
int temp2;

void setup() {
  lcd.begin(16, 2); //Initialize the 16x2 LCD
  lcd.clear();  //Clear any old data displayed on the LCD
  Serial.begin(9600);
}
  void loop() {
  
lcd.setCursor(0, 1);  

 delay(100);
 
     temp = analogRead(tempPin);
     temp = temp * 0.48828125 * 1.1;
     Serial.print (" temp ");
     Serial.print (temp);

  delay(5000);
  
     temp2 = analogRead(tempPin);
     temp2 = temp2 * 0.48828125;
     Serial.print (" temp2 ");
     Serial.print (temp2);
   
  if (temp  < temp2) lcd.print(" alert ");
  
  delay(2000);

  lcd.clear();
    }

how would you refine this?

I'd use better names. Does temp mean temporary? Or temperature?

Why temp and temp2? temp1 and temp2 make more sense to me.

I'd use consistent indenting and I'd put every { on a line by itself.

I'd get rid of all the delay()s. I'd be using the blink without delay philosophy.

I'd have comments in the code explaining why the first reading is multiplied by 1.1.

I'd do something more useful than print " alert " on the LCD.

I'd save the last reading as the previous reading, so that on any given pass through loop(), I was reading the temperature once.

I'd use a more reliable temperature sensor.

I’d suggest building your project in stages. Something like (maybe you’ve done some already):

  1. Figure out how to read your particular temperature sensor.

  2. Figure out how to read the sensor at a known rate using the “Blink without Delay” technique.

  3. Use one of the methods suggested (or create your own) to compute dT / dt.

  4. Add alarm logic to fire when the temp change rate computed in #3 exceeds your chosen limit. I imagine you’ll want to also alarm on an absolute temperature threshold and not just rate of change.