[PIC] relay control by mcu

Status
Not open for further replies.

Muhammad.Saad

Newbie level 6
Joined
Mar 11, 2014
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Islamabad, Pakistan
Visit site
Activity points
105
Hi
I am new to mcu. I want to make a project for controlling two leds. I am using PCW (Pic c compiler)Please help me to correct my code to on/off relay through potentiometer reading(0 to 5v). I need to on the relay when voltage is under 1.5v and above 4.5v. here is my circuit.I need to operate rely for under and over voltage limitations.
lower limit is 1.5v
upper limit is 4.5 v.

Here is code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <main.h>
 #fuses NOWDT
 #fuses NOLvP
 #fuses HS
 #fuses NODEBUG
 #include<stdio.h>
 #include<STDLIB.H>
 #use delay(clock=11059200)
 #DEFINE relay_on 1
 #define relay_off 0
 #fuses HS,WDT
 unsigned int8 relay,adcvalue, LOWERLIMIT, UPPERLIMIT;
 void main(void)
 {
 setup_adc( ADC_CLOCK_INTERNAL);
 setup_adc_ports( ALL_ANALOG );
 ( input(PIN_A0));
 set_adc_channel(0);
 
relay = read_adc();
 LOWERLIMIT = (5*relay*100/1023);
 UPPERLIMIT = (5*relay*100/1023);
 
if (LOWERLIMIT < 2)
 output_low(PIN_C0);
 Else
 output_high(PIN_C0);
 if (UPPERLIMIT <= 4)
 output_high(PIN_C0);
 else
 output_low(PIN_C0);
 }



Looking forward for reply
Looking for help
 

Attachments

  • 1.png
    56.4 KB · Views: 87
Last edited by a moderator:

1) In this statement:" LOWERLIMIT = (5*relay*100/1023)" why are you multiplying by 100? I am assuming you are trying to convert the ADC reading to voltage (which is not really necessary to begin with), but that's not what you're doing (unless the ADC range is 0 to 500 volts). And then you've assigned UPPERLIMIT which is EXACTLY THE SAME THING as LOWERLIMT!!

2) What you really want is something like:

Code:
 (not real C code)
if( (relay< lowlim) or (relay>hilim) )then 
RCO=1;
else
RCO=0;

3) I assume the top of your relay is tied to some voltage?
4) Put a diode across that relay, or you'll damage something...
 

LOWERLIMIT = (5*relay*100/1023);
UPPERLIMIT = (5*relay*100/1023);

if (LOWERLIMIT < 2)
output_low(PIN_C0);
Else
output_high(PIN_C0);
if (UPPERLIMIT <= 4)
output_high(PIN_C0);
else
output_low(PIN_C0);

change to this

Code:
relay = read_adc();
relay = relay*(5/1023);

if (relay < 1.5)
{
 output_high(PIN_C0);
}
else if (relay > 4.5)
{
 output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
}
 

HTML:
Hi baryy

the problem is relay in continuously on the code is not working properly. If you have time i am sending proteus file please correct the code and simulate on proteus. I will be very thankful to you. 
thanks alot

- - - Updated - - -

HTML:
hi lockman_akim
thanks for help but it is still not working
 

Attachments

  • Lab 03.rar
    22.7 KB · Views: 66
  • Lab 03.rar
    22.7 KB · Views: 63

Is this a real circuit or a simulation you are having a problem with? It's totally unclear.

I am using pic c compiler for coding and it is a simulation on proteus. i just want to switch on the relay when voltage is under 1.5volts and above 4.5 volts.

1.5 is the lower limit
4.5 is the upper limit.
when ever voltage cross the limit the led should on.
thanks
 

Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <main.h>
#include<stdio.h>
#include<STDLIB.H>
 
#use delay(clock=11059200)
 
#fuses HS, NOWDT, NOLVP, NODEBUG
 
#define RELAY PIN_C0
 
#define LOWER_LIMIT 1.5
#define UPPER_LIMIT 4.5
 
float adcValue = 0.0, oldvalue = 0.0;
 
void main(void)
{
    setup_adc(ADC_CLOCK_INTERNAL);
    setup_adc_ports(AN0);
    set_adc_channel(0);
 
    while(1)
    {
        adcvalue = ((read_adc() * 5.0) / 1023.0);
 
        if(oldvalue != adcValue)
        {
            if((adcValue < LOWERLIMIT) || (adcValue > UPPER_LIMIT))
                output_high(RELAY);
            else
                output_low(RELAY);
 
                        oldValue = adcValue;                
        }
    }
}



- - - Updated - - -

i just want to switch on the relay when voltage is under 1.5volts and above 4.5 volts.

You should say that you want to turn ON the Relay when voltage is less than 1.5 V or greater than 4.5 V. It can't be < 1.5 V and > 4.5 V at the same time.

Your circuit is wrong. The Ceramic Capacitors used in the Crystal circuit should be 33 pF each and the diode in relay circuit should be connected across the relay coil terminals.
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…