Ex 7
Ex 7
Monitor (Wokwi)
Objective:
To simulate the control of a relay module using the Arduino UNO and Serial Monitor in
Wokwi. The goal is to learn how to send commands via serial communication to control a
relay that can switch an external load (e.g., an LED or motor). This experiment demonstrates
the use of digital output pins, serial communication, relay switching, and real-time user
interaction.
Introduction:
Relays are electrically operated switches commonly used to control high voltage devices (like
fans, bulbs, or motors) with a low-power controller like an Arduino. This ensures safety and
provides electrical isolation. In this simulation, we control a relay via the Arduino Uno, and
instead of connecting a high voltage device, we use an LED as an indicator to represent the
ON/OFF state of a load. The user sends the command through the Serial Monitor – typing 1
turns the relay ON and 0 turns it OFF.
We also use the Arduino’s built-in LED on pin 13 to give visual feedback that the relay has
been activated. This makes it easier to verify functionality without checking the relay
directly.
Relay Module:
o VCC → Arduino 5V
void setup() {
pinMode(RELAY_PIN, OUTPUT); // Set relay pin as output
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Start serial communication
digitalWrite(RELAY_PIN, LOW); // Ensure relay is OFF at startup
digitalWrite(LED_PIN, LOW); // Ensure LED is OFF at startup
Serial.println("Relay Control Ready. Enter '1' to turn ON, '0' to turn
OFF.");
}
void loop() {
if (Serial.available()) { // Check if data is available
char command = Serial.read(); // Read command from Serial
Monitor
if (command == '1') {
digitalWrite(RELAY_PIN, HIGH); // Turn ON relay
digitalWrite(LED_PIN, HIGH); // Turn ON LED (feedback)
Serial.println("Relay ON");
}
else if (command == '0') {
digitalWrite(RELAY_PIN, LOW); // Turn OFF relay
digitalWrite(LED_PIN, LOW); // Turn OFF LED (feedback)
Serial.println("Relay OFF");
}
else {
Serial.println("Invalid command! Enter '1' to turn ON, '0' to turn
OFF.");
}
}
}
Working Principle:
1. Upload the above code into the Wokwi Arduino Uno simulation.
2. Start the simulation.
Flow of Logic:
Setup()
Observation Table:
Applications:
Provides isolation between low voltage controller and high voltage appliances
Offers a safe way to automate real-world electrical devices
Easy to program and control using simple digitalWrite() function
Conclusion:
Through this experiment, we successfully simulated the control of a relay using Arduino and
Serial Monitor. We learned how to read serial input, control digital outputs, and provide
feedback using an onboard LED. This forms a basic yet important foundation for building
advanced systems like smart homes and industrial control setups. The use of proper input
validation also makes the project more reliable and user-friendly.