PDA

View Full Version : An algorithm for controlling temperature and humidity of greenhouse and aviculture



amindzo
- 7th November 2008, 15:50
Hi, i want to control the humidity and temperature of the greenhouse and aviculture,so i need a good and simple algorithm,I use SHT75 (a digital temperature and humidity sensor for measuring temperature and humidity) and PIC16F877A. I don't have any problem to work with this sensor and microcontroller,i just want a good idea for controlling algorithm.Imagine we have 4 variables: 1)real_temp 2)real_humid 3)temp_value 4)humid_value
variables 1 and 2 are real temperature and real RH that measured by sensor and variables 3 and 4 are favorite values for temperature and humidity that inserted by operator with keyboard.I want to measure temperature and humidity every 10 minutes and execute the controlling program in a subroutine so i want an algorithm that control the temperature and humidity and adjust them in favorite values.

Acetronics2
- 7th November 2008, 16:43
Hi, Amindzo

Not so simple as a calculation ... nor to realize.

In fact, you try to realize a full climate control unit !!!

I think the easy way is

A) for entering air

- Measure temp and Humidity

B) for Target conditions

- calculate the Dewpoint temp for those conditions

C) Heat ( or cool ... depends on entering air ) your air to the calculated dewpoint temp

D) saturate it with sprinkled water @ ~ the dewpoint temp (- only if needed ! )

E) Heat it to your required temp; the humidity will be very close to what you look for ... no ???

Alain

amindzo
- 7th November 2008, 17:03
i just want a simple and utility algorithm,for example with conditional causes:

IF (real_temp)<((temp_value)-1) then

turn on heaters
............
............
ENDIF

for controlling, i have 8 fans,2 heaters and there is a pad on one side of the aviculture and greenhouse that i can control the water with valve that pour on the pad and big fans (2.5Kwatt) on the other side of the saloon pull the air and adjust temperature and humidity of the saloon.the saloon approximately has 20 meter width and 75 meter length.
i should exert a margin for temperature and humidity to avoid frequently turning on and off ,fans and heaters.for example if the favorite value is 29C', i should use (29-1) and (29+1) in my algorithm,are you agree with me?

aratti
- 7th November 2008, 17:21
Amindzo, add the code necessary and it should work.


Rdegree var byte ' sensor reading variable
Sdegree var Byte ' Temperature setpoint variable
RHumidity var byte ' Sensor Humidity variable
SHumidity var byte ' Humidity setpoint variable
TempDev var Portx.x ' Pin to control the heating system
HumDev var Portx.y ' pin to control water spry motor


Main:
'make a loop delay for the time required at the end call CheckAmbient
'Here you need also the routine for setting your setpoints
goto main

CheckAmbient:
if Rdegree > Sdegree then
TempDev=Low
else
TempDev=high
Endif
if RHumidity > SHumidity then
HumDev=Low
else
HumDev=high
Endif
return

end

Al.

amindzo
- 7th November 2008, 17:28
thanks but it is very simple with your algorithm fans and heaters turning on and off frequently and it will damage them so i think i should exert a margin for example for 1 degree.((setpoint+1) and (setpoint-1))

amindzo
- 8th November 2008, 09:49
it's not a modern system.the water pour on the pads and big fans on the other side of the saloon pull the air and flow of the air make an approximately constant temperature and humidity in aviculture. in aviculture humidity is not as important as temperature(the pads not heated) but in greenhouse we use fog makers to control humidity exactly. The fans,heaters,valves and fog makers are controlled with PIC16F877A. i put some sensors in different places of saloon and measure the humidity and temperature and send data to main controller and that controller decide what to do and it will turn on/off fans,heaters,vaves and fog makers.for example for turning on a fan, i will make PORTB.0 high and it turns on the relay(5v 10A relay) then this relay will turn on the contactor( SIEMENS 3PH contactors) and contactor with turn on the fan.i also put a keypad to control each fan and heater automatically or manually that can selected with the operator.

imagine that we have 8 fans and 2 heaters and a valve for pour water on the pad(work as a cooler in hot weather and increase humidity)
fans: fan1 fan2 fan3 fan4 fan5 fan6 fan7 fan8
heaters: heater1 heater2
valve: valve1
fan1,fan3 fan5 and fan7 are always on for ventilation in aviculture.
for controlling i should use fan2,fan4,fan6 and fan8 and also heaters and valve.
so in my algorithm when i tell fans ,it means fan2,fan4,fan6 and fan8

in aviculture humidity is not as important as temperature so i exert this margine for temperature and humidity:
for temperature: (setpoint-1) and (setpoint+1)
for humidity: (setpoint-4) and (setpoint+4)

this is my algorithm that executed every 10 mintutes:


1-if (temp<(setpoint-1)) and (humidity<(setpoint-4)) then

turn on heaters
turn off fans

if the situation is not changed for 20 minutes then turn on valve

if the situation is not changed for 40 minutes turn on alarm

2- if (temp<(setpoint-1)) and (humidity>(setpoint+4) then

turn off valve
turn on heaters
turn off fans

if the situation is not changed for 20 minutes then turn on fans(for reducing humidity)

if the situation is not changed for 40 minutes then turn on alarm

3-if (temp>(setpoint+1)) and (humidity>(setpoint+4)) then

turn off valve
turn off heater

if the situation is not changed for 10 minutes then turn on fans

if the situation is not changed for 20 minutes turn on valve(for reducing temperature)

if the situation is not changed for 40 minutes turn on on alarm


4-if (temp>(setpoint+1)) and (humidity<(setpoint-4) then

turn off heaters

if the situation is not changed for 10 minutes then turn on fans and valve

if the situation is not changed for 40 minutes then turn on alarm


what do you think about this algorithm?

mackrackit
- 8th November 2008, 10:04
what do you think about this algorithm?
Like I said on one of your other threads. Write down every thing you would do if you were controlling the system manually.

Looks like you have done that.

Now copy your algorithm into a code editor and comment it out. Start writing the code in between each operation.

amindzo
- 8th November 2008, 12:26
my problem is a good algorithm.i can convert the algorithm to picbasic easily.

locko
- 12th November 2008, 01:06
I made a simple temp/rh controller for my wife's crab tank a long time ago. I found better results when I averaged the readings and gave it a plus/minus window. Eg. temp drops below the low setting - heater came on and drove it back to the required setting and shut off. The slight overshoot was well within the upper setting so no need to worry about a complicating the controller for handling the hysteresis etc.

Darrel Taylor
- 12th November 2008, 02:17
what do you think about this algorithm?

Yuck.
OMG, did I say that. :o

If all those fans, heaters, etc. are individually controlled?
You'd get much better results using a PID loop or two.

Then it would be able to actively "Maintain" the preset conditions, instead of just overreacting when things get too far out of whack.

Check out this thread from Henrick.

PID-filter routine (2nd try).
http://www.picbasic.co.uk/forum/showthread.php?t=5874

And this explains how it works.
It's for the basic stamp, but it's what henrick based his program on.
Start at page 97

Industrial Control (pdf)
http://www.parallax.com/Portals/0/Downloads/docs/books/edu/ic.pdf
<br>

leonardodvc
- 17th February 2012, 07:19
'************************************************* ***************
'* Name : controlhumedad2.BAS *
'* Author : Leonardo Castillo *
'* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 13/11/2010 *
'* Version : 2.0 *
'* Notes : emplea sensor HMZ 433A1 "pero su voltaje aumenta *
'* : con la temperatura como si fuera un PTC, esto es *
'* :debido a la resistencia en paraleo ya que el voltaje*
'* :obtenido es el de la resistencia en paralelo, por lo*
'* :que se debe de obtener el complemento para tener la *
'* :lectura sobre el termistor. "Vt=5-lectura *
'************************************************* ***************
'p=pic16F88
INCLUDE "modedefs.bas"
asm
__CONFIG _CONFIG1, _CP_OFF & _CCP1_RB3 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_OFF & _WDT_OFF & _INTRC_IO
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
endasm
clear ;limpia todos los registros
define OSC 4 ; especifica que se va a utilizar uno de 4 Mhz

; Definicion de conexiónes del LCD
DEFINE LCD_DREG PORTB ; LCD bits de comunicacion al LCD
DEFINE LCD_DBIT 1 ; Coneccion puerto B (RB4,RB5,RB6,RB7)
DEFINE LCD_RSREG PORTB ; LCD bit RS conectado a PORTB
DEFINE LCD_RSBIT 2 ; LCD bit RS conectado a puerto B (RB2)
DEFINE LCD_EREG PORTB ; LCD bit E conectado a PORTB
DEFINE LCD_EBIT 3 ; LCD bit E conectado a puerto B (RB3)
DEFINE LCD_BITS 4 ; LCD comunicado 4Bits
DEFINE LCD_LINES 2 ; LCD de 2 lineas

n var word
n1 var word
J1 var word
J2 var word
V1 VAR WORD
V2 VAR WORD
inicial var word
B var word
J var word
Voltemp Var word ' Variable donde se guarda el resultado de conversion A/D
tempV var word
temp var word
degC var word
HumedadV var word
Humedad var word
symbol TI=PORTA.1
SYMBOL Humed=PORTA.0
symbol templm35=PORTA.4
symbol Ventilador=PORTB.0
; definicion de parametros de conversion A/D
DEFINE ADC_BITS 10 ; numero de bit conversion A/D
DEFINE ADC_CLOCK 3 ; reloj interno RC usado en conversion A/D
DEFINE ADC_SAMPLEUS 50 ; tiempo de conversion en Us
Inicio:
OSCCON = %01100110 ;se selecciona el tipo de oscilador (interno) y a 4 MHz
TRISA =%00010011 ;RA0 y RA1 RA4(AN 0,AN1,AN4 CANAL 0, 1 y 4) toma las lecturas
TRISB =%00000000 ;Pines de PORTB al LCD son salida
CCP1CON = 0 ;DESHABILITA modulo PWM
CMCON = 7 ;deshabilita modulo comparador
PAUSE 500 ;0.5s para inicializar LDC
lcdout $FE,$0C ;lcd cursor off
ANSEL=%00010011 ;AN 0 AN 1 AN4 ENTRADA ANALOGA
;ADCON0 = %11000000 ;RC interno y modulo convertidor sin operar,CANAL AN"0" ,
ADCON1 = %10000000 ;voltaje de referencia Vdd-Vss y ADFM =0 justifica resultado a la derecha
thermistor:
ADCON0.3=1 ;modulo combertidor en operacion canal 1
ADCIN 1,Voltemp ;Lee canal 0 y guardar en variable sensar
V1=VOLTEMP ;Este valor de lectura corresponde al complemento de la lectura del termistor
gosub logaritmo ;debido a la resistencia conectada en paralelo para linealizar la lectura.
j1=j
voltemp=1024-voltemp 'calcula el valor complementario "5000-Vi" 5000=Lectura*4.88 correspondiente a
V2=VOLTEMP 'la lectura en el termistor
gosub logaritmo 'como se usan 10 bits de combercion entonces valor maximo=2^10
j2=j
n=j2-j1+1315 'ln(Vs-Vt)-Vt+1315
b=3922 'para poder efectuar la operacion DIV32 la operacion de multiplicacion
n1=b*100 'debe realizarce empleando almenos una variable para poder asignar registros
inicial=div32 n 'internos de memoria, si se multiplican 2 ctes la operacion de mult es efectuada
n1=inicial 'durante la compilacion y no durante la ejecucion del programa.
degC=inicial-273

LCDOUT $FE,1,DEC V1," ",DEC V2," d ",dec n1 'LIMPIA DISPLAY
lcdout $FE,$C0,"ln ",dec j1," ",dec j2," ",dec n
pause 2000
LeeHumedad:
ADCON0.3=0 'canal AN0 operando
ADCIN 0,HumedadV 'Lee canal 1 y guardar en variable 5000/1024=4.88
;ADCON0.3=0 'apaga canal AN2
Humedad=(Humedadv*4+(Humedadv/100)*88)/10 'se puede emplear H=H*/1249 <--1249=4.88*256
humedad=(33*humedad)/100 'se calcula la humedad relativa
humedad=humedad min 90
ADCON0.5=1 'canal AN4 operando
adcin 4,tempv
ADCON0.5=0 'apaga canal
TEMP=(TEMPV*4+(TEMPV*88/100)) 'COMBIERTE VOLTAJE EN TEMPERATURA "Volt/10 [mV/ºC]*10 Ej: 12.5ºC=125
lcdout $FE,1,DEC degC," ",DEC HUMEDAD,"%"," ",dec temp/10,".",dec1 temp," C"
LCDOUT $FE,$C0,DEC humedadv," ",dec tempv
pause 3000
;rutina de encendido de ventilador
if (humedad>80)&(temp/10>26) then high Ventilador 'prende el ventilador
if (humedad<70)|(temp/10<24) then low ventilador 'apaga ventilador
goto thermistor
Logaritmo:
J=ncd voltemp max 1-1
j=(j*693)/10 + (((voltemp-dcd j)/10)*693)>>j 'el factor 693 es para combertir de base 2 a base e
return 'y multiplicado por 1000 para obtener digitos
'significativos y se divide entre 10 para limitar
'a tres digitos
'"En el segundo termino de la suma se dividio entre 10
'primero para limitar el resultado a decimos, debido a
'que el valor de la diferencia o residuo puede ser del
'orden de centenaas y si se multiplica por 639, la
'magnitud del resultado puede ser superior a 65534
'con lo que el resultado de la operacion nos daria un
'error."
'Temperature(..) 0 10 20 25 30 40 50 60
'Resistance(K..) 160.56 98.714 62.328 50.0 40.356 26.756 18.138 12.554
'Voltaje 3.8(778)3.3(676)2.7(553)2.5(512)2.2(455)2.2(455)1. 3(271)1(198)
'PARA OBTENER LOS VALORES EMPLEADOS EN LA TABLA SE EMPLEA LA ECUACION
' Rt = R25 exp(B/Tt-B/T25) --> Tt = 1/(1/B*ln(Rt/R25)+1/T25) ------(1
' B se obtiene de B = Tt*T25/(Tt-T25)*ln(R25/Rt) ------(2
'colocando una resistencia en paralelo con laa salida de la sseñal del termistor se logra linealizar
'la lectura y el voltaje a la salida seria el medido por la resistencia en paralelo
'Vs=Vt(Rs/(Rs+Rt)) despejando Rt=Rs(Vs-Vt)/Vt substituyendo en (1
'Tt = 1/(1/B*ln(Rs(Vs-Vt)/(R25*Vt))+1/T25)____ Hasiendo Rs=R25 ;Rs=50kohms
'Tt = 1/(1/B*ln((Vs-Vt)/Vt)+1/T25)-273.15____ Valor de temperatura en ºC
'T=B/(ln(Vss-Vt)-lnVt+K/298.15)-273=3922*100/((ln(Vss-Vt)-ln(Vt)+13.15)*100)
'Vss=5000/1024=4.88 => lectura*4.88=Vt
'Para obtener el logaritmo se obtiene el valor expoencial de la caracteristica del valor
'esto se logra empleando la instruccion NCD que nos debuelbe la posicion del bit mas significativo
'que corresponde al valor inicial de el logaritmo, se le resta un digito para ajustar la posicion
'ya que el comando debuelbe un valor comprendido entre 1 y 16 y debe ajustarce el valor de 0 a 15.
'ademas se emplea el operador MAX para limitar el valor de 1 a 16.
'Con este valor de entrada se obtiene la mantisa, que corresponde al valor del residuo,
'para esto se resta del valor inicial el exponente 2^n, este residuo se divide entre el mismo
'exponente 2^n, a esta mantisa se le suma la caracteristica que es el valor n, por ultimo se
'multiplica el valor obtenido por un factor de escala y por la constante para combertir el valor a
'logaritmo natural (.693).
'Ademas de que si se lineariza el thermistor se puede calcular el valor de la resistencia
'empleando la siguiente ecuacion Vt = Vs* Rs/(Rs+Rt) --> Rt = Rs*(Vs-Vt)/Vt ,Rs=R25, Vs=5 volts
'Con lo que la temperatura se puede calcular con Tt = 1/(1/B*ln(5-Vt/Vt)+1/298.15) [ºK]
;ObtieneHumedad:
' 10.. 15.. 20.. 25.. 30.. 35.. 40..
'20%RH 0.75 0.72 0.69 0.66 0.62 0.59 0.55
'30%RH 1.03 1.00 1.00 0.99 0.96 0.93 0.90
'40%RH 1.32 1.30 1.31 1.32 1.30 1.28 1.25
'50%RH 1.64 1.63 1.64 1.65 1.64 1.63 1.61
'60%RH 1.97 1.97 1.98 1.98 1.98 1.98 1.96
'70%RH 2.30 2.30 2.31 2.31 2.31 2.31 2.30
'80%RH 2.64 2.64 2.63 2.64 2.63 2.63 2.61
'90%RH 2.97 2.96 2.94 2.97 2.94 2.92 2.90
'realizando una analisis de regresion se obtienen los siguientes factores
'HR=3.28*VoltHum+.2 Para temperaturas de 10-40ºC
'por lo que se puede emplear la siguiente ecuacion para determinar la HR
'HR=328/100*VoltHum/100, estando VoltHum en centesimas de volt (0-330V)
'pero para poder efectuar el calculo se recurre a las siguientes operaciones
'33*VoltHum/100 , el valor de .2 es minimo y se desprecia sin que afecte el resultado