I
I
idin2018-03-05 22:53:03
Arduino
idin, 2018-03-05 22:53:03

How to remove interference from relay in atmega328p (arduino) during interrupt?

I am now trying to figure out interrupts in the AVR
. There is such a scheme (I’ll say in advance I don’t know how to draw a relay on the diagram):
5a9d9cc84b364994714844.png
And there is this code:

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

void setting(void){
  //Настройка всех портов на выход
  DDRC = 0xFF;
  //Устанавливаем везде 0
  PORTC = 0x00;

  //Настройка всех портов на вход
  DDRD = 0x00;
  // Подтягиваем резистор чтобы еденица четко читалась
  PORTD|= 0xFF;

  EIMSK |= (1 << INT0);     // Turns on INT0
  EICRA |= 0b00000011; // set INT0 to trigger on ANY logic change
    

}

ISR (INT0_vect){
    _delay_ms(100);
    	PORTC = (1<<PIN0);
    _delay_ms(10000);
    PORTC &=~(1<<PIN0);
    _delay_ms(2000);
    
}


int main(){
  setting();
    while(1) {
    		cli();
      	_delay_ms(1000);
      	sei();
      	_delay_ms(1000);
    }
    return 0;
}

And everything works with 1 diode. (that is, I hold down the button, nothing happens, since I made a response to the rising front, I press it after a while, everything works). If you connect a relay with a load, then the interruption occurs constantly. If the load is removed, then sometimes it works normally, sometimes it also freezes for three or more cycles. As I understand it, this is interference similar to interference from the rattling of a button, but from a relay and a load. And my question is the following. How can I get around this mess. Or I have a problem in the code.
Thanks in advance.
PS And how to add the C language tag to the toaster? I don't seem to be able to.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Syomov, 2018-03-05
@Izy

Do you have a relay winding hanging directly on the legs of the controller? This is not correct, of course.
Even a very small relay has a rather small pickup and hold current. Much more than the controller can give. And also a large induction, which, when the voltage is removed, gives a surge.
A power switch on a transistor is required between the relay winding and the controller output.
And the surge of current when the relay is released can be extinguished, for example, on a diode connected in parallel to the winding.
You can google the diagrams for "connecting a relay to a microcontroller".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question