PIC12F675 Thermostat problem

General discussion on mikroC.
Post Reply
Author
Message
nelson_mendes
Posts: 66
Joined: 01 Feb 2008 15:48
Location: Porto, Portugal

PIC12F675 Thermostat problem

#1 Post by nelson_mendes » 27 May 2008 19:39

Hi there,

I recently did a project with a PIC12F675 in order to control the temperature of some internet equipment. It has the PIC, an LM35 sensor, one NPN BJT, one fan and 3 3mm led's.

I think you might be seeing what all this does... it just switches the fan ON and OFF to control the temperature sensed by the LM35.
The 3 led's just give me some information of what's happening, like a thermometer...

The code is working great, I had some problems with MCLRE but I put it as internal and was solved. The thing is, the middle led somehow doesn´t stay as bright as the other two and I don't have any idea why.

The resistors are all the same (1K), the led is also OK (I tested it separate) and the other two have a nice bright light. The middle one I hardly can tell when it's ON.

If someone has the solution for this problem I would be very thankful! :cry:

This is my code:

Code: Select all

                          //Input of calibration values:

//                                               Temp(0~255) = Temp(ºC) x 255
const min_temp = 168;     //min_temp ~= 33ºC   |               --------------
const max_temp = 219;     //max_temp ~= 43ºC   |                     50

unsigned short zone_1 = (0.2 * (max_temp - min_temp)) + min_temp;
unsigned short zone_2 = (0.4 * (max_temp - min_temp)) + min_temp;
unsigned short zone_3 = (0.6 * (max_temp - min_temp)) + min_temp;
unsigned short zone_4 = (0.8 * (max_temp - min_temp)) + min_temp;
unsigned short temperature;

#define red    GPIO.F5
#define yellow GPIO.F4
#define fan    GPIO.F2
#define green  GPIO.F1

//------------------------------------------------------------------------------
void InitMain()
{
  OPTION_REG = 3;
  GPIO = 0;
  TRISIO = 4;
  ANSEL = 1;
  ADCON0.VCFG = 0;
}
//------------------------------------------------------------------------------
void Thermo_LED()
{
  temperature = Adc_Read(ANS0) >> 2;
  if(temperature < zone_1)
  {
    yellow = 1;
    green = 0;
    red = 0;
  }

  if((temperature < zone_2) && (temperature >= zone_1))
  {
    yellow = 1;
    green = 1;
    red = 0;
  }

  if((temperature <= zone_3) && (temperature >= zone_2))
  {
    yellow = 0;
    green = 1;
    red = 0;
  }

  if((temperature <= zone_4) && (temperature > zone_3))
  {
    yellow = 0;
    green = 1;
    red = 1;
  }

  if(temperature > zone_4)
  {
    yellow = 0;
    green = 0;
    red = 1;
  }
  asm {CLRWDT}
}
//------------------------------------------------------------------------------
void main()
{
  InitMain();
  
  delay_ms(25);
  
Fan_OFF: fan = 0;
  while(1)
  {
    temperature = Adc_Read(ANS0) >> 2;
    if(temperature >= max_temp)
    {
      goto Fan_ON;
    }
    asm {CLRWDT}
    Thermo_LED();
  }

Fan_ON: fan = 1;
  while(1)
  {
    temperature = Adc_Read(ANS0) >> 2;
    if(temperature <= min_temp)
    {
      goto Fan_OFF;
    }
    asm {CLRWDT}
    Thermo_LED();
  }
}

around
Posts: 119
Joined: 27 May 2008 23:16
Location: Victoria, BC Canada
Contact:

PIC12F675 Thermostat problem

#2 Post by around » 27 May 2008 23:26

Nelson,

Is your AD set-up correctly? You have selected AN0 (ANSEL=1) but you do not have the corresponding TRIS bit set.

nelson_mendes
Posts: 66
Joined: 01 Feb 2008 15:48
Location: Porto, Portugal

Re: PIC12F675 Thermostat problem

#3 Post by nelson_mendes » 27 May 2008 23:52

around wrote:Nelson,

Is your AD set-up correctly? You have selected AN0 (ANSEL=1) but you do not have the corresponding TRIS bit set.
Hi,

I think the A/D is working well. I've putted TRIS = 4 because an error of self reseting the PIC. It was like some sort of voltage noise was reseting it even with the push-button and the 10k resistor placed correctly on the PCB board.
So I've putted TRIS = 4 and MCLRE = 0 (on edit project option).

The led's are switching correctly, even the green one light's but hardly is noted. I even tough that GPIO.F1 on 12F675 can't provide the same amount of current as the others... but datasheet doesn't mention anything like that.

Anyway, thank you very much for your attention!

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#4 Post by drdoug » 28 May 2008 03:53

I didn't take a real close look at your code but I suspect you are turning the light on and off very fast which makes it appear dimmer.

Did you try to comment out some of your routines in the Thermo_LED sub?
Also, you may need to look at your variable assignments. Try to put in the actual numbers (ie 183) rather than the calculation and see if that has an effect. With the unsigned short type you may be freaking out the complier and running over 255, change to int and see if that helps.

Good luck.

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

#5 Post by Sparky1039 » 28 May 2008 07:39

Green LED's generally have a higher voltage drop (>2V) than red or yellow ones. Figure out the voltage drop for your device and re-adjust the current limiting resistor accordingly. Yours are all set at 1K and may be too high. Just remember the PIC I/O ports can only source or sink 25mA max current.

As an example:
Resistor value = (VDD - V-Drop LED) / LED current

R = (5V - 2.2V)/ 10mA
280 ohms or 270 ohms using standard values.
The 10mA was arbitrarily picked. You have to decide what you want to use or how bright the LED needs to be. Just don't make this value > 25mA

nelson_mendes
Posts: 66
Joined: 01 Feb 2008 15:48
Location: Porto, Portugal

#6 Post by nelson_mendes » 28 May 2008 12:48

drdoug wrote:I didn't take a real close look at your code but I suspect you are turning the light on and off very fast which makes it appear dimmer.
The red and yellow led's are switching very well and since the routines are OK by the software and don't think that is occurring dimmer effect.
drdoug wrote:Did you try to comment out some of your routines in the Thermo_LED sub?
Also, you may need to look at your variable assignments. Try to put in the actual numbers (ie 183) rather than the calculation and see if that has an effect. With the unsigned short type you may be freaking out the complier and running over 255, change to int and see if that helps.

Good luck.
Sorry, I didn't commented this program because I was using '#define ...' for the Pic pins for the first time (rookie). The program seemed intuitive to me and I didn't commented it.
Basically, I assumed 5 diferent temperature levels or zones (what you prefer most) and the result indicated by the 3 led's are according to the test made to the temperature (only one of those 5 IF is true in one execution of that function).

Unsigned short type is only valid from 0 to 255, the int as a bigger range and doesn't suites for this application. I am also making a double shift right to ensure the value read by the ADC is only between 0 and 255.

To Sparky1039:

I tested yesterday applying 5V to the led on the board (with the rest of the circuit OFF) using also a 1k resistor and the led was OK... Its brightness was OK for what I needed so I think it might be a programming problem.

I'm using higher resistor values because this is going to be a standalone application and is going to be on my bedroom... I've got plenty of lights blinking at night and I didn't wanted very bright leds disturbing my sleep. :wink:

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#7 Post by drdoug » 28 May 2008 16:24

Try to comment this out

Code: Select all

if(temperature < zone_1)
  {
    yellow = 1;
//    green = 0;
    red = 0;
  }

and the other spots that green=0 appears in this portion of your sub-procedure and see if the LED gets brighter. Then use process of elimination to track it down.

nelson_mendes
Posts: 66
Joined: 01 Feb 2008 15:48
Location: Porto, Portugal

#8 Post by nelson_mendes » 28 May 2008 18:08

I did it!

First I replace the 10k resistor from the reset button for 1k (it stayed more immune to voltage noise).

Then the code used was the followed:

Code: Select all

/* Test configuration:
     MCU:             PIC12F675
     Oscillator:      Internal, 04.0000 MHz
     Ext. Modules:     -
     SW:              mikroC v8.1

   Pin Distribution:
     LM35                - GP0
     Yellow LED          - GP4
     12V Fan control pin - GP2
     Green LED           - GP1
     Red LED             - GP5
*/
                          //Input of calibration values:

//                                               Temp(0~255) = Temp(ºC) x 255
const min_temp = 102;     //min_temp ~= 20ºC   |               --------------
const max_temp = 153;     //max_temp ~= 30ºC   |                     50

unsigned short level_1 = (0.2 * (max_temp - min_temp)) + min_temp;
unsigned short level_2 = (0.4 * (max_temp - min_temp)) + min_temp;
unsigned short level_3 = (0.6 * (max_temp - min_temp)) + min_temp;
unsigned short level_4 = (0.8 * (max_temp - min_temp)) + min_temp;
unsigned short temperature;

#define red    GPIO.F5
#define yellow GPIO.F4
#define fan    GPIO.F2
#define green  GPIO.F1

//------------------------------------------------------------------------------
void InitMain()
{
  OPTION_REG = 0b1111;   // Assign Prescaler to WDT @ 1/128.
  OPTION_REG.F7 = 1;     // Internal pull-up's are disabled.
  GPIO = 0;
  CMCON = 7;             // Comparator module is OFF.
  TRISIO = 1;            // GPIO.F0 is input
  ANSEL = 1;             // Ans0 is an analogue input.
  ADCON0.VCFG = 0;       // Vdd as Vref
}
//------------------------------------------------------------------------------
void Thermo_LED()
{
  temperature = Adc_Read(ANS0) >> 2;
  if(temperature < level_1)
  {
    yellow = 1;    // bellow level 1, only yellow is ON.
    green = 0;
    red = 0;
  }
  if((temperature < level_2) && (temperature >= level_1))
  {
    yellow = 1;    // between level 1 and level 2, yellow and green are ON
    green = 1;
    red = 0;
  }
  if((temperature <= level_3) && (temperature >= level_2))
  {
    yellow = 0;    // between level 2 and level 3, only green is ON
    green = 1;
    red = 0;
  }
  if((temperature <= level_4) && (temperature > level_3))
  {
    yellow = 0;    // between level 3 and level 4, green and red are ON
    green = 1;
    red = 1;
  }
  if(temperature > level_4)
  {
    yellow = 0;    // above level 4, only red is ON.
    green = 0;
    red = 1;
  }
  asm {CLRWDT}
}
//------------------------------------------------------------------------------
void main()
{
  InitMain();

Fan_OFF: fan = 0;
  while(1)
  {
    temperature = Adc_Read(ANS0) >> 2;
    if(temperature >= max_temp)
    {
      asm {CLRWDT}
      goto Fan_ON;
    }
    asm {CLRWDT}
    Thermo_LED();
  }

Fan_ON: fan = 1;
  while(1)
  {
    temperature = Adc_Read(ANS0) >> 2;
    if(temperature <= min_temp)
    {
      asm {CLRWDT}
      goto Fan_OFF;
    }
    asm {CLRWDT}
    Thermo_LED();
  }
}
I discovered that I had several errors on the code... but now it's working!

Thank you all! In the summer this circuit will be specially useful on those hot days in order to my Internet connection don't go down.

Thanks :wink:

Note: The only thing I need now is to discover the correct values for min_temp and max_temp!

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: PIC12F675 Thermostat problem

#9 Post by drdoug » 08 Nov 2010 12:05

I'm glad it's working for you.
If you have that much voltage noise, I would make sure to put 10uF, 1uF, and 0.1uF caps on your voltage source. With the .1uF chips as close to you pic as possible.
Maybe even use 0.01??
Some others will may have some better noise suppression suggestions.
I would be a little concerned about that value resistor for a pull-up. I would consider another problem????
Care to post a schematic and/or picture of the circuit so we can take a look?

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: PIC12F675 Thermostat problem

#10 Post by drdoug » 08 Nov 2010 20:42

After thinking about this a little, I wonder if you have a loose connection to v+ on the pic and the lower value resistor on mclr allows you to get some para site power???

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: PIC12F675 Thermostat problem

#11 Post by Sparky1039 » 08 Nov 2010 21:15

Replying to a 2.5 yr old post? :wink:

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: PIC12F675 Thermostat problem

#12 Post by drdoug » 08 Nov 2010 21:20

I didn't look at the date but I got an email that the thread was updated. Wtf???
Oh well. Maybe I shouldn't reply before breakfast anymore.

Post Reply

Return to “mikroC General”