Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

temperature alert with LM335 sensor + pic 16F877A

Status
Not open for further replies.

lordmag

Junior Member level 3
Joined
Nov 9, 2010
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,446
hi ,
my projet is to make a temperature alert with pic 16f877A

i m using the LM335 sensor to detect the temperature this is the datasheet

the input is RA2/AN2 so i should make the ADC and the code that make the RED LED which is the output RB3 on when the temperature reach 60 °c
notice that this sensor range is between (-50 ° 125 °)

The temperature sensor's voltage output is related to absolute temperature by the following equation: Vout = VoutT0 * T / T0, where T0 is the known reference temperature where VoutT0 was measured. The nominal VoutT0 is equal to T0 * 10 mV/K. So, at 25 C, VoutT0 is nominally 298 K * 10 mV/K = 2.98 V

without the ADC the RED LED is on when the Vout=2,5v T= -23°c
so hellppp me please to do that analogic digital conversation to make the RED LED on when T reach 60°c = 333 k when Vout >= 3,33V the input is RA2/AN2
the pic is 16F877A and i'm using CCS C compiler
thank youu !!
 

as you know, the voltage will convert to 10 bit binary..so when volt = 5V the 10 bit value = 1023..
i have calculate when 3.33 = 680

so in your program,

you need a variable to hold the analog value..

if "variable = 680" then RB3 = 1
 
plz attach your code,so we can see where is the mistake

you can do something like that
adc1= read_adc();
adc2=(adc1*5)/1023;
if(adc2>=your value)
{
turn on led
}
else
{
turn off led
}
 
Hi,
Try something like this. Read the value from ADC. As you know 3.33v means that your ADC at 10-bit resolution will read 680. > (3.33/5) * 1024
Then check if the value read is larger than or equal to this. If it is turn the led on else, keep it off.
Code:
Temp = ADC_Read(2);
if (Temp >= 680)
{
   PORTB.F3 = 1;
}
else
{
   PORTB.F3 = 0;
}

The code is in mikroC, but I hope you get it. PORTB.F3 means RB3.

Hope this helps.
Tahmid.
 
hi ,
i' m very happy thank you for your attention :) !!
i think that the problem is here in the declaration please correct this code
Code:
#include "16F877A.h"
#define PIC16F877 *=16 ADC=10

void main()
{
int adc_value;
int temp,adc1,adc2;
SET_TRIS_B( 0x00 );
SET_TRIS_A( 0xFF );

setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(2); //channel 2 selected or pin RA2 selected for adc 
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(PIN_A2);
adc1=read_adc();
adc2=(adc1*5)/1023;
if(adc2>=680)
{
temp=1;
}
else
{
temp=0;
}
i m still new in ccs i dont know how to declare the ADC
 
Last edited:

this is my projet and all the code that i have done !!
isis + code .txt (pic c compiler )
 

Attachments

  • ALARME SYSTEM1.rar
    32 KB · Views: 151
Last edited:

Hi,
Make these changes:
Code:
#include "16F877A.h"
#define PIC16F877 *=16 ADC=10

void main()
{
int adc_value;
int temp,adc1,adc2;
SET_TRIS_B( 0x00 );
SET_TRIS_A( 0xFF );

setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(2); //channel 2 selected or pin RA2 selected for adc 
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(PIN_A2);
adc1=read_adc();
//adc2=(adc1*5)/1023;          ------>>>> remove this line
if(adc1>=680)      //------------>>> change here
{
temp=1;
}
else
{
temp=0;
}

Hope this helps.
Tahmid.

---------- Post added at 18:06 ---------- Previous post was at 18:03 ----------

Try this instead:
Code:
#include "16F877A.h"
//#use delay(clock=4000000)
#device ADC=10
#fuses XT, NOPROTECT, NOLVP,PUT,NOWDT,NOBROWNOUT

void main()
{
SET_TRIS_B( 0x00 );
SET_TRIS_A( 0xFF );

setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(2); //channel 2 selected or pin RA2 selected for adc 
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(RA2_ANALOG);
adc1=read_adc();
if(adc1>=680)
{
temp=1;
}
else
{
temp=0;
} 
while (true)
{
   if (input(PIN_A0))
      {
        output_high(PIN_B1);
        if (input(PIN_A1)&& temp ==1  )
        {
         output_high(PIN_B3);
         output_high(PIN_D0);
         output_low(PIN_B2);
        }
        else
        if ((input(PIN_A1)&& temp!=1)|| (!input(PIN_A1) && temp==1))
        {
         output_low(PIN_B3);
         output_low(PIN_D0);
         output_high(PIN_B2);  
        }
        else
        {
         output_low(PIN_B3);
         output_low(PIN_D0);
         output_low(PIN_B2);
        }
      }
  else
{
output_low(PIN_B1);
output_low(PIN_B3);
output_low(PIN_D0);
output_low(PIN_B2);
}

}
}

Hope this helps.
Tahmid.
 
hi i try this code
Code:
#include "16F877A.h"
#define PIC16F877 *=16 ADC=10
#fuses XT, NOPROTECT, NOLVP,PUT,NOWDT,NOBROWNOUT
void main()
{
int temp,adc1;
SET_TRIS_B( 0x00 );
SET_TRIS_A( 0xFF );

setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(2); //channel 2 selected or pin RA2 selected for adc 
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(PIN_A2);
adc1=read_adc();
if(adc1>=680)      
{
temp=1;
}
else
{
temp=0;
} 
while (true)
{
   if (input(PIN_A0))
      {
        output_high(PIN_B1);
        if (input(PIN_A1)&& temp ==1  )
        {
         output_high(PIN_B3);
         output_high(PIN_D0);
         output_low(PIN_B2);
        }
        else
        if ((input(PIN_A1)&& temp!=1)|| (!input(PIN_A1) && temp==1))
        {
         output_low(PIN_B3);
         output_low(PIN_D0);
         output_high(PIN_B2);  
        }
        else
        {
         output_low(PIN_B3);
         output_low(PIN_D0);
         output_low(PIN_B2);
        }
      }
  else
{
output_low(PIN_B1);
output_low(PIN_B3);
output_low(PIN_D0);
output_low(PIN_B2);
}

}
}

the problem that was showen here that ( adc1 is never greater then 680 ) line 15
 

yes this is because you are reading adc value in an 8-bit integer and 680 is greater than 255 (8-bit is equal to 255).
for higher value you can use int16.
void main()
{
long value1,value2,value3;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);

while(1)
{
set_adc_channel(0);
delay_ms(10);
value1=read_adc();
if (value1>=680)
{
do any thing you want.
}
set_adc_channel(1);
delay_ms(10);
value2=read_adc();
if (value2>=500)
{
do any thing you want.
}
}
}
 
Yea, I messed up the int here with that in BASIC. Use int16 or long for declaring adc1.
Code:
#include "16F877A.h"
#define PIC16F877 *=16 ADC=10
#fuses XT, NOPROTECT, NOLVP,PUT,NOWDT,NOBROWNOUT
void main()
{
int16 adc1; //---------------> here
int temp; //---------------> and here
SET_TRIS_B( 0x00 );
SET_TRIS_A( 0xFF );

setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(2); //channel 2 selected or pin RA2 selected for adc 
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(PIN_A2);
adc1=read_adc();
if(adc1>=680)      
{
temp=1;
}
else
{
temp=0;
} 
while (true)
{
   if (input(PIN_A0))
      {
        output_high(PIN_B1);
        if (input(PIN_A1)&& temp ==1  )
        {
         output_high(PIN_B3);
         output_high(PIN_D0);
         output_low(PIN_B2);
        }
        else
        if ((input(PIN_A1)&& temp!=1)|| (!input(PIN_A1) && temp==1))
        {
         output_low(PIN_B3);
         output_low(PIN_D0);
         output_high(PIN_B2);  
        }
        else
        {
         output_low(PIN_B3);
         output_low(PIN_D0);
         output_low(PIN_B2);
        }
      }
  else
{
output_low(PIN_B1);
output_low(PIN_B3);
output_low(PIN_D0);
output_low(PIN_B2);
}

}
}
 
hi , thank you !!! for your help !!
i try this code now and it s runnig
Code:
#include "16F877A.h"
#use delay(clock=4M)
#define PIC16F877 *=16 ADC=10
#fuses XT, NOPROTECT, NOLVP,PUT,NOWDT,NOBROWNOUT
#byte PORTB= 0x06
#byte TRISB= 0x86
int temp;
long value1;
 void ADC()
{
set_adc_channel(0);
delay_ms(10);
value1=read_adc();
if (value1>=170)
{
temp=1;
}
else
{
temp=0;
} }  

void main(void)
{
 TRISB=0x00; 
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);


while (1)
{
ADC();
    if (input(PIN_C1))
         {
           if (temp ==1 )
             {
               if (input(PIN_C2))
                  {PORTB=0x0d;
                   delay_ms(300);
                   PORTB=0x09;
                   delay_ms(200);
                    }
               else
                  { PORTB=0x03;}
             }
            else
            {
            if (input(PIN_C2))
               { PORTB=0x03;}
            else
               { PORTB=0x01;}
            }
         }
          else
         { PORTB=0x00;}
}}
but the problem that the adc of 3,33 v is 170 not 680 :?
temp =1 when value >= 170
so i' dont know why it s not 680 like what are you calculating
please can you more expalin for me the 680 and why it show me 170 not 680 and think you very much

notice that the code is running good
;)
 

Hi,
It's 170, because (3.33/5)*255 = 170. That means it's running in 8-bit mode, not 10-bit where it would be 680.

What does this do(I'm not very good with CCS)?
Code:
#define PIC16F877 *=16 ADC=10

Try changing the code:
Code:
#include "16F877A.h"
#use delay(clock=4M)
//#define PIC16F877 *=16 ADC=10       //      ---------->> What does this do?
#device adc=10 //           ------------->> Try this 
#fuses XT, NOPROTECT, NOLVP,PUT,NOWDT,NOBROWNOUT
#byte PORTB= 0x06
#byte TRISB= 0x86

Hope this helps.
Tahmid.
 
Last edited:
shokran Tahmidd ;) thanks a lot also for all member that give me the help !!
that s working now with :
#device ADC=10
my tempeture detector is working good now and in the 60°c it lunch the red led :)))))
can you propose me a solution how to simulate a CO2 sensor in isis and which one can i buy for this project fire detector with pic 16f877A
 

Hi,
I don't know much about these, but this seems popular, the MG811:

**broken link removed** ->> datasheet

https://www.futurlec.com/Gas_Sensors.shtml ->> sale at Futurlec

Hope this helps.
Tahmid.

---------- Post added at 15:20 ---------- Previous post was at 15:15 ----------

Hi,
I don't think it's available in ISIS. However, you could study the datasheet and make your program. Then, you can for example, send the signal that would be present at a particular level and see if your circuit responds to it. You can use a second microcontroller (this could get complex). Since the output of MG811 would vary between 30-50mV. Connect a voltage source of 30-50mV using a pot in ISIS and then simulate.

Hope this helps.
Tahmid.
 
Last edited:
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top