need help for connecting relay to PIC

Discussion in 'Embedded Systems and Microcontrollers' started by arashian1, Sep 19, 2016.

  1. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    i made a timer with PIC that include 2 digit 7segment and three pushbuttons for setting time
    this operate well and completly that i programed and test on breadboard
    any number taht user sets with pushbuttons it begin to decrease each 1 second and when reaches zero the output is on
    but when i connect relay (bobin 5 volt) circuit to output of micro after reach zero it works again (like a loop).

    whats the problem?

    the circuit attached in pdf file.
     
  2. be80be

    Senior Member

    Jul 5, 2008
    432
    57
    Can't open the file
     
  3. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    thanks
     
  4. NorthGuy

    Active Member

    Jun 28, 2014
    605
    121
    Most likely PIC resets because of

    Make sure the relay has separate connection to the ground. Put a big capacitor (up to 100 uF) on PIC power as close to the PIC as possible.
     
  5. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    how connect capacitor?
     
  6. be80be

    Senior Member

    Jul 5, 2008
    432
    57
    You need to pull up MCLR with 10 K
     
  7. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    You need a resistor, 10k, 22k, something like that, from MCLR to +5V

    @be80be beat me to it!.
     
  8. NorthGuy

    Active Member

    Jun 28, 2014
    605
    121
    Between VDD and VSS.
     
  9. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    in breadboard i connected MCLR with 10 K to +5 volt
     
  10. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    in breadboard i connected MCLR with 10 K to +5 volt
     
  11. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    i must test it
     
  12. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    in proteus it work true but after test it on breadboard its not work true
     
  13. jayanthd

    Member

    Jul 4, 2015
    274
    29
    The D3 and D4 is shorting the PIC Pins with ground. Please add a 330E 1/4W resistors in series with D3 and D4. Put a 100nF 50V Ceramic Capacitor in parallel with the diode which is connected across the relay coil.
     
  14. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    D2 and D5 also do not appear to have the resistors they should have and they would almost short the 5V supply.
     
  15. jayanthd

    Member

    Jul 4, 2015
    274
    29
    And yo7u can't directly drive the 7 Segment Display [ SSD ] from PIC. Yo7u need to use two BC337 transistors to drive the common cathode pins of the displays. PIC cannot source or sink more than 25 mA per pin. If all the 7 segment of one display is ON at a time and assuming 10 mA current is set to each segment then 70 mA current will flow through the common cathode pin of the display and you have connected it directly to PIC pin. Please make changes to the circuit.
     
  16. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    389
    And each segment of the display also needs a current limiting resistor.
     
  17. jayanthd

    Member

    Jul 4, 2015
    274
    29
    And you need to connect a 10 uF 6.3V and 100nF 16V accross the PIC power pins and close to PIC power pins on the PCB.
     
  18. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,571
    2,381
    Also Consider using 2n7000 instead of bipolar.!
    Max.
     
  19. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    thanks
    with capacitor it worked correctly
     
  20. jayanthd

    Member

    Jul 4, 2015
    274
    29
    Use SumatraPDF 3.0

    Try the attached project. See PDF files for Schematic if Proteus file doesn't open. The counter seems to run a bit slower but it is only in Simulation due to 2ms timer interrupt. In hardware it will work fine. R13 and R14 are only needed in Simulation and not required in hardware.

    Code (Text):
    1. sbit Set_Or_Run_Button at RB0_bit;
    2. sbit Increment_Button  at RB1_bit;
    3. sbit Decrement_Button  at RB2_bit;
    4.  
    5. sbit LED   at RD6_bit;
    6. sbit RELAY at RD7_bit;
    7.  
    8. #define ON  1
    9. #define OFF 0
    10.  
    11. #define SET   1
    12. #define CLEAR 0
    13.  
    14. #define TRUE  1
    15. #define FALSE 0
    16.  
    17. #define SET_OR_RUN_BUTTON_PRESSED (Set_Or_Run_Button == 0)
    18. #define INCREMENT_BUTTON_PRESSED  (Increment_Button == 0)
    19. #define DECREMENT_BUTTON_PRESSED  (Decrement_Button == 0)
    20.  
    21. unsigned char myFlags = 0, select = 0;
    22. unsigned char cc_mask[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
    23. unsigned char digits[2] = {0, 0};
    24. signed char delay_value_in_seconds = 0, counter = 0;
    25. unsigned int two_x_milli_second_counter = 0;
    26.  
    27. sbit mode_flag at myFlags.B0;
    28. sbit count_down_flag at myFlags.B1;
    29.  
    30. #define SET_MODE (mode_flag == 1)
    31. #define RUN_MODE (mode_flag == 0)
    32.  
    33. //Timer1
    34. //Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 2 ms
    35. //Place/Copy this part in declaration section
    36. void InitTimer1() {
    37.     T1CON = 0x01;
    38.     TMR1IF_bit = 0;
    39.     TMR1H = 0xF8;
    40.     TMR1L = 0x30;
    41.     TMR1IE_bit = 1;
    42.     INTCON = 0xC0;
    43. }
    44.  
    45. void Interrupt() {
    46.     if(TMR1IF_bit) {
    47.         //Enter your code here
    48.         PORTC = 0x00;
    49.         PORTE = (PORTE & 0x04);
    50.  
    51.         switch(select) {
    52.             case 0:
    53.                   PORTC = digits[0];
    54.                   PORTE = (PORTE & 0x04) | 0x01;
    55.                   break;
    56.             case 1:
    57.                   PORTC = digits[1];
    58.                   PORTE = (PORTE & 0x04) | 0x02;
    59.                   break;
    60.         };
    61.  
    62.         if(++select == 2) {
    63.             select = 0;
    64.         }
    65.  
    66.         if(count_down_flag) {
    67.             if(++two_x_milli_second_counter == 500) {
    68.                  if(--delay_value_in_seconds == 0) {
    69.                      RELAY = ON;
    70.                      count_down_flag = CLEAR;
    71.                  }
    72.            
    73.                  two_x_milli_second_counter = 0;
    74.             }
    75.         }
    76.  
    77.         TMR1IF_bit = 0;
    78.         TMR1H = 0xF8;
    79.         TMR1L = 0x30;
    80.     }
    81. }
    82.  
    83. void main() {
    84.  
    85.     CMCON = 0x07;
    86.  
    87.     TRISA = 0x00;
    88.     TRISB = 0x07;
    89.     TRISC = 0x00;
    90.     TRISD = 0x00;
    91.     TRISE = 0x00;
    92.  
    93.     PORTA = 0x00;
    94.     PORTB = 0x00;
    95.     PORTC = 0x00;
    96.     PORTD = 0x00;
    97.     PORTE = 0x00;
    98.  
    99.     Delay_ms(100);
    100.  
    101.     InitTimer1();
    102.  
    103.     while(1) {
    104.  
    105.           if(SET_OR_RUN_BUTTON_PRESSED) {
    106.               Delay_ms(30);
    107.               while(SET_OR_RUN_BUTTON_PRESSED);
    108.        
    109.               mode_flag = ~mode_flag;
    110.        
    111.               if(RUN_MODE) {
    112.                   count_down_flag = 1;
    113.                   RELAY = OFF;
    114.               }
    115.               else if(SET_MODE) {
    116.                   count_down_flag = 0;
    117.               }
    118.           }
    119.    
    120.           if(SET_MODE) {
    121.                if(INCREMENT_BUTTON_PRESSED) {
    122.                    Delay_ms(20);
    123.                    if(INCREMENT_BUTTON_PRESSED) {
    124.                         if(++delay_value_in_seconds > 99) {
    125.                              delay_value_in_seconds = 0;
    126.                         }
    127.                    }
    128.                }
    129.                else if(DECREMENT_BUTTON_PRESSED) {
    130.                    Delay_ms(20);
    131.                    if(DECREMENT_BUTTON_PRESSED) {
    132.                         if(--delay_value_in_seconds < 0) {
    133.                              delay_value_in_seconds = 99;
    134.                         }
    135.                    }
    136.                }
    137.           }
    138.    
    139.           if((delay_value_in_seconds >= 0) && (delay_value_in_seconds < 10)) {
    140.               digits[0] = 0;
    141.               digits[1] = cc_mask[delay_value_in_seconds];
    142.           }
    143.           else if((delay_value_in_seconds >= 10) && (delay_value_in_seconds < 100)) {
    144.               digits[0] = cc_mask[delay_value_in_seconds / 10];
    145.               digits[1] = cc_mask[delay_value_in_seconds % 10];
    146.           }
    147.     }
    148. }
    See image for Circuit. Circuit.png
     
    Last edited: Sep 20, 2016
Loading...