Write a program to interface a lamp with 220 volt supply and relay with Arduino and power on/off as per user input.
Last Updated : 16 Jul 2025
Jan 23, Jul 24
Solution:
const int relaypin = 7;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available()>0){
char input = Serial.read()
if (input=='1'){
digitalWrite(relaypin,HIGH);
}
else{
digitalWrite(relaypin,LOW);
}
}
}
Output Description:
Input '1': Relay turns ON.
Input '0', 'A', etc.: Relay turns OFF.
Code Desciption:
-
Pin Setup:
relaypin = 7;
: Connects to the relay.
-
Initialization:
Serial.begin(9600);
: Starts serial communication at 9600 baud.
-
Main Loop:
if (Serial.available() > 0)
: Checks for serial input.char input = Serial.read();
: Reads the input character.if (input == '1')
: Turns the relay ON if input is'1'
.else
: Turns the relay OFF for any other input.
Meanwhile you can watch this video
Watch Video