# 12F675 relay control/small problems

#### varden

Joined Jul 11, 2016
38
Hello. I tried to build a circuit that controls relay according to voltage. But I have small problems and hope you can help me about them. The main purpose of circuit, at first opening it measures the main voltage and if it is higher than 10.5V it gives that voltage to relay until voltage drops under 10.5V. İf drops below 10.5 cuts. And after voltage gets higher than 12V, it gives power to relay, again. İnfinity loop. But my circuit doesn't do that initial measuring, always close relay at initial. And also, sometimes it doesn't work according to my voltage values. I mean cuts at 11V and opens at 12.30V. If you can see what I am doing wrong, it would really help me
I added photo and also codes of circuit.
Code:
int okunan=0,okunan2=0,ort,ort2,top,top2;
float milivolt,milivolt2;
char i=0,k=0,txt[15];

void kurulum() {

//ANSEL =00000001;

TRISIO.GP5=0;
GPIO.GP5  =0;
TRISIO.GP2=0;
GPIO.GP2  =0;
//OSCCAL=0x80;

}

void main(){
kurulum();

while(1){
for(i=0;i<20;i++)
{
top+=okunan;
delay_ms(10);
}
ort=top/20;
top=0;
milivolt=ort*4.8828125;

if(milivolt < 2626){
GPIO.GP5 = 0;
GPIO.GP2 = 0;
delay_ms(100);
}
if(milivolt > 2626){
GPIO.GP5 = 1;
GPIO.GP2 = 1;
delay_ms(100);
}
while(1){
for(i=0;i<20;i++)
{
top+=okunan;
delay_ms(10);
}
ort=top/20;
top=0;
milivolt=ort*4.8828125;

if(milivolt < 2626){
GPIO.GP5 = 0;
GPIO.GP2 = 0;
delay_ms(100);
}
if(milivolt > 2975 ){
GPIO.GP5 = 1;
GPIO.GP2 = 1;
delay_ms(100);
}
}

}
}

#### shteii01

Joined Feb 19, 2010
4,644
Sounds like you have uC starup problem. When uC is designed, the designers assign the initial state to the pins. Some uC has all pins go high, some have all the pins go low. Then the programming kicks in and the pins change state according to the program that you, the user, wrote.

#### varden

Joined Jul 11, 2016
38
Thank you shteii01 but I didn't understand. I already assigned them as 0 and leave the changing part to program. By the way program works just fine on proteus but in build circuit, it doesn't.

#### jjw

Joined Dec 24, 2013
798
What is the needed coil current?
You could try smaller R2, e.g. 1kohm.

#### Papabravo

Joined Feb 24, 2006
19,563
Thank you shteii01 but I didn't understand. I already assigned them as 0 and leave the changing part to program. By the way program works just fine on proteus but in build circuit, it doesn't.
You don't quite understand. There is an interval of time between when the processor is powered up, comes out of RESET, and when the first instruction is executed when the behavior of an IO pin is governed by a default configuration. The default behavior is fixed and you cannot change it during this interval. The external circuitry must take account of this and provide for it, until the program can begin execution and change it to the way you want. You can find the default RESET configuration of each pin in the datasheet.

#### varden

Joined Jul 11, 2016
38
Thank you all for answers and interest. I couldn't think that before. I will try to fix it and may bother you again if can't fix it

#### ErnieM

Joined Apr 24, 2011
8,346
It is possible the relay is causing some sort of power upset when it transitions. You may want to try removing the relay portion (just open the base or collector) and see if the led alone gives the correct states.

Later we can get into how the use of floating point numbers is completely unnecessary in this application. <grin>

#### MrAl

Joined Jun 17, 2014
9,623
Hello,

I was going to comment on the 10k base resistor too. Probably has to be smaller like 5k, 2k, or 1k.

I can also comment on the potentiometer. That is not a good place to use a potentiometer. use a fixed resistor instead. Pots open up sometimes and that means the uC chip will get a 12v voltage applied to one of the i/o pins and blow it out. Pots also become momentarily disconnected between the arm and the element, which also then looks like an open circuit, which then places 12v on the i/o pin momentarily. So change that to a fixed resistor and hope nothing blew out already or else replace the uC chip.

Also, as someone else said, when the relay coil is turned 'on' it will cause a current surge from the power supply, which could reduce the voltage momentarily this means you might have to isolate that from the rest of the circuit with a diode going to the 5v regulator, and a decent cap like 10uf or 100uf or even 1000uf for the input of the 5v regulator. That's only if it causes a problem when it turns on however.
This is the same problem that can occur if the LOAD is switched and it draws significant current from the 12v supply. Check that too.

Other than that, check the i/o pin that drives the transistor base to make sure it comes up to at least 3.5v so that it can turn the transistor on well enough when the base resistor is a lower value, or whatever value you choose to use.
Check the saturation voltage of the transistor collector to emitter to make sure it is 1v or less, typically 0.5v or less at room temperature.

Of you intend to use this in an automobile then you should test over a wide range of temperatures to make sure it works well, typically -20 degrees F to 140 degrees F but higher is even better, like 180 degrees F.

Any reason why you need a relay and not just a transistor?

Joined Jul 18, 2013
26,254
I prefer 2n7000 Fet(lington).
Max.

#### varden

Joined Jul 11, 2016
38
I really thank you for all answers and useful tips. I will check them.
I used relay because I will use this with 12V 80Ah dry battery. Will transistor do the same job?
And I think I'd better say I am kind of beginner in this area Still student and we do not learn much about practical part in school,
So I'm trying to learn myself.

#### NorthGuy

Joined Jun 28, 2014
611
Before you do your first measurement, you do not initialize the "top" variable. The C standard requires the compiler to set all non-initialized variables to zero, but who knows ...

You don't really need an MCU. An op-amp can do the job and might be easier to deal with.