Tranzystor.pl – Vortal Elektroniczny

Arduino – sterowanie przekaźnikami

W tym szybkim tutorialu Arduino opiszę w jaki sposób można sterować przekaźnikami za pomocą Arduino. Potrzebne nam będą jeden rezystor 10kΩ, moduł przekaźnika, Po naciśnięciu przycisku nasz przekaźnik włączy się i pozostanie w tym stanie aż do momentu ponownego naciśnięcia przycisku.

Wykaz elementów:

Arduino np. nano
rezystory 10kΩ x8
przyciski x 8
płytka uniwersalna
cyna oraz lutownica

Kod programu (dla 1 przekaźnika)

/* karta przekaźnikowa pkx1
by swistak
www.tranzystor.pl
2016
*/

int pinButton1 = A0;
int Relay1 = 2;

int stateRelay1 = HIGH;

int stateButton1;

int previous = LOW;
long time = 0;
long debounce = 500;

void setup() {
pinMode(pinButton1, INPUT);
pinMode(Relay1, OUTPUT);
}

void loop() {
stateButton1 = digitalRead(pinButton1);
if(stateButton1 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay1 == HIGH){
stateRelay1 = LOW;
} else {
stateRelay1 = HIGH;
}
time = millis();
}
digitalWrite(Relay1, stateRelay1);
previous == stateButton1;

}

Docelowo potrzebowałem wysterować 8 przekaźnikami.Wszystko w celu rozbudowy już istniejącej automatyki domowej.

Kod programu (dla 8 przekaźników)

/* karta przekaźnikowa pkx1-8
by swistak
www.tranzystor.pl
2016
*/

int pinButton1 = A0;
int Relay1 = 2;
int pinButton2 = A1;
int Relay2 = 3;
int pinButton3 = A2;
int Relay3 = 4;
int pinButton4 = A3;
int Relay4 = 5;
int pinButton5 = A4;
int Relay5 = 6;
int pinButton6 = A5;
int Relay6 = 7;
int pinButton7 = 13;
int Relay7 = 8;
int pinButton8 = 12;
int Relay8 = 9;

int stateRelay1 = HIGH;
int stateRelay2 = HIGH;
int stateRelay3 = HIGH;
int stateRelay4 = HIGH;
int stateRelay5 = HIGH;
int stateRelay6 = HIGH;
int stateRelay7 = HIGH;
int stateRelay8 = HIGH;

int stateButton1;
int stateButton2;
int stateButton3;
int stateButton4;
int stateButton5;
int stateButton6;
int stateButton7;
int stateButton8;

int previous = LOW;
long time = 0;
long debounce = 500;

void setup() {
pinMode(pinButton1, INPUT);
pinMode(Relay1, OUTPUT);

pinMode(pinButton2, INPUT);
pinMode(Relay2, OUTPUT);

pinMode(pinButton3, INPUT);
pinMode(Relay3, OUTPUT);

pinMode(pinButton4, INPUT);
pinMode(Relay4, OUTPUT);

pinMode(pinButton5, INPUT);
pinMode(Relay5, OUTPUT);

pinMode(pinButton6, INPUT);
pinMode(Relay6, OUTPUT);

pinMode(pinButton7, INPUT);
pinMode(Relay7, OUTPUT);

pinMode(pinButton8, INPUT);
pinMode(Relay8, OUTPUT);
}

void loop() {
stateButton1 = digitalRead(pinButton1);
if(stateButton1 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay1 == HIGH){
stateRelay1 = LOW;
} else {
stateRelay1 = HIGH;
}
time = millis();
}
digitalWrite(Relay1, stateRelay1);
previous == stateButton1;
//2
stateButton2 = digitalRead(pinButton2);
if(stateButton2 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay2 == HIGH){
stateRelay2 = LOW;
} else {
stateRelay2 = HIGH;
}
time = millis();
}
digitalWrite(Relay2, stateRelay2);
previous == stateButton2;

//3
stateButton3 = digitalRead(pinButton3);
if(stateButton3 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay3 == HIGH){
stateRelay3 = LOW;
} else {
stateRelay3 = HIGH;
}
time = millis();
}
digitalWrite(Relay3, stateRelay3);
previous == stateButton3;

//4
stateButton4 = digitalRead(pinButton4);
if(stateButton4 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay4 == HIGH){
stateRelay4 = LOW;
} else {
stateRelay4 = HIGH;
}
time = millis();
}
digitalWrite(Relay4, stateRelay4);
previous == stateButton4;
//5
stateButton5 = digitalRead(pinButton5);
if(stateButton5 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay5 == HIGH){
stateRelay5 = LOW;
} else {
stateRelay5 = HIGH;
}
time = millis();
}
digitalWrite(Relay5, stateRelay5);
previous == stateButton5;
//6
stateButton6 = digitalRead(pinButton6);
if(stateButton6 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay6 == HIGH){
stateRelay6 = LOW;
} else {
stateRelay6 = HIGH;
}
time = millis();
}
digitalWrite(Relay6, stateRelay6);
previous == stateButton6;
//7
stateButton7 = digitalRead(pinButton7);
if(stateButton7 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay7 == HIGH){
stateRelay7 = LOW;
} else {
stateRelay7 = HIGH;
}
time = millis();
}
digitalWrite(Relay7, stateRelay7);
previous == stateButton7;
//8
stateButton8 = digitalRead(pinButton8);
if(stateButton8 == HIGH && previous == LOW && millis() – time > debounce) {
if(stateRelay8 == HIGH){
stateRelay8 = LOW;
} else {
stateRelay8 = HIGH;
}
time = millis();
}
digitalWrite(Relay8, stateRelay8);
previous == stateButton8;
}

Schemat połączeniowy ( dla 8 przekaźników)

 Arduino - sterowanie przekaźnikami

5/5 - (1 ocena/y)
Exit mobile version