Home automation using Arduino

Circuit Diagram:

Arduino code:

#define fan 2
#define light 3

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(fan, OUTPUT);
  pinMode(light, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() == 1)
  {
    String val = Serial.readString();
    Serial.println(val);
    if(val == "fan on")
    {
      digitalWrite(fan, HIGH);
    }
    if(val == "fan off")
    {
      digitalWrite(fan, LOW);
    }
    if(val == "lights on")
    {
      digitalWrite(lights, HIGH);
    }
    if(val == "lights off")
    {
      digitalWrite(light, LOW);
    }
    if(val == "all on")
    {
      digitalWrite(fan, HIGH);
      digitalWrite(light, HIGH);
    }
    if(val == "all off")
    {
      digitalWrite(light, LOW);
      digitalWrite(fan, LOW);
    }
  }
}

Post a Comment

4 Comments

  1. Sir I have a doubt-Can we upload Arduino Uno code in Arduino Nano

    ReplyDelete
    Replies
    1. Cir can you reply as fast as you can I have a project to do,And time is less

      Delete
  2. Bro when I compile code is showing error 😟what can I do

    ReplyDelete