Forums MikroC Pro for PIC Project Discussions temperature controlled fan

This topic contains 3 replies, has 2 voices, and was last updated by  Bitahwa Bindu 1 month, 1 week ago.

  • Author
    Posts
  • #3051

    co6alt
    Participant

    hello. so i have a problem with the code. im using the following:
    MikroC application
    pic and pickit2
    pic16f887
    lm35
    two potentiometer
    lcd
    fan

    this is the program:
    sbit LCD_RS at RC2_bit;
    sbit LCD_EN at RC3_bit;
    sbit LCD_D4 at RC4_bit;
    sbit LCD_D5 at RC5_bit;
    sbit LCD_D6 at RC6_bit;
    sbit LCD_D7 at RC7_bit;

    sbit LCD_RS_Direction at TRISC2_bit;
    sbit LCD_EN_Direction at TRISC3_bit;
    sbit LCD_D4_Direction at TRISC4_bit;
    sbit LCD_D5_Direction at TRISC5_bit;
    sbit LCD_D6_Direction at TRISC6_bit;
    sbit LCD_D7_Direction at TRISC7_bit;
    // End LCD module connections

    //variables
    char Txt[7];
    float Temp_Ref ;
    unsigned int temp;
    float mV, ActualTemp;
    unsigned char inTemp;
    //end variables
    void main() {
    //configuration
    TRISA=0xff;
    trisB=0x1f;
    TRISD=0;
    trisc=0;
    ansel=0x03;
    anselh=0x00;
    //end configuration
    // lcd part 1
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Lcd_Out(1, 4, “Automatic”);
    Lcd_Out(2, 2, “Temp Control”);
    delay_ms(1000);
    //end lcd part 1

    // fan off
    portd.f0=0;
    Lcd_Cmd(_LCD_CLEAR);
    while(1)
    {
    // ADC
    temp = ADC_Read(0);
    mV = temp * 5000.0/1024.0;
    ActualTemp = mV/10.0 ;
    Temp_Ref=ActualTemp;
    //end ADC
    Lcd_Out(1, 1, “Enter Temp”);
    FloatToStr( Temp_Ref,Txt);
    Lcd_Out(1,10,Txt);
    Lcd_Chr(1,15,223);
    Lcd_Chr(1,16,’C’);
    Lcd_Out(2, 1, “Temp= “);
    FloatToStr(ActualTemp,Txt);
    Txt[4] = 0;
    Lcd_Out(2,7,Txt);
    Lcd_Out(2,12,” “);

    if (Temp_Ref > ActualTemp){portd.f0=0;}
    if (Temp_Ref < ActualTemp){portd.f0=1;}
    if (Temp_Ref == ActualTemp){portd.f0=0;}

    Delay_ms(2000);
    }

    }

    the idea is if the temperature is above the wanted temperature. the fan will turn on. if not it’s off.
    the display should display “automatic temprature” and then “enter temp”. and i should enter the temp from the potentiometer.
    the problem im having is : first the code is skipping “automatic…”
    secondly the lcd displays “enter tem(and b/w the brackets its displaying the temperature of the lm35sensor)”
    the connection is fine. but the problem is with the code that i cant figure out.
    thanks to whoever tries to help and sorry for my long post

  • #3052

    Bitahwa Bindu
    Keymaster

    Hi.

    1. Problem 1: the program is skipping “automatic..”: If everything else is correct especially the oscillator frequency and configuration bits, it should display “Automatic…” when it’s start, try to increase the delay and see if they’ll be any difference.
    2. Problem 2: The lcd displays the temperature of the sensor: That’s what you have set it to display. In your code you are writing Temp_Ref = ActualTemp, these values will be the same. It should display the same value on first line and second line. Now you are trying to compare the same thing? Temp_Ref will always be  = ActualTemp because that’s how you have set it. You want to read temperature from potentiometer! Where are you connecting your potentiometer? Where in the code you are reading from the potentiometer? How are you going to calibrate your potentionmeter readings so that a certain degree angle of potentiometer rotation will mean a certain degree in temperature?
    3. Did you watch the our video of Automatic Temperature Control? We explained in details how to set the reference temperature.
    4. Did you watch our Analog to Digital video? We explained in details how to read values from potentiometer.
  • #3053

    co6alt
    Participant

    hello sir/mam. thank you so much for replying. so i did update the code a little bit and i managed to fix some errors but now im stuck with two problems. firstly the lm35 sensor is just displaying the same value of the potentiometer with .10 extra i still havent figured out why. the second problem is that the fan is still not working with me even though the desired temperature is .10 less than the sensor as the lcd displays.

    here is the updated code:

    sbit LCD_RS at RC2_bit;
    sbit LCD_EN at RC3_bit;
    sbit LCD_D4 at RC4_bit;
    sbit LCD_D5 at RC5_bit;
    sbit LCD_D6 at RC6_bit;
    sbit LCD_D7 at RC7_bit;

    sbit LCD_RS_Direction at TRISC2_bit;
    sbit LCD_EN_Direction at TRISC3_bit;
    sbit LCD_D4_Direction at TRISC4_bit;
    sbit LCD_D5_Direction at TRISC5_bit;
    sbit LCD_D6_Direction at TRISC6_bit;
    sbit LCD_D7_Direction at TRISC7_bit;
    // End LCD module connections

    //variables
    char Txt[7],Txt2[7];
    float Temp_Ref ;
    unsigned int temp,temp2;
    float mV, ActualTemp;
    unsigned char inTemp;
    //end variables
    void main() {
    //configuration
    TRISA=0xff;
    TRISD=0;
    ansel=0x03;
    anselh=0x00;
    //end configuration
    // lcd part 1
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Lcd_Out(1, 4, “Automatic”);
    Lcd_Out(2, 2, “Temp Control”);
    delay_ms(1000);
    //end lcd part 1

    // fan off
    portd.f0=0;
    Lcd_Cmd(_LCD_CLEAR);
    while(1)
    {
    // ADC
    temp = ADC_Read(0);
    temp2 = ADC_Read(1);
    mV = temp * 5000.0/1024.0;
    temp2 = temp2 * 5000.0/1024.0;
    ActualTemp = mV/10.0 ;
    Temp_Ref=temp2/10.0 ;
    //end ADC
    Lcd_Out(1, 1, “Enter Temp:”);
    FloatToStr( Temp_Ref,Txt);
    Lcd_Out(1,12,Txt);
    Lcd_Chr(1,15,223);
    Lcd_Chr(1,16,’C’);
    Delay_ms(1000);
    Lcd_Out(2, 1, “Temp= “);
    FloatToStr(ActualTemp,Txt2);
    //Txt[4] = 0;
    Lcd_Out(2,7,Txt2);
    Lcd_Out(2,12,” “);

    if (Temp_Ref > ActualTemp){portd.f0=0;}
    if (Temp_Ref < ActualTemp){portd.f0=1;}
    if (Temp_Ref == ActualTemp){portd.f0=0;}

    Delay_ms(2000);
    }

    }

     

    i hope the code answers your questions. And no i didnt see the videos yet however i will do so and try to fix it. thank you again.

  • #3054

    Bitahwa Bindu
    Keymaster

    Please watch these videos they’ll respond to most of your questions. After watching then ask the remaining questions.

    Don’t take a short cut, learn first the principle then apply later.

    Analog to Digital Converter: https://youtu.be/X9F-Kr9uIGo

    Automatic Temperature control part 2: https://youtu.be/m7wCqsdbas0

    Automatic Temperature control part 3: https://youtu.be/E9KzENHTmcY

You must be logged in to reply to this topic.