Very simple program needed to 'control' a thermostat.

I was going to use a Basic Stamp as I have one, but opted to order an Arduino for this. It will be easier to wire I think.

I have a Chinese diesel parking heater and for those that don't know, the thermostats suck. They never actually shut the heater down and will pump the area to be heated well above their set temp. It slows their fuel rate down, but maintains a burning firebox. I want to be able to cycle the unit on and off with a thermostat.. The 'dumb' thermostats available for these heaters have seperate on/ off buttons.

I'm pretty sure, that a simple button LED test sketch which is in the IDE will work once modified.. I'm going to use a 2 relay module, and create jumper wires on the 'dumb' thermostat on/ off buttons which the relays can 'jump' so that the thermostat registers a push on or off.. Then I can use a basic battery operated home heating programmable thermostat to create the 'button' input..

This is not in format or exactly correct but my basic idea. Could format it correct in about 2 seconds with help of the IDE
If Thermostat is high, digitalwrite, onbutton high
else digitalwrite offbutton high

This will keep one button or the other 'pressed' at all times which I'm not sure if the thermostat would tolerate that.

What I would like to do is, have the buttons only 'press' for like 3 seconds and then release, and then wait until the thermostat input changes to re loop the program. This code is where I'm getting hung up, because I don't know how to implement it. I need a little help with that, and yes this is my first attempt with Arduino. I did a fair amount with Basic Stamp years ago but Arduino was where it was at and I didn't have that ( Stamp was a gift )

Hey there!

First thing I would do is make sure you know how the thermostat reacts to having a button pressed all the time. Without knowing that, you might be looking at expensive issues if you leave an uncontrollable code running on it.

Ok so let me make sure I know what you want to do. "What I would like to do is, have the buttons only 'press' for like 3 seconds and then release, and then wait until the thermostat input changes to re loop the program."

Very simply put, you could just use delay(3000); to wait 3 seconds before turning the relays off again.

The wait time you want sounds like it could be fixed if you always knew which input the thermostat was on. If the input was connected electronically, you could monitor what that was on a pin, use digitalRead, and add some code like this in your loop:

while (thermostat is not the input I want){
; // do nothing as you wait
}

Hope this helps my friend! If I didn't answer the way you are looking, ask away ;).

-Englishscone

The home thermostat operates as a basic switch between the R and W terminals of the thermostat. Closed if heat is to be on. Open if its not. It has more terminals than that. But those 2 are all i need. I would just power a basic voltage through the thermostat switch and return it to a digital io pin as digitalread.

The chinese diesel heater thermostat would always match to the home thermostat switch being open or closed according to the basic code. So i think the while and delay syntax could be used for both heat on and heat off lines.

As well as i could use a delay of say 6 mins before the arduino closed either of the relay contacts. The heater takes about 3 mins to fully warm up. So this would allow the heater to fully warm up even if the home thermostat quit calling for heat. It would also delay the heater from turning on for 6 mins.

1 Like

This is what I have so far. I borrowed code from one of the example programs and added to it. It comes up with a compiler error but I don't have the Arduino yet. I'll figure it all out when I have it.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13;
const int ledpin2 = 14// the number of the LED pin

// variables will change:

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledpin2,OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay 3000
digitalwrite(ledpin, LOW);
while (buttonState == HIGH);
} else {
// turn LED off:
delay 360000;
// 6 min delay
digitalWrite(ledPin2, HIGH);
delay 3000;
digitalWrite (ledpin2, LOW);
while (buttonState == LOW);
}

First, please use the code tags to post your code, it helps when reading from a smartphone for example.

Your code has a few errors:

const int ledpin2 =  14 <-- need a ; at the end
...
delay 3000 <-- delay(3000); (the same for 360000)
...
buttonState = digitalRead(buttonPin); <-- buttonState has to be declared: bool buttonState = digitalRead(buttonPin);
const int ledpin2 =  14// the number of the LED pin

Missing a semicolon

   delay 3000

Syntax of the delay() function wrong several times

  buttonState = digitalRead(buttonPin);

buttonState not declared in the program

   digitalwrite(ledpin, LOW);

nor is ledpin, although ledPin is
nor is ledPin2

   digitalwrite(ledPin, LOW);

Function name spelled incorrectly

There is a } missing at the end of the code

   while (buttonState == HIGH);

How will the program ever exit this while loop as the only code in it is the semicolon at the end of the line so the value of buttonState is never checked

I never checked the logic of the program so there may be other problems

I'm coding this without an Arduino to check/ test it on. Arduino arrives sometime after the holidays. supposed to be before, but. Fedex.. I will change the tags from ledpin, ledpin2 and the button to on, off and thermostat or something like that

How should I code the while loop? I just want it to press and release a button depending on if the thermostat is switched on or not. then wait to do any more button pressing until that input changes

Matt167:
I'm coding this without an Arduino to check/ test it on. Arduino arrives sometime after the holidays. supposed to be before, but. Fedex.. I will change the tags from ledpin, ledpin2 and the button to on, off and thermostat or something like that

All the errors pointed out can be fixed with no Arduino on hand. In fact, having one won't help, because you can't download the code with those errors.

Matt167:
How should I code the while loop? I just want it to press and release a button depending on if the thermostat is switched on or not. then wait to do any more button pressing until that input changes

Maybe

while (digitalRead(buttonPin));

Thanks. I understand I can't download broken code. But at least I can reference the layout and write the learning programs to better understand the language.. I have programmed a bit in basic. But C++ is new to me.. it's going to be a fun venture though. For this project and more.

Matt167:
I'm coding this without an Arduino to check/ test it on. Arduino arrives sometime after the holidays. supposed to be before, but. Fedex..

Just download the IDE from the Arduino site and then you can get the compiler to tell you about your syntax errors. You'll need to do so when your hardware arrives anyway.

I have the IDE and used it to code what I did. It's also where I got the borrowed code

Matt167:
I have the IDE and used it to code what I did. It's also where I got the borrowed code

Did you try compiling the code ?

Yeah. It points out the compiler errors

Matt167:
Yeah. It points out the compiler errors

Your next step is to correct the errors. As has been pointed out you don't need an Arduino to do this

I decided to just go back to basics. Scrubbed it and then re wrote it, and check verified at each time

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;
const int ledpin2 =  12;// the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledpin2,OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(3000);
    digitalWrite(ledPin, LOW);
  } else {
    // turn LED off:
     delay(360000);
    digitalWrite(ledpin2,HIGH);
   delay(3000);
  }   digitalWrite(ledpin2,LOW);
  while (digitalRead(buttonPin));}

[CODE]

I don't understand what you want to do in this program.

First, I hope you connected your push button with a pulldown resistor:

so that it reads HIGH when pushed.

In the loop, you first read the button.
If it is pushed, then you light the led for 3 seconds and wait until the button is released.
But if it is not pushed, which may be the case at startup, then the code will do this:

  } else {
    // turn LED off:
     delay(360000);
    digitalWrite(ledpin2,HIGH);
   delay(3000);
  }   digitalWrite(ledpin2,LOW);

The delay(360000) stops the microcontroler for 360 seconds (6 minutes), during which it will not read the button. Is it what you really want?

If not, you should consider writing a non blocking code (that does not use delays or at least no long ones), there are lots of tutorials about that here on the forum.

1 Like

I plan on copying the schematic for the 'button' example, just modified for my use. I have a kit of resistors coming, along with a breadboard, the arduino ect.. I know breadboard for prototyping, but some hot glue will hold the components in place permanently inside the enclosure. I could do a solder board later, or even design/ order a circuit board.

The reason for the 6 min delay, is I want the heater to run for at least 6 mins before the heater is switched off.. If the heater is shut down before it is fully warmed up, the fire box will soot up.. This will probably make it slightly hotter than thermostat temp but once the Arduino comes out of pause, the cycle will normalize..

After many trials, this is the only working code I was able to produce. All other variations of code I tried just caused both relays to freeze on.. However this works perfect. The Chinese heater thermostat could care less that the buttons are continuously pressed, and it takes about 6 mins alone just to get the heater up to temp, and a few mins beyond that to bring the temp up the 2-3 degrees that it takes for the home thermostat to click the heat off.. The extra code didn't even matter anyway.

I have a 10k pulldown resistor on the switch side, which is not controlled by the home type thermostat.

const int buttonPin = 2;     // the number of the pushbutton pin
const int Relay =  13;
const int Relay2 = 4;
// the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(Relay, OUTPUT);
  pinMode(Relay2, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(Relay, HIGH);
    digitalWrite(Relay2, LOW);
  } else {
    // turn LED off:
    digitalWrite(Relay2, HIGH);
    digitalWrite(Relay, LOW);
  }
}
[CODE]

I don't understand. if you allready have a thermostat then why do you need the Arduino? Why not just use the signal lead from the thermostat to directly control your relay?

If you really wanted to learn and use Arduino, Then why would you need a thermostat? You can get a temperature sensor for Arduino for a couple dollars and it will do everything a thermostat will?

I needed a programmable thermostat that is easy to use, which cost $18 and feeds the Arduino the information it needs. Either on or off.. The Arduino could do all of this for sure but it would cost a lot more than $18 in components and time. In the end I needed a working thermostat control, not so much a project

As far as using the signal from the thermostat to control the relays, it would have worked if the diesel heater thermostat control had a single mechanical on/ off button. But it has 2 buttons. I needed some amount of logic to press one button or the other. Yes I could probably have wired it to switch using just an extra relay or 2 and some careful wiring. But it was much easier to program it out.

I bought 2 Arduino's actually and I have some bigger plans for #2.