• AVR Freaks

Hot!Relay switch delay

Author
mateusz24052
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2018/10/18 02:21:41
  • Location: 0
  • Status: offline
2019/05/22 03:16:50 (permalink)
0

Relay switch delay

Gooday to you all,
 
I have designed a circuit (using PIC12F1822) for the purpose of closing the relay when the temperature has fallen below a certain level. The output of the comparator (to output 1 for temperature below and 0 for temperature for above set threshold). If the relay is closed (temperature below set threshold), the LED will flash on and off when relay is closed. What I would like to do now is add timer so that when the relay is closed, it will remain closed for 90 minutes. I have tried using for and delay but not sure if that works or is even a good idea.
 
How can this be done? The code I have is below:
 
#define _XTAL_FREQ 4000000

#include <xc.h>
#include <htc.h>

#define LED_RL LATAbits.LATA2
#define LED_ON LATAbits.LATA1
#define SW PORTAbits.RA4
#define RELAY LATAbits.LATA0

void main(void)
{
    OSCCON = 0b01101000 ;
    ANSELA = 0;
    
    TRISAbits.TRISA2 = 0;
    TRISAbits.TRISA4 = 1;
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA0 = 0;
    
    
    while(1) {
        
        if(SW == 1)
            
        {
        
        LED_RL = 1;
        __delay_ms(500);
        LED_RL = 0;
        __delay_ms(500);
        LED_ON = 1;
        RELAY = 1;
        
        }
        
        else
            
        {
            LED_RL = 0 ;
            LED_ON = 1;
            RELAY = 0;
        }
    }
    
    
    return;
}

#1

14 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 04:15:18 (permalink)
    +1 (1)
    What is the meaning of LED_RL and LED_ON?
     
    LED_RL is presumably controlling the flashing.
    So LED_ON is doing what?  You only ever set it to 1 - so nothing, it would seem!
     
    How accurate do you need your 90 minutes to be?
    Are you using the internal oscillator or a crystal?  Sorry - cannot be bothered to decode your OSCCON bits to find out, and you have not commented them.
     
    Probably you will want to use one of the PICs internal timers, and count timer rollovers (either via software polling or interrupts) to determine when 90 minutes have elapsed.
     
    Edit: for loop with a delay is fine, as long as you do not require great accuracy and you do not need to be doing anything else while waiting (other than flashing LED).
    post edited by pcbbc - 2019/05/22 04:17:16
    #2
    mateusz24052
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2018/10/18 02:21:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 04:26:46 (permalink)
    0
    LED_RL is the flashing
    LED_ON to show the device is on
     
    Not very accurate but around that is fine.
    Internal oscillator of 4MHz
     
    How can I do that? 
    #3
    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 04:59:32 (permalink)
    0
    mateusz24052
    LED_RL is the flashing

    No need to turn it OFF in the ELSE section then, as it will already be OFF at the end of the IF section.
     
    mateusz24052
    LED_ON to show the device is on

    No need to keep setting it to ON all the time then.  Just once, outside of your WHILE loop, will do.
    In fact, to show the device is ON with a LED you do not need an IO pin at all!
     
    mateusz24052
    Not very accurate but around that is fine.
    Internal oscillator of 4MHz

    Just a loop around your 1 second ON/OFF LED flash will do then.
    for (int i = 0; i < 90 * 60; i++)
     
    {
     
        //...
    }

    Edit: Note - you will need to turn the relay ON before you start flashing the LED.
    post edited by pcbbc - 2019/05/22 05:01:51
    #4
    mateusz24052
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2018/10/18 02:21:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 05:49:19 (permalink)
    0
    Thank you for the reply and this is what I now have.
     
    #define _XTAL_FREQ 4000000
     
    #include <xc.h>
    #include <htc.h>
     
    #define LED_RL LATAbits.LATA2
    #define SW  PORTAbits.RA4
    #define RELAY LATAbits.LATA0
     
    void main(void)
    {
        OSCCON = 0b01101000 ;
        ANSELA = 0;
        
        TRISAbits.TRISA2 = 0;
        TRISAbits.TRISA4 = 1;
        TRISAbits.TRISA1 = 0;
        TRISAbits.TRISA0 = 0;
        
        while(1) {
            
            if(SW == 1)
                
            {
                 RELAY = 1;   
        for (int i = 0; i < 90 * 60; i++)
     
            {
            LED_RL = 1;
            __delay_ms(500);
            LED_RL = 0;
            __delay_ms(500);
            
            }        
            
            }
            
            else
                
            {
                LED_RL = 0 ;
                RELAY = 0;
            }
        }
        
        
        return;
    }

     
    However it doesn't seem to work because therelay turns off when the output has gone back to 0 before 90 minutes. What could it be?
    #5
    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 07:43:40 (permalink)
    0
    After how long?
    Does the LED flash for a bit?  Or not at all?
    What period is the LED flashing with?
     
    Most likely watchdog timer is enabled and resetting CPU I think?
    Where are your configuration bits?
    #6
    NorthGuy
    Super Member
    • Total Posts : 5441
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Relay switch delay 2019/05/22 07:52:56 (permalink)
    0
    If you want to add hysteresis, instead of a delay, simply make the "ON" threshold somehow lower than the "OFF" threshold. Or you can use PID.
    #7
    mateusz24052
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2018/10/18 02:21:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 07:54:49 (permalink)
    0
    After a few seconds.
     
    Yes it does but when the output comparator is high it goes to the else.
     
    It is flashing off 500ms and on 500ms
     
    This is my config:
     
     

    // PIC12F1822 Configuration Bit Settings
    // 'C' source line config statements
    // CONFIG1
    #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
    #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
    #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
    #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
    #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
    #pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
    #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
    #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
    // CONFIG2
    #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
    #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
    #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
    #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
    #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #include <xc.h>

    #8
    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/22 09:05:59 (permalink)
    0
    It can not be "going to the else" if SW (PORTAbits.RA4) is still HIGH.
    Not with the code you posted, anyway.
     
    If the flashing is 500ms ON and 500ms off, but not for 5400 cycles (approx 90 minutes) then my guess is the PIC is spontaneously resetting.
    If, at the point it resets, SW (PORTAbits.RA4) is LOW, then yes - it will "go to the else".
    As you have configured WDTE=OFF and MCLRE=OFF, that leaves only a power glitch to cause a reset.
     
    NorthGuy's suggestion of some hysteresis could also be valid, although it does not explain why your 90 minute delay is not 90 minutes.
    #9
    mateusz24052
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2018/10/18 02:21:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/23 00:23:26 (permalink)
    0
    How would you mezn for the first part? "It can not be "going to the else" if SW (PORTAbits.RA4) is still HIGH"
     
    The resetting?
     
    The power glitch could be because I have split a USB cable to supply 5V
    #10
    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/23 01:41:00 (permalink)
    0
    mateusz24052
    How would you mezn for the first part? "It can not be "going to the else" if SW (PORTAbits.RA4) is still HIGH"
     
    The resetting?
     
    The power glitch could be because I have split a USB cable to supply 5V




    You said in post #8:
    mateusz24052Yes it does but when the output comparator is high it goes to the else.

    The output from the comparator is supplied to RA4 - Correct?
    My reply was there is no way your code (as posted in post #1 or #5) can be executing the ELSE section if RA4=1.
     
    Conclusion: RA4=0 at some point, or that is not the code you are running.
    That conclusion stands regardless of if the chip is resetting or not.
    The resetting is the probably cause of it leaving the "for" loop early before 5,400 iterations.
    It cannot be the reason why it is going in the ELSE.  That must be because RA4=0.
    #11
    mateusz24052
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2018/10/18 02:21:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/23 02:46:20 (permalink)
    0
    I understand.
     
    RA4 is the output of the comparator correct. If high the led will flash and relay close. If low the led will turn off and relay will be open.
     
    I don't understand your conclusion? Are you saying that the MCU is resetting itself?
     
    I understand the for loop for the delay.
     
    What would you recommend?
    #12
    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/23 03:49:41 (permalink)
    0
    If the for loop is not waiting for 90 minutes, or 60*90 = 5400 cycles, then the MCU resetting is the only answer.
    There is no other way out of this code without a reset:
    for (int i = 0; i < 90 * 60; i++)
    {
        LED_RL = 1;
        __delay_ms(500);
        LED_RL = 0;
        __delay_ms(500);
    }

    #13
    mateusz24052
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2018/10/18 02:21:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/23 05:30:58 (permalink)
    0
    Thank you for the feedback. I will have a look into it and understand why it is resetting. Have you got any idea why?
    #14
    pcbbc
    Super Member
    • Total Posts : 1008
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Relay switch delay 2019/05/23 05:33:22 (permalink)
    +1 (1)
    mateusz24052
    Thank you for the feedback. I will have a look into it and understand why it is resetting. Have you got any idea why?

    Dirty power supply is the most likely cause.  Post a circuit diagram.
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5