Simple thermostat

JPB33

Senior Member
I'm trying to make a thermostat but not sure if I'm taking the right approach and need some help. I want to allow the temperature to drop say 10 degrees before C.1 goes high again after its switched off to stop it hunting and give some hysterisis---think thats the right word!

This is the bare bones of my code without the display etc to see if anyone can point me in the right direction

Many Thanks Peter

Code:
 symbol varA = b0
	symbol varB = b1	
		
	start:

	readtemp C.4, varA ; Temp Sensor on pin C.4
	readadc C.2, varB ; Adjust temp on pin C.2
	if varA <= varB then high C.1
	else
	low C.1
	endif
	goto start
 

BESQUEUT

Senior Member
Some improvements :
- you need some calculation to range SetPoint to the values readed on port C.2,
- you are true with hysterisis, but not your code...
- please use meaningfull symbols... (and remove unnecessary comments...)

Maybe something like :
Code:
symbol Temperature= b0
	symbol SetPoint = b1	
        symbol SP_H=b2
        symbol hysterisis=10
		
do
	readtemp C.4, Temperature
	readadc C.2, SetPoint                
        SP_H=SetPoint +hysterisis

	if Temperature<= SetPoint  then
               high C.1
	elseif  Temperature>SP_H then
	      low C.1
	endif
loop
 
Last edited:

eggdweather

Senior Member
Assuming the values you read are in deg-C then add another line
varB = varB - 10; or some value that is 10-degC
if varA <= varB then high C.1

You could name a new variable hysteresis before start:
varB = varB - hysteresis
 

JPB33

Senior Member
Thanks for the prompt replies and advice, yes its in deg C. Point taken about meaningful symbols, I'm still learning!

Tried the suggestions and Besquet, your works but switches off 10 degrees above the setpoint and I want to keep the setpoint but switch back in say 10 deg less. Tried various changes but without success, would help my understanding if you could talk me through the process its following.

Eggdweather, tried adding your suggestions but it switches at the set point rather than on at -10 deg less and adding varB = varB - hysteresis gives a syntax error.

I feel its almost there so pleased with the progress, thanks
 

BESQUEUT

Senior Member
I feel its almost there so pleased with the progress, thanks
Please, publish your code (Select code and then right click "copy for forum")
your works but switches off 10 degrees above the setpoint and I want to keep the setpoint but switch back in say 10 deg less.
???
As it is :
Switch ON when Temperature is below SetPoint
Switch OFF when Temperature is over SetPoint+Histerisis

Do you want :
Switch ON when Temperature is below SetPoint-Histerisis
Switch OFF when Temperature is over SetPoint

Or (why not) :
Switch ON when Temperature is below SetPoint-(Histerisis/2)
Switch OFF when Temperature is over SetPoint+(Histerisis/2)

NB : as you have seen, I hate the GOTO command...
 
Last edited:

fernando_g

Senior Member
If the thermostat is for human comfort controller, 10_C hysteresis is way too large.

One should make it no larger that 2_C. And in high humidity environments (where I live), where the A/C is essentially working to control relative humidity, I've found that one requires a 1_C maximum hysteresis.
 

JPB33

Senior Member
If the thermostat is for human comfort controller, 10_C hysteresis is way too large.

One should make it no larger that 2_C. And in high humidity environments (where I live), where the A/C is essentially working to control relative humidity, I've found that one requires a 1_C maximum hysteresis.
Thiink I'm misleading everyone with my 10 deg hysteresis, I only left it like that as it was easy to see when adjusting the "pot" in simulator. 1 or 2 degrees will be fine. This figure is based on a working thermostat I've constructed in PLF, as I've learned more about Picaxe I want to do it in basic.
This is my code modified as suggested by Eggweather,

Code:
      [color=Blue]symbol [/color][color=Black]varA [/color][color=DarkCyan]= [/color][color=Purple]b0
      [/color][color=Blue]symbol [/color][color=Black]varB [/color][color=DarkCyan]= [/color][color=Purple]b1  
      [/color][color=Black]varB [/color][color=DarkCyan]= [/color][color=Black]varB [/color][color=DarkCyan]- [/color][color=Navy]10 [/color][color=Black]hysteresis
            
      start:

      [/color][color=Blue]readtemp C.4[/color][color=Black], varA [/color][color=Green]; Temp Sensor on pin C.4
      [/color][color=Blue]readadc C.2[/color][color=Black], varB [/color][color=Green]; Adjust temp on pin C.2
;     if varA <= varB then high C.1
      [/color][color=Black]varB [/color][color=DarkCyan]= [/color][color=Black]varB [/color][color=DarkCyan]- [/color][color=Navy]10[/color][color=Green]; or some value that is 10-degC
      [/color][color=Blue]if [/color][color=Black]varA [/color][color=DarkCyan]<= [/color][color=Black]varB [/color][color=Blue]then high C.1
      else
      low C.1
      endif
      goto [/color][color=Black]start[/color]
Besqueut I want to :

Switch ON when Temperature is below SetPoint-Histerisis
Switch OFF when Temperature is over SetPoint

I used your code as is as using it set up like that is a new experience to me!

Thanks everyone Peter

Code:
[color=Blue]symbol [/color][color=Black]Temperature[/color][color=DarkCyan]= [/color][color=Purple]b0
      [/color][color=Blue]symbol [/color][color=Black]SetPoint [/color][color=DarkCyan]= [/color][color=Purple]b1    
        [/color][color=Blue]symbol [/color][color=Black]SP_H[/color][color=DarkCyan]=[/color][color=Purple]b2
        [/color][color=Blue]symbol [/color][color=Black]hysterisis[/color][color=DarkCyan]=[/color][color=Navy]10
            [/color]
[color=Blue]do
      readtemp C.4[/color][color=Black], Temperature
      [/color][color=Blue]readadc C.2[/color][color=Black], SetPoint                
        SP_H[/color][color=DarkCyan]=[/color][color=Black]SetPoint [/color][color=DarkCyan]+[/color][color=Black]hysterisis

      [/color][color=Blue]if [/color][color=Black]Temperature[/color][color=DarkCyan]<= [/color][color=Black]SetPoint  [/color][color=Blue]then
               high C.1
      else if  [/color][color=Black]Temperature[/color][color=DarkCyan]>[/color][color=Black]SP_H [/color][color=Blue]then
            low C.1
      endif
loop[/color]
 

eggdweather

Senior Member
I suggest you change the line in BESQUET's code:
SP_H=SetPoint + hysteresis
to:
SP_H=SetPoint - hysteresis

Then it will switch at your temperature requirement.
 

BESQUEUT

Senior Member
Besqueut I want to :

Switch ON when Temperature is below SetPoint-Histerisis
Switch OFF when Temperature is over SetPoint

I used your code as is as using it set up like that is a new experience to me!
In that case :
Code:
[color=Blue]symbol [/color][color=Black]Temperature[/color][color=DarkCyan]= [/color][color=Purple]b0[/color]
[color=Blue]symbol [/color][color=Black]SetPoint [/color][color=DarkCyan]= [/color][color=Purple]b1    [/color]
[color=Blue]symbol [/color][color=Black]SP_H[/color][color=DarkCyan]=[/color][color=Purple]b2[/color]
[color=Blue]symbol [/color][color=Black]Hysterisis[/color][color=DarkCyan]=[/color][color=Navy]2

            [/color]
[color=Blue]do
      readtemp C.4[/color][color=Black], Temperature
      [/color][color=Blue]readadc C.2[/color][color=Black], SetPoint                
      SP_H[/color][color=DarkCyan]=[/color][color=Black]SetPoint [/color][color=DarkCyan]- [/color][color=Black]Hysterisis

      [/color][color=Blue]if [/color][color=Black]Temperature [/color][color=DarkCyan]<= [/color][color=Black]SP_H  [/color][color=Blue]then
               high C.1
      else if  [/color][color=Black]Temperature [/color][color=DarkCyan]> [/color][color=Black]SetPoint [/color][color=Blue]then
            low C.1
      endif
loop[/color]
Is there anything not clear for you ?



I suggest you change the line in BESQUET's code:
SP_H=SetPoint + hysteresis
to:
SP_H=SetPoint - hysteresis

Then it will switch at your temperature requirement.
Don't forget to also swap tests...
 
Last edited:

premelec

Senior Member
@JPB33 note that in any practical situation there is a lag between calling for heat and temperature change being sensed by the thermostat - this lag can be short or long depending on thermal mass, distance between source and sense etc... have fun!
 

BESQUEUT

Senior Member
@JPB33 note that in any practical situation there is a lag between calling for heat and temperature change being sensed by the thermostat - this lag can be short or long depending on thermal mass, distance between source and sense etc... have fun!
etc... : external temperature, thermal insulation...
 

JPB33

Senior Member
Thanks everyone, I tried swapping the + to - but it didnt work for me!

Ive never allocated symbols like that always just used the variables and found them difficult to follow so the symbols seem more logical. Is there any significane in using _ or is it just a personal preference? Its making sense when Ive read it a few more time and seem logical.

The application is a water heated tray for uncapping frames of honey so lag is fine as is thermal conduction. The picaxe drives an existing SSR unit and replaces a LCD stat from 1988 which suffered display failure. The flowchart version which is few years old, works fine but I just wanted to learn how to do it in basic for the next one I construct. Been pondering about it for ages and now I know---Thanks
 
Top