Simple Thermostat Inspiration Needed!

JPB33

Senior Member
Hi,

I want to build a picaxe thermostat to drive a heater for a wax pot. The existing 25 year old LCD display in the controller had died but the SSR which it drove is ok so can be re-used. Got this running in simulation switching on/off fine but ideally I would like to have a degree of variable hysteris (not sure of the word, brain dead!) so I could set it to stop at 60c and come on again when the temp drops say 5 degrees. Was going to use 2 pots for the setpoints, viewing the setpoints with an interupt routine on C2 and drive the SSR via an existing transistor from B0.

Been dabbling with this for ages on and off, looked at the code snippets but nothing really fits. Sorry its a PLF with all its limitations, but its the best I can do! Anyone solved a simlar problem?

Thanks Peter
 

Attachments

jims

Senior Member
I've used this approach successfully. Jims
Code:
[color=Green]'*************************************************
'* 20m2 Controls temperature "C" between the 
'* set points (low_temp) and (low_+temp + offset).
'*************************************************[/color]

[color=Blue]symbol temp[/color][color=DarkCyan]=[/color][color=Blue]B.6   [/color][color=Green]'DS18B20 probe[/color]
[color=Blue]symbol [/color][color=Purple]hi_temp[/color][color=DarkCyan]=[/color][color=Purple]b11[/color]
[color=Blue]symbol [/color][color=Purple]low_temp[/color][color=DarkCyan]=[/color][color=Purple]b12[/color]
[color=Blue]symbol ry3[/color][color=DarkCyan]=[/color][color=Blue]B.3    [/color][color=Green]'heater[/color]
[color=Blue]symbol ry4[/color][color=DarkCyan]=[/color][color=Blue]B.2
symbol [/color][color=Purple]offset[/color][color=DarkCyan]=[/color][color=Purple]b10[/color]
[color=Blue]symbol [/color][color=Purple]dstempc[/color][color=DarkCyan]=[/color][color=Purple]b6 [/color][color=Green]'temp from ds18b20[/color]
[color=Navy]#picaxe [/color][color=Black]20m2
main:
 
      [/color][color=Blue]let [/color][color=Purple]low_temp[/color][color=DarkCyan]=[/color][color=Navy]20   [/color][color=Green]'Set low temp set point.
       
      [/color][color=Blue]readtemp temp[/color][color=Black],[/color][color=Purple]dstempc
      
      [/color][color=Green]'*Turn heater(RY3) ON when temperature reaches low temperature set point.
      '* Heater stays ON until temperature reaches the low temp set point
      '* plus the offset.
 
      [/color][color=Blue]let [/color][color=Purple]offset[/color][color=DarkCyan]= [/color][color=Purple]low_temp [/color][color=DarkCyan]+[/color][color=Navy]4 [/color][color=Green]'Set value for offset.      

      [/color][color=Blue]if [/color][color=Purple]dstempc[/color][color=DarkCyan]=<[/color][color=Purple]low_temp [/color][color=Blue]then high ry3[/color][color=Black]:[/color][color=Blue]endif
      if [/color][color=Purple]dstempc[/color][color=DarkCyan]=>[/color][color=Purple]offset [/color][color=Blue]then low ry3[/color][color=Black]:[/color][color=Blue]endif
 
      pause [/color][color=Navy]1000
      [/color][color=Blue]goto [/color][color=Black]main[/color]
 

Goeytex

Senior Member
Very basic on/off control.

Note that the actual temperature will coast above the setpoint of 65. The amount depending upon heater power, thermal mass, ambient temperature, insulation, etc.

Code:
symbol temperature = B1
symbol heater = B.7
symbol sensor = C.1


do
   readtemp sensor,temperature    '// sensor on Pin c.1
   
   if temperature <= 55 then      '// turn heater on
      high heater      
   endif
   
   if temperature >= 60 then     '// turn heater off
      low heater
   endif
   
  pause 1000 
  
loop
 

lbenson

Senior Member
For overrun and underrun adjustment, perhaps something like this:
Code:
symbol on_setpoint  = b20
symbol off_setpoint = b21
symbol temperature = b22

symbol eeprom_on_setpoint  = 0 ; eeprom address where "on" setpoint is saved
symbol eeprom_off_setpoint = 1

symbol initial_on_setpoint  = 55
symbol initial_off_setpoint = 60
symbol maxTemp = 65
symbol minTemp = 50

read eeprom_on_setpoint,on_setpoint,off_setpoint

if on_setpoint = 0 then ' first time
  on_setpoint = initial_on_setpoint
  off_setpoint = initial_off_setpoint
  write 0, initial_on_setpoint, initial_on_setpoint ' save setpoints
endif

main:
  do

    readtemp sensor,temperature    '// sensor on Pin c.1
   
    if temperature <= on_setpoint and bHeater0n = 0 then      '// turn heater on
      high heater
      bHeaterOn=1
      bAdjusted=0
    endif
   
    if temperature >= off_setpoint and bHeaterOn = 1 then     '// turn heater off
      low heater
      bHeaterOn=0
      bAdjusted=0
    endif
    
    if temperature > maxTemp and bAdjusted = 0 and off_setpoint > on_setpoint then
      dec off_setpoint
      bAdjusted=1
      write eeprom_off_setpoint, off_setpoint
      sertxd("New off setpoint: ",#off_setpoint,cr,lf)
    endif

    if temperature < minTemp and bAdjusted = 0 and on_setpoint < off_setpoint then
      inc on_setpoint
      bAdjusted=1
      write eeprom_on_setpoint, on_setpoint
      sertxd("New on setpoint: ",#on_setpoint,cr,lf)
    endif

  pause 1000
  loop
On and off temperature setpoints are saved in eeprom for future use; if those values are "0" when read at beginning of program, they are set to the desired initial setpoints (e.g., 55 for on and 60 for off).

For each pass through the loop, if the temperature is at or below the "on" setpoint, the heater is turned on, if at or above the "off" setpoint, the heater is turned off. In addition, if the temperature goes above a specified maximum, the off setpoint is reduced by a degree (only once per on/off cycle, and only if greater than the on setpoint). If the temperature goes below a specified minimum, the on setpoint is increased by a degree. Setpoint changes are written to eeprom and are reported with sertxd.

Untested. This should, of course, be monitored during use for some time. No tests are made to assure that the on and off setpoints don't become the same. This might be unlikely, but if it could happen, then code should be added to prevent it.
 

nekomatic

Member
A PICAXE should be quite capable of doing proportional control in this sort of application. In a loop, measure your temperature, if it's below setpoint then subtract the actual temperature from the setpoint, and if the difference is less than your chosen 'proportional band' - you might try somewhere between 5 and 10 C to start off with - pulse the output on for a fraction of your chosen time period, which if you have an SSR can be quite short, say a couple of seconds, and off for the remainder. If the actual temp is above setpoint keep the SSR off and if it's below (setpoint - proportional band) turn it full on. For a straightforward load you should be able to get pretty good, stable control like this. The stable temperature will always be a little below the setpoint, but if your desired setpoint is always the same you can figure out a suitable offset to add to get the temperature you want. Reducing the proportional band will reduce the size of the difference and give quicker warmup, but also more chance of overshoot and temperature oscillations - if you see those, increase the band again.

The next step would be to add an integral term to bring the temperature exactly up to the setpoint, but for this application it doesn't sound like that should be necessary.
 

JPB33

Senior Member
Many thanks for the thoughtful replies and reasoned discussion, its a geat help. I like the idea of proportional control but for one of my applications which is a cabinet with fans for warming honey, the switching when nearing the set point was not good for them when I experimented previously with a PID controller using a thermocouple. The beeswax melting pot of course would be ideal for this.
After more thought I have now modified my attached PLF to use two procedures and I can set the dead band to what I want on simulation so I think the next stage is to build it.
Not sure if its possible to add prortional control within the limitations of the flow chart and my knowledge though!
Thanks again.
 

Attachments

Puuhaaja

Senior Member
Nice project! I am sure this can be done easily with Picaxe. I have just made quite much similar system which is controlling paint sprayer's paint temperature2014-09-26 16.44.24.jpg
In my paint warmer there's ds18b20 temp sensor and potentiometer which adjust the paint temperature. There's 230 volt dim switch for hotplate and picaxe is controlling that with led. Simple and workin technic.
 
Last edited:

The bear

Senior Member
@Puuhaaja,
I'm very interested in your paint heater, can you give me a clue, as to how the potentiometer sets the desired temperature?
Regards, Bear..
 

JPB33

Senior Member
Yes looks great, a similar appliaction to mine, did you make the pcb as well?

Would like to able to read the display in fahrenheiht but dont really understand the conversion process as yet, perhaps it only works in basic or can the flowchart cope as well?
 

Puuhaaja

Senior Member
@Puuhaaja,
I'm very interested in your paint heater, can you give me a clue, as to how the potentiometer sets the desired temperature?
Regards, Bear..
Potentiometer can give values between 0-255 when using readadc commands. Paint temperature can be between 10-60 celcius. So..Picaxe divide potentiometer value by 5 and then add 10 for that amount. For example 255/5 + 10 = 61 celcius which is max paint temperature and minimum temperature is 0/5 + 10 = 10

Here's the code how it works

readadc b.1, b1 ' read potentiometer value
let b1 = b1/5 + 10 'calculate paint desired temperature
In addition there is an isolated ds18b20 temp sensor which read paint temperature. If paint temperature is lower than desired then Picaxe will switch heat on. I have contacted a led with dim switch. When led is on(paint is warm) it's bright and dim switch is off. when paint need to be heated then led is off and dim switch is on.

This is the dim switch that i'm using: https://translate.google.com/translate?sl=fi&tl=en&js=y&prev=_t&hl=fi&ie=UTF-8&u=http://www.clasohlson.com/fi/H%C3%A4m%C3%A4r%C3%A4kytkin/36-4488&edit-text=&act=url

That dim switch will control the heat/cooking plate. It's worth mentioning that there need to be water bowl between paint can and heat plate. Otherwise paint will burn.

@ JPB33. I made that pcb originally for my small robot project. I think that conversion between celcius and fahrenheit can be done with flowchart using different math functions. If I remember right there was option to see flowchart's basic program. You can copy that basic program and send it here forum so that some of us members can made concersion.
 

Goeytex

Senior Member
Potentiometer can give values between 0-255 when using readadc commands. Paint temperature can be between 10-60 celcius. So..Picaxe divide potentiometer value by 5 and then add 10 for that amount. For example 255/5 + 10 = 61 celcius which is max paint temperature and minimum temperature is 0/5 + 10 = 10
To explain this a bit further:

This is called mapping or scaling a range of values. In Puuhaja's example, a range of 0 - 255 was mapped to a range of 20 to 60. But how is the math done?

One formula is: output_val = (input_val - input_min) * (output_max - output_min) / (input_max - input_min) + output_min

In this case the input value is the value returned by readadc. So, for example, if you need the pot to set the temp range from 40 to 80 Celsius then ....

output_Value = (adc_value - 0) * (80 - 40) / ( 255 - 0) + 40 or
output_value = adc_value * 40 / 255 + 40

Simple code would then be:

readadc c.1, adc_val
setpoint = adc_val * 40 / 255 + 40
 

JPB33

Senior Member
Thanks for the replies, very useful as I am only a beginner hence flowcharts. Tried adding the restricted range and a fahrenhiet conversion but cannot make it work in flowchart, perhaps someone can advise if its possible?

Converted my flowchart into basic and it still ran to my surprise ( sure it looks awful!) and I can see the readac, would I need to alter every entry to restrict range?

Also found a basic fahrenheit conversion but the first line says "readtemp DS18B20_Pin, w1 ; read value into w1" whereas my program says "readtemp C.0, varA" Both work but why the difference? Sorry if i'm asking the obvious!

Thanks Peter
 

Attachments

Goeytex

Senior Member
Hi Peter.

I doubt that you will get a whole lot of help here using flowcharts. Very few here use flowcharts because of the limitations and quite frankly very few know how to use it or want to know how to use it, myself included. Therefore the support will be minimal.

The flowcharting software assigns non-intuitive generic variables. And yes, the converted BASIC looks awful, making the BASIC code difficult to follow. Beginner or not, you will be doing yourself a favor by learning & using normal BASIC instead of flowcharting. If I were teaching programming to beginners, flowcharting would not get more than a casual mention, if even that.
 

Puuhaaja

Senior Member
Also found a basic fahrenheit conversion but the first line says "readtemp DS18B20_Pin, w1 ; read value into w1" whereas my program says "readtemp C.0, varA" Both work but why the difference? Sorry if i'm asking the obvious!
You can read temperature nearly every pin from your microcontroller code can looks different if you use symbols

symbol DS18B20_Pin = C.0
main:
readtemp DS18B20_Pin, w1
goto main
is similar than
main:
readtemp c.0, w1
goto main

@Goeytex
Good example of scaling and it brings memories when I need to sit in math lessons. My example was really simple and it's better to learn better ways how to scale numbers. Anyways I need to test better scaling methods because I will make a pwm tester for a small dc motor.

@JPB33
Flowchart can be good for first timer. I also started with flowchart. The faster you change it to basic the better. Real basic is much more faster to write and more flexible. Try and test it.
 

JPB33

Senior Member
Hi Goeytex, Good point taken and I am sure you are right. But like Puuhaaja, flowcharts at least got me going and I have produced a few projects with help from the forum over the last couple of years that I couldnt have any other way way. These have given me great satisfaction and are still working so perhaps its a sort of backwards learning approach. My self and good friend the Bear (We live within shouting distance!) have often spoken about training but failed to find anything so far. A few hours face to face would go a long way. Being involved as a hobby only has limitations but its geat fun trying anyway and nothing ventured nothing gained even if you cringe and think twice before asking questions!

Peter
 

geoff07

Senior Member
If I were teaching programming to beginners, flowcharting would not get more than a casual mention, if even that
These days, programming for beginners tends to mean a BeeBot for the tinies, and then Scratch. The Code Club I run for 10 year olds starts with Scratch and then moves on to html and python. You might be surprised at what they can do.

Flowcharts are a way of getting kids studying for GCSE exams (15 and 16 year olds) with no programming background and no time to learn a language to program one chip as part of the GCSE Design Technology electronics thing. Once the new generation that is starting to learn to program now, from age 5 (following the new UK curriculum where they all have to learn to code from 5), has got to the GCSE stage they won't be using flowcharts.
 

The bear

Senior Member
@Puuhaaja,
Thank you for the information, I'm not going to heat paint, I just want to learn about temperature control.

@Goeytex,
Thank you for the further explanation of mapping or scaling.
Regards, Bear..
 

fernando_g

Senior Member
Would like to able to read the display in fahrenheiht but dont really understand the conversion process as yet, perhaps it only works in basic or can the flowchart cope as well?
I'm completely for the metric system. Having said that, I like the F scale for ambient temperature best.

If you want an ultra simple way of reading degrees F, get yourself a LM34 Farenheit temp sensor. Use the READADC10 command, and divide the word variable by two. The results will now be directly in degrees F...As simple as that!

For most accurate results, use a variable regulator like the LM317 for the Picaxe's supply. Adjust for an output of 5.12 volts, and that is it.
 

nekomatic

Member
Also found a basic fahrenheit conversion but the first line says "readtemp DS18B20_Pin, w1 ; read value into w1" whereas my program says "readtemp C.0, varA" Both work but why the difference? Sorry if i'm asking the obvious!
C.0 refers directly to a PICAXE pin and w1 refers directly to a word variable - see the manual if you're not clear on either of those.

'DS18B20_Pin' and 'varA' will be names that have been defined earlier on in the program, using the SYMBOL keyword, to refer to a pin and a variable respectively. It's just a way of making programs more comprehensible by using names that describe what they're for (although 'varA' isn't much of an improvement on w1).
 

JPB33

Senior Member
Thanks all for your help, at last made progress. I saw the LM34 previously but I have around 15 D18B20 sensors in stock so I think that I will have to use them up first! Agree that Farenheit is useful for background temp but thats about it.
Got a conversion to work using attached example. Ideally I would like to able to switch between F & C is there any easy way to do this?

Read a lot, by looking at code explorer when its running presume they are grouped? ie W0 covers b0, b1 and var a & b etc? So much learning to do!
 

Attachments

fernando_g

Senior Member
Instead of doing:
varA = varA * 9 / 5 + 32

where you overwrite the Centigrade value, do the following:
varB = varA * 9 / 5 + 32

Then you have both.

Another tip, in the symbol definitions I would label the variable something more descriptive, like Temp_C or Temp_F.
 
Top