Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Relay causes a problem to Button's functioning.

Status
Not open for further replies.

alphacat

New Member
I implemented a mechanism where when you press a button for 3-sec, the latching relay switches its state.
The button's interrupt is falling-edge triggered.

The mechanism goes like this:
1. Button is pressed.
2. A timer checks every 100ms whether the button is still press.
3. If after 4 checks or less, the button is not pressed (i.e. a momentary pressing), a LED blinks.
4. If after 30 checks, the button is still pressed, the relay is switched.
5. Mechanism is stopped until next press.

1. Problem #1 is that when I press the button for 6sec (nonstop), The relay switches its state firstly after 3sec, and secondly after another 3sec.

However, it isnt supposed to happen according to the mechanism I wrote.
Moreover, When I debug the unit, without enough voltage for the relay driver to operate, and press the button for 6sec, the unit commands the relay to switch only once.

2. Problem #2 is that sometimes when I lift my finger from the button, after the relay was switched, it simulates a momentary pressing on the button.
I know that since when a momentary pressing happen, the LED blinks.

How can I solve these problems?

Thank you.
 
Last edited:
Step 5. Mechanism is stopped until next press. You should stop until the first press is released.
 
1. Wait for a button press.
1.a. Button is debounced.
1.b. de-latch the relay.
2. A timer checks every 100ms whether the button is still press.
2.a. The increment variable is incremented
3. If increment <= 4 AND the button is not pressed, a LED blinks, AND the increment variable resets to 0.
4. If increment >= 30 AND the button is pressed, the relay latches, AND the increment variable resets to 0
5. Wait for the button release
5.a. Go back to 1 (to wait for a new press)
 
Last edited:
Thank you very much.

How shall I wait for the button to be released?
What shall I do until then, and when it happens?

What is considered a release of a button?
I mean, the relay's toggling could create spikes that could mislead me to thinking that the button was released for a breif moment.
 
Hello.

You can deal with the relay coil spiking with a freewheeling diode (look at my blog).

It is very important to debounce the switch (better if done via software), to prevent false triggerings.

Ok, imagine a switch circuit:
+V --- R --§-- SWITCH --- GND.

Where the input is at §.

When you press the swtich, § goes down to 0V, when you release the switch § goes up to +V.

How can you wait for the button to be released? Algorithm:
SENSE_RELEASE:
WHILE § == 0, GOTO SENSE_RELEASE

Observe that, while you keep the button pressed, your state machine enters in a LOOP. It will only get out of the loop if you release the button.

And how can you wait for the button to be pressed? Algorithm:
SENSE_PRESS:
WHILE § == 1, GOTO SENSE_PRESS

Observe that, while you keep the button released, your state machine enters in a LOOP. It will only get out of the loop if you press the button.

I used algorithms to describe, you can apply that to an uC, a state machine...
 
Hey.
Thanks again.

This method is not very practical since you woundn't want to wait inside a loop for a button to be pressed or released, because you would want to release the Uc to perform its other tasks.

So, i'm just not sure about how to sense the release.
Once 3 seconds have passed and the relay was switched, however the button is still pressed, shall I keep checking every 100ms whether the button is still pressed, and once it is released, wait another 100ms to see that its still released? (so I'd know that this release wasnt some spike caused by the relay's switching).
 
Last edited:
Just do what I've suggested, but instead of locking the uC in a loop, sample the input.

For example, what I have suggested is composed by 9 steps (9 states). You gonna need to memorize the present state. And use a Timer Interrupt:

Code:
On Timer Interrupt (100 ms){
    (STATE == 0){
         ButtonPressed?{

             TRUE{
                  Delatch the relay.
                  STATE = 1.
                  ReturnFromInt;
                       }

             FALSE{
                  Return from interrupt
                       }
                                   }
                             }

    (STATE == 1){
          ButtonPressed?{

             TRUE{
                 Increment the count variable.
                         IsCountVariable >= 30?{
                                  TRUE{
                                       Latch the Relay
                                       count variable = 0;
                                       State = 2;
                                       ReturnFromInt;}

                                   FALSE{
                                       ReturnFromInt;}
                                                                }
                           }

                FALSE{
                           IF COUNT VARIABLE <= 4 {BlinkTheLed;}

                           Count variable = 0;
                           State = 0;
                           ReturnFromInt;
                          } 
                                    }

    (STATE == 2){
          ButtonPressed?{
                
                  TRUE{
                        ReturnFromInt
                          }  
                  
                   FALSE{
                         State = 0;
                         ReturnFromInt;
                               }
                         }
}

Can you understand it?
 
Last edited:
Well, the state is a "memory" to remind you the present situation (status) among a collection of chronological succeded events or another kind of events.
 
Thank you pal.

I understand you program now, its great.
There is one difference however between my program's configuration and yours.

When the button is not pressed, I dont check it every 100ms to see when its pressed, but uses an interrupt.
The button's pin is connected to an input pin of the Uc, so when an interrupt happens the Uc vectors to the corresponding ISR, and then I check every 100ms whether the button is pressed.
Do any other cautions need to be taken due to this fact?
 
Well, you must debounce the switch to prevent false triggerings.

With sampling, you don't need to debounce.
 
Hey,
Thank you very much again.
Great help.

Yes I already have the button debounced.

I have one more question please about your program.
When the relay is switched, you go to STATE_2.
However, I noticed that when the relay is switched, the button's VCC goes low and high several times.
It might makes you think that the button is not pressed, and instead of staying at STATE_2, you'll go back to STATE_0.
Isnt it?
 
Last edited:
As far I understood the Switch's VCC is bouncing when you activate the relay?

It is not right. You must add some bypass capactior to your PSU. (some 100nF between the VCC line and GND, to filter the spikes produced by the relay's coil). Are you using a snubber capacitor or freewheeling diode to the relays coil?
 
Hey.

I'm using FDC6420C in my H-bridge, which has the following built-in diodes inside:

**broken link removed**

**broken link removed**

Are they ok?

I dont have a bypass capacitor at the VCC (its a 5V regulator connected to the output of a mains-to-7.5V buck converter), only a 10uF capacitor.
Why do these spikes happen?
 

Attachments

  • FDC6420C..pdf
    102.6 KB · Views: 191
  • DIODESMOS..png
    DIODESMOS..png
    26.3 KB · Views: 136
  • MOSFETS..png
    MOSFETS..png
    8.6 KB · Views: 137
Last edited:
Those are built in MOSFET protection diodes, they do not prevent spikes from the relay coil to contamine the Supply line. (Did you looked at my blog?). You must use a freewheeling diode on your relay.

Take a look at the blog.
 
Hey.
Thanks for the reference to your blog!
I just love to read such detailed articles with graphs, they are very rare.

The built-in diode in the MOSFET just protects the N-ch MOSFET against possitive voltage from source to drain, am I correct? (same to the P-ch one).
And it doesnt protect it against high positive voltage from drain to source, as happens when the MOSFET is switched OFF, yes?

These spikes happen because the switch oscillates between allowing current flowing and disallowing current flowing several times?
What makes the voltage eventually decreasing in a form of a capacitor's discharge?
**broken link removed**

About adding a protection diode as was advised in your blog, how do you connect protection diodes to a single-coil latching relay, that current flows through it in both directions?
(An H-bridge comprises 4 MOSFETS drives that relay).

Thank you fellow.
 

Attachments

  • Pure..jpg
    Pure..jpg
    41 KB · Views: 139
Last edited:
The built-in diode in the MOSFET just protects the N-ch MOSFET against possitive voltage from source to drain, am I correct? (same to the P-ch one).
The intrinsic diode will conduct in this direction, but in a unipolar switch there isn't such a current to "protect" from.

And it doesnt protect it against high positive voltage from drain to source, as happens when the MOSFET is switched OFF, yes?
Correct.

About adding a protection diode as was advised in your blog, how do you connect protection diodes to a single-coil latching relay, that current flows through it in both directions?
(An H-bridge comprises 4 MOSFETS drives that relay).
An H-drive uses 4 diodes, one across each transistor. You probably have already seen them. In some designs the intrinsic diodes are used and you won't see separate devices.

It helps me to call the diode the "intrinsic diode" to remind me that the diode built in to a MOSFET may, or may not, be suitable for other purposes even if it happens to be located conveniently. The data sheet tells me what use I can make of the diode.
 
Last edited:
Thank you guys for your help.
I'll test it again with diodes connected.

About the button's code.
I really thank you Hayato, you helped me big time, I tried it out now and its working great.
I really can't thank you enough!

I feel so stupid for not thinking of this idea of waiting for the button to be released :(
 
Last edited:
Hey again.

I'm experiencing this weird problem, and thought I could get an advice.

When I power up the unit, the button works great.
However, after a few minutes, I experience problems with the button:
- the relay doesnt get switched.
- when I release the button, after a long pressing, the LED blinks, as if it was a short pressing.

It never happens at the beginning of the unit's operation (right after power-up).
It happens a few minutes afterwards, and remains like this until I power off and on the unit.

What could cause such situation?

Thank you.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top