This snippet comes from code I did for a Fuel Vaporizer. The Picaxe controls an "EGR Valve" to keep the temperature at about 450F inside a heat exchanger. I modified this code for general use by removing the specific temperature sensor stuff and replaced it with a Pot to simulate a temperature sensor.
I would not recommend using a DS18B20 for accurate proportional temperature control because the sampling rate is too slow. We used a K type thermocouple along with an Adafruit Max31855 Thermocouple Amplifier in our project. The sample rate was 10 samples per second in the final code, but I think that 5 samples per second should work fine for most applications.
I would not recommend using a DS18B20 for accurate proportional temperature control because the sampling rate is too slow. We used a K type thermocouple along with an Adafruit Max31855 Thermocouple Amplifier in our project. The sample rate was 10 samples per second in the final code, but I think that 5 samples per second should work fine for most applications.
Code:
[COLOR="#008000"]'**************************************************************************************
'Proportional Control algorithm / code by for HOT Gas Valve in Fuel Vaporizer System
'With modification of the constants , this can also be used for general proportional control .
'Non-Engineering terms were used for variables to 'hopefully make the code and
'process more understandable 'for non-engineers.
'Attach a 10K Pot to pin c.1 to simulate a temperature sensor
'Goeytex
'**************************************************************************************[/COLOR]
#Picaxe 20M2
#no_data
[COLOR="#008000"]'#terminal 4800[/COLOR]
[COLOR="#008000"]'constants[/COLOR]
symbol gain = 10[COLOR="#008000"] ' Determines Proportional band
' With a gain of 10 the P Band will be from 400F to 500F
' Increase gain to narrow the band [/COLOR]
symbol bias = 500[COLOR="#008000"] ' Power Level at nominal operating condition (Assuming 50 percent power)[/COLOR]
symbol set_point = 450 [COLOR="#008000"]' Set Point value [/COLOR]
[COLOR="#008000"]
'variables[/COLOR]
symbol adc_val = w0
symbol process_value = w1[COLOR="#008000"] ' Process Value (temperature)[/COLOR]
symbol error_value = w2 [COLOR="#008000"]' difference between SV & PV[/COLOR]
symbol power_level = w3[COLOR="#008000"] ' Range is 0 to 1000 for 0 to 100 percent[/COLOR]
symbol temp_var = w4[COLOR="#008000"] ' General Purpose Variable for intermediate math[/COLOR]
MAIN:
pwmout pwmdiv16, b.1, 249, 0
DO
readadc10 c.1,ADC_Val
process_value = ADC_Val / 4 + 300 [COLOR="#008000"]'Scale ADC to Temperature Range [/COLOR]
if process_value <= set_point then
error_value = set_point - process_value
power_level = gain * error_value + bias Max 1000
pwmduty b.1,power_level
elseif process_value > set_point then
error_value = process_value - set_point
temp_var = gain * error_value max bias
power_level = bias - temp_var
pwmduty b.1,power_level
endif
LOOP
Last edited: