0% found this document useful (0 votes)
27 views

Arduino Code Foe Home Automation

This code defines constants for relay on/off states and pin 4 as relay 1. It sets pin 4 as an output, turns the relay off on startup, and prints instructions to the serial monitor to turn the relay on or off by sending '1' or '0'. When input is received, it writes the relay pin high or low accordingly and prints the new relay state.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Arduino Code Foe Home Automation

This code defines constants for relay on/off states and pin 4 as relay 1. It sets pin 4 as an output, turns the relay off on startup, and prints instructions to the serial monitor to turn the relay on or off by sending '1' or '0'. When input is received, it writes the relay pin high or low accordingly and prints the new relay state.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#define RELAY_ON 0

#define RELAY_OFF 1
#define RELAY_1 4
char data = 0;
void setup() {
// Set pin as output.
pinMode(RELAY_1, OUTPUT);
// Initialize relay one as off so that on reset it would be off by default
digitalWrite(RELAY_1, RELAY_OFF);
Serial.begin(9600);
Serial.print("Type: 1 to turn on bulb. 0 to turn it off!");
}
void loop() {
if (Serial.available() > 0) {
data = Serial.read(); //Read the incoming data and store it into variable data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n"); //New line
if(data == '1'){
digitalWrite(RELAY_1, RELAY_ON);
Serial.println("Bulb is now turned ON.");
}
else if(data == '0'){
digitalWrite(RELAY_1, RELAY_OFF);
Serial.println("Bulb is now turned OFF.");
}
}
}

You might also like