need help for connecting relay to PIC

Thread Starter

arashian1

Joined Sep 9, 2016
39
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.
 

Attachments

NorthGuy

Joined Jun 28, 2014
611
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.
 

jayanthd

Joined Jul 4, 2015
902
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.
 

jayanthd

Joined Jul 4, 2015
902
D2 and D5 also do not appear to have the resistors they should have and they would almost short the 5V supply.
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.
 

jayanthd

Joined Jul 4, 2015
902
Can't open the file
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:
sbit Set_Or_Run_Button at RB0_bit;
sbit Increment_Button  at RB1_bit;
sbit Decrement_Button  at RB2_bit;

sbit LED   at RD6_bit;
sbit RELAY at RD7_bit;

#define ON  1
#define OFF 0

#define SET   1
#define CLEAR 0

#define TRUE  1
#define FALSE 0

#define SET_OR_RUN_BUTTON_PRESSED (Set_Or_Run_Button == 0)
#define INCREMENT_BUTTON_PRESSED  (Increment_Button == 0)
#define DECREMENT_BUTTON_PRESSED  (Decrement_Button == 0)

unsigned char myFlags = 0, select = 0;
unsigned char cc_mask[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
unsigned char digits[2] = {0, 0};
signed char delay_value_in_seconds = 0, counter = 0;
unsigned int two_x_milli_second_counter = 0;

sbit mode_flag at myFlags.B0;
sbit count_down_flag at myFlags.B1;

#define SET_MODE (mode_flag == 1)
#define RUN_MODE (mode_flag == 0)

//Timer1
//Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 2 ms
//Place/Copy this part in declaration section
void InitTimer1() {
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0xF8;
    TMR1L = 0x30;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void Interrupt() {
    if(TMR1IF_bit) {
        //Enter your code here
        PORTC = 0x00;
        PORTE = (PORTE & 0x04);
  
        switch(select) {
            case 0:
                  PORTC = digits[0];
                  PORTE = (PORTE & 0x04) | 0x01;
                  break;
            case 1:
                  PORTC = digits[1];
                  PORTE = (PORTE & 0x04) | 0x02;
                  break;
        };
  
        if(++select == 2) {
            select = 0;
        }
  
        if(count_down_flag) {
            if(++two_x_milli_second_counter == 500) {
                 if(--delay_value_in_seconds == 0) {
                     RELAY = ON;
                     count_down_flag = CLEAR;
                 }
           
                 two_x_milli_second_counter = 0;
            }
        }
  
        TMR1IF_bit = 0;
        TMR1H = 0xF8;
        TMR1L = 0x30;
    }
}

void main() {

    CMCON = 0x07;

    TRISA = 0x00;
    TRISB = 0x07;
    TRISC = 0x00;
    TRISD = 0x00;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    Delay_ms(100);

    InitTimer1();

    while(1) {

          if(SET_OR_RUN_BUTTON_PRESSED) {
              Delay_ms(30);
              while(SET_OR_RUN_BUTTON_PRESSED);
        
              mode_flag = ~mode_flag;
        
              if(RUN_MODE) {
                  count_down_flag = 1;
                  RELAY = OFF;
              }
              else if(SET_MODE) {
                  count_down_flag = 0;
              }
          }
    
          if(SET_MODE) {
               if(INCREMENT_BUTTON_PRESSED) {
                   Delay_ms(20);
                   if(INCREMENT_BUTTON_PRESSED) {
                        if(++delay_value_in_seconds > 99) {
                             delay_value_in_seconds = 0;
                        }
                   }
               }
               else if(DECREMENT_BUTTON_PRESSED) {
                   Delay_ms(20);
                   if(DECREMENT_BUTTON_PRESSED) {
                        if(--delay_value_in_seconds < 0) {
                             delay_value_in_seconds = 99;
                        }
                   }
               }
          }
    
          if((delay_value_in_seconds >= 0) && (delay_value_in_seconds < 10)) {
              digits[0] = 0;
              digits[1] = cc_mask[delay_value_in_seconds];
          }
          else if((delay_value_in_seconds >= 10) && (delay_value_in_seconds < 100)) {
              digits[0] = cc_mask[delay_value_in_seconds / 10];
              digits[1] = cc_mask[delay_value_in_seconds % 10];
          }
    }
}
See image for Circuit.Circuit.png
 

Attachments

Last edited:
Top