12F675 relay control/small problems

Discussion in 'Embedded Systems and Microcontrollers' started by varden, Jul 11, 2016.

  1. varden

    Thread Starter New Member

    Jul 11, 2016
    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 :D
    I added photo and also codes of circuit.
    Code (Text):
    3. int okunan=0,okunan2=0,ort,ort2,top,top2;
    4. float milivolt,milivolt2;
    5. char i=0,k=0,txt[15];
    8. void kurulum() {
    10. ADC_Init();
    11. ADCON0=01000000;
    12. //ANSEL =00000001;
    15. TRISIO.GP5=0;
    16. GPIO.GP5  =0;
    17. TRISIO.GP2=0;
    18. GPIO.GP2  =0;
    19. //OSCCAL=0x80;
    22. }
    24. void main(){
    25. kurulum();
    27. while(1){
    28. for(i=0;i<20;i++)
    29. {
    30. okunan=ADC_Read(0);
    31. top+=okunan;
    32. delay_ms(10);
    33. }
    34. ort=top/20;
    35. top=0;
    36. milivolt=ort*4.8828125;
    38. if(milivolt < 2626){
    39. GPIO.GP5 = 0;
    40. GPIO.GP2 = 0;
    41. delay_ms(100);
    42. }
    43. if(milivolt > 2626){
    44. GPIO.GP5 = 1;
    45. GPIO.GP2 = 1;
    46. delay_ms(100);
    47. }
    48. while(1){
    49. for(i=0;i<20;i++)
    50. {
    51. okunan=ADC_Read(0);
    52. top+=okunan;
    53. delay_ms(10);
    54. }
    55. ort=top/20;
    56. top=0;
    57. milivolt=ort*4.8828125;
    59. if(milivolt < 2626){
    60. GPIO.GP5 = 0;
    61. GPIO.GP2 = 0;
    62. delay_ms(100);
    63. }
    64. if(milivolt > 2975 ){
    65. GPIO.GP5 = 1;
    66. GPIO.GP2 = 1;
    67. delay_ms(100);
    68. }
    69. }
    71. }
    72. }
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    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 likes this.
  3. varden

    Thread Starter New Member

    Jul 11, 2016
    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.
  4. jjw


    Dec 24, 2013
    What is the needed coil current?
    You could try smaller R2, e.g. 1kohm.
    varden likes this.
  5. Papabravo


    Feb 24, 2006
    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 likes this.
  6. varden

    Thread Starter New Member

    Jul 11, 2016
    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 :D
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    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>
    varden likes this.
  8. MrAl

    Well-Known Member

    Jun 17, 2014

    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?
    varden likes this.
  9. MaxHeadRoom


    Jul 18, 2013
    I prefer 2n7000 Fet(lington).
    varden likes this.
  10. varden

    Thread Starter New Member

    Jul 11, 2016
    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 :D :D Still student and we do not learn much about practical part in school,
    So I'm trying to learn myself.
  11. NorthGuy

    Active Member

    Jun 28, 2014
    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.
    varden likes this.