Arduino CODE...

Hello everyone!

Recently I began with arduino controllers and so far I have not had any problems…
I started a project that includes a seven-day monitoring of temperature.
It is necessary that in the course of one week, " Ardiuno " monitors temp. and if the temp. for those 7 days is not more than a given it neads to turn on heater for an hour or two.
The problem is the fact that I do not know which library to use or how to write a code which will do this.
If anyone has some advice ( idea ) I’d be much appreciative !! Thank you in advance! :slight_smile: :slight_smile:

Sounds like you want to check the temp for a whole week and then turn the heater on if it was too cold any time in those 7 days? Surely that can't be right.

It doesn't sound like you need a library at all. Reading the temperature is usually just reading in a value from the sensor and then performing some simple maths to relate the reading to an actual temperature.

Turning on a heater is usually just a matter of using digitalWrite to send a signal to a relay to turn it on or off.

I need to monitor temp. whole week, and than if temp. was lover (that whole week) then 70 C it need to switch the heater on. To simplify, i have water tank and once a week I need to remove the bacteria Legionela (DHW). Bacteria dies on temperature above 70C...

My english is not wery good so I apologize

Your English is fine: you did actually explain it properly, I just didn’t believe you :wink:

Well first you need to investigate what sensor you can use in your environment, and see if it’s compatible with Arduino. Does it have to be in liquid for example? And then see if it has a library. Not all sensors need libraries; eg the LM35 just has an analog (ie variable voltage) that you read. Others may need libraries but those are often just a download away.

Then I’d also suggest you go for a real time clock (RTC) like a DS1307; easy to use and also has libraries a click away. There’s an alarm library too; that makes it easy to set things up, eg take the temp every 2 minutes or 5 hours or whatever you like. We start off with artificially setting the minumimum temp to an unreasonably high value, and then the first read replaces that. Then every time you take a new temp, if it’s lower than the minimum-so-far, it becomes the new minimum.

Running the heater is just a matter after 7 days of comparing that minimum with your allowed minimum and turning a pin high to (probably) drive a relay via a transistor like this where the heater will be on the other side of the relay.

Does this work on a “rolling” seven days, ie anytime in the last 7 days, or 7 days as in calendar week?

It looks no difficult. Just for an example HW: Arduino, temperature sesor - some kind, relay, SW: time library, something for sensor (if e.g. DS18B20 - onewire lib.), and switching relay via native Arduno function. It. is some kind of thermostat. Did you used Google, lots of project are there. You can take one or create your own then first of all you have to have clear concept. We know that once a week you want overheat the tank above 70*C. How to keep the time. Do you want the serious date or just let say counter like? Which kind of temperature sensor? HW is now important, how to install it into your water tank. I can imagine DS18B20 at this place. Relay switching is trivial. Last thing is what kind of interface you want? None, LCD, etc. Clarify this first.

@JimboZA You take over me a wind. :)

Budvar10: You take over me a wind. :)

Ok well that's a new idiom for me, but I think I know what you mean Budvar 8)

Thanks for quick responde!!

I am using lm35 sensor and it will be in coper cocoon submerged into water. Also I am not going to use any interface..I did not need. This setup need to be like counter, repeat the same proces every 7 days.

Can you give me some example how to make code that compare the temperature with set temp. and after 7 days turns relay on or not if the temp. was higher then set temp. in any given moment in that 7 days.

Hey c’mon… give it a try yourself Hefest then we’ll help I’m sure.

We are, after all, not a coding service but here are some ideas:

If you don’t want to use a clock have a look at BlinkWithoutDelay where the “blink” is when you check to see if the temperature was under the minimum allowed, and the interval would be the number of milliseconds in a week.

The beauty of BWoD is that it leaves the processor free to do other stuff, like take the temperature.

For the rest of it, at the top of the code initialise a variable say minimumTempSoFar = 200 or some high value.

Then every time through loop(), read the LM35 into a variable say currentTemp. Then if currentTemp is less than minimumTempSoFar, replace the (old) minimumTempsoFar with currentTemp.

Some days later when the “blink” fires, if minimumTempSoFar is less than 70, fire a function called runHeater() or something, in which the heater will run. You could simplify that and use delay() in there I guess, with delay of the right number of millis for an hour.

ok , thanks wery much everyone!! I will give it a try!

Perhaps an easier task that accomplishes the same result:

If the liquid temperature is above 70, note the time and turn off the heater. else if the time noted is more than 7 days in the past, turn on the heater.

This will assure that the liquid reaches 70 at least once every 7 days.

Because the timing is probably not that critical you could use millis() (not very accurate) and use a 6-day interval (6UL*24UL*60UL*60UL*1000UL) so if the clock is running slow it will still sterilize the water within 7 days.

const int HeaterRelayPin = 2;

void setup() {
  digitalWrite(HeaterRelayPin, LOW);  // Initial state
  pinMode(HeaterRelayPin, OUTPUT);

int getTemperature() {
  // Put code here to read the temperature

void loop() {
  static unsigned long LastTimeOver70 = 0;
  // If the liquid temperature is 70 or higher,
  if (getTemperature() >= 70) {
    // note the time... 
    LastTimeOver70 = millis();
    // ...and turn off the heater.
    digitalWrite(HeaterRelayPin, LOW);
  else {  // The liquid temperature is below 70
    // if the time noted is more than 6 days in the past,
    if (millis() - LastTimeOver70 > (6UL*24UL*60UL*60UL*1000UL)) {
      // turn on the heater.
      digitalWrite(HeaterRelayPin, LOW);

Of course if the heating needs to be done on a certain day and time (say, 2AM on Monday) you should use an RTC. But then what happens if the water gets up to 70 on Tuesday and then stays under 70 the rest of the week... Wouldn't that mean that the next regular 2AM Monday heating would be skipped and it would be almost 13 days under 70 before the next 2AM Monday sterilization session?

you could always use the modulo operator in a if() statement ie int time = millis(); if(time%Period==0) {digitalWrite(heaterswitch,HIGH);int waterTemp = analogRead(tempsensor); if(watertemp>70){digitalWrite(heaterswitch,LOW);};} else{digitalWrite(heaterswitch,LOW);}

where the period is set to the number of milliseconds in a week