With the increasing demand for home automation systems, people are constantly looking for affordable and customizable solutions. One such solution is the Arduino IR remote control system, which allows users to control various appliances with just a click of a button. In this article, we will provide a step-by-step guide on how to set up an Arduino IR remote home automation system.
This code uses the IRremote library to read the IR signal received by the IR receiver module and turn the LED (or any other appliance) on or off based on the signal received.
Once you've finished writing the code, upload it to the Arduino board by clicking the "Upload" button in the Arduino IDE software. Wait for the upload to finish, and then disconnect the USB cable.
#include <IRremote.h>
#define relay1 4
#define relay2 5
#define relay3 6
#define relay4 7
#define switch1 8
#define switch2 9
#define switch3 10
#define switch4 11
#define IR_RECV_PIN 3
#define ir_pin1 0x1FE50AF
#define ir_pin2 0x1FED827
#define ir_pin3 0x1FEF807
#define ir_pin4 0x1FE30CF
IRrecv irrecv(IR_RECV_PIN);
decode_results results;
// Switch State
bool SwitchState_1 = LOW;
bool SwitchState_2 = LOW;
bool SwitchState_3 = LOW;
bool SwitchState_4 = LOW;
void relayOnOff(int relay){
switch(relay){
case 1:
digitalWrite(relay1, !digitalRead(relay1)); // change state for relay-1
delay(100);
break;
case 2:
digitalWrite(relay2, !digitalRead(relay2)); // change state for relay-2
delay(100);
break;
case 3:
digitalWrite(relay3, !digitalRead(relay3)); // change state for relay-3
delay(100);
break;
case 4:
digitalWrite(relay4, !digitalRead(relay4)); // change state for relay-4
delay(100);
break;
default : break;
}
}
void ir_remote(){
if (irrecv.decode(&results)) {
switch(results.value){
case ir_pin1: relayOnOff(1); break;
case ir_pin2: relayOnOff(2); break;
case ir_pin3: relayOnOff(3); break;
case ir_pin4: relayOnOff(4); break;
default : break;
}
Serial.println(results.value, HEX);
irrecv.resume();
}
}
void manual_control()
{
if (digitalRead(switch1) == LOW && SwitchState_1 == LOW) {
digitalWrite(relay1, LOW);
SwitchState_1 = HIGH;
}
if (digitalRead(switch1) == HIGH && SwitchState_1 == HIGH) {
digitalWrite(relay1, HIGH);
SwitchState_1 = LOW;
}
if (digitalRead(switch2) == LOW && SwitchState_2 == LOW) {
digitalWrite(relay2, LOW);
SwitchState_2 = HIGH;
Serial.println("Switch-2 on");
}
if (digitalRead(switch2) == HIGH && SwitchState_2 == HIGH) {
digitalWrite(relay2, HIGH);
SwitchState_2 = LOW;
Serial.println("Switch-2 off");
}
if (digitalRead(switch3) == LOW && SwitchState_3 == LOW) {
digitalWrite(relay3, LOW);
SwitchState_3 = HIGH;
Serial.println("Switch-3 on");
}
if (digitalRead(switch3) == HIGH && SwitchState_3 == HIGH) {
digitalWrite(relay3, HIGH);
SwitchState_3 = LOW;
Serial.println("Switch-3 off");
}
if (digitalRead(switch4) == LOW && SwitchState_4 == LOW) {
digitalWrite(relay4, LOW);
SwitchState_4 = HIGH;
Serial.println("Switch-4 on");
}
if (digitalRead(switch4) == HIGH && SwitchState_4 == HIGH) {
digitalWrite(relay4, HIGH);
SwitchState_4 = LOW;
Serial.println("Switch-4 off");
}
}
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
pinMode(switch3, INPUT_PULLUP);
pinMode(switch4, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
delay(500);
}
void loop() {
ir_remote();
manual_control();
}
0 Comments