Table of Contents
- Relay Module Basics: How Relays Work with Arduino
- Wiring a Relay Module Safely
- Simple Timer: On for X Minutes, Off for Y Minutes
- RTC-Based Scheduling: Daily On/Off Times
- Multi-Channel Relay Control for Multiple Appliances
- Safety Considerations for Mains Voltage
- Advanced Features: Manual Override and LCD Display
- Frequently Asked Questions
- Conclusion
Relay Module Basics: How Relays Work with Arduino
An arduino relay timer schedule system uses an electromagnetic relay to switch mains-powered appliances (lights, fans, pumps, motors) on and off based on time schedules programmed into the Arduino. The relay acts as an electrically controlled switch — a small 5V signal from the Arduino controls a switch that can handle 230V AC at up to 10A.
Standard relay modules for Arduino come in 1, 2, 4, 8, and 16 channel variants. Each channel has three output terminals: COM (Common), NO (Normally Open), and NC (Normally Closed). When the relay is off, COM is connected to NC. When activated, COM switches to NO. Most projects use the NO terminal so the appliance is off by default.
Relay modules are either active HIGH (5V on input activates the relay) or active LOW (GND on input activates). Most modules sold in India are active LOW — setting the input pin to LOW turns the relay ON. This is counterintuitive but important to remember when writing your code.
Wiring a Relay Module Safely
Low-voltage side (Arduino to relay module):
- VCC to Arduino 5V
- GND to Arduino GND
- IN1 to Arduino digital pin (e.g., pin 7)
- For 4+ channel modules, consider a separate 5V supply for the relay coils to avoid overloading the Arduino’s regulator
High-voltage side (relay to appliance) — DANGER: 230V AC:
- Cut the LIVE wire of the appliance (NOT neutral)
- Connect one end to COM
- Connect the other end to NO
- Never cut or switch the neutral wire
- Use proper wire connectors (not bare twisted joints)
Safety Warning: Working with 230V mains electricity is dangerous. Incorrect wiring can cause electrocution, fire, or equipment damage. If you are not confident with mains wiring, use the relay to switch low-voltage devices (12V LED strips, 5V devices) instead. Always unplug from mains before making wiring changes.
Simple Timer: On for X Minutes, Off for Y Minutes
// Simple cyclic timer: 30 min ON, 60 min OFF (e.g., for a pump)
const int RELAY_PIN = 7;
const unsigned long ON_TIME = 30UL * 60 * 1000; // 30 minutes in ms
const unsigned long OFF_TIME = 60UL * 60 * 1000; // 60 minutes in ms
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Relay OFF (active LOW module)
}
void loop() {
// Turn ON
digitalWrite(RELAY_PIN, LOW); // Active LOW = relay ON
delay(ON_TIME);
// Turn OFF
digitalWrite(RELAY_PIN, HIGH); // Active LOW = relay OFF
delay(OFF_TIME);
}
This pattern is useful for aquarium pumps, greenhouse ventilation fans, and periodic irrigation systems. The UL suffix on the constants ensures the multiplication does not overflow the unsigned long type.
RTC-Based Scheduling: Daily On/Off Times
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
const int RELAY_PIN = 7;
struct Schedule {
byte onHour, onMinute;
byte offHour, offMinute;
};
// Define schedules: lights on at 6:00, off at 22:00
// Pump on at 7:00, off at 7:30
Schedule schedules[] = {
{6, 0, 22, 0}, // Relay 1: Lights
{7, 0, 7, 30}, // Can add more for additional relays
};
void setup() {
Wire.begin();
rtc.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
}
void loop() {
DateTime now = rtc.now();
int currentMinutes = now.hour() * 60 + now.minute();
int onMinutes = schedules[0].onHour * 60 + schedules[0].onMinute;
int offMinutes = schedules[0].offHour * 60 + schedules[0].offMinute;
if (currentMinutes >= onMinutes && currentMinutes < offMinutes) {
digitalWrite(RELAY_PIN, LOW); // ON
} else {
digitalWrite(RELAY_PIN, HIGH); // OFF
}
delay(10000); // Check every 10 seconds
}
Multi-Channel Relay Control for Multiple Appliances
// 4-channel relay controller with independent schedules
const int relayPins[] = {4, 5, 6, 7};
const int NUM_RELAYS = 4;
Schedule allSchedules[4] = {
{6, 0, 22, 0}, // Relay 1: Garden lights 6AM-10PM
{7, 0, 7, 30}, // Relay 2: Morning pump 7:00-7:30
{18, 0, 18, 30}, // Relay 3: Evening pump 6:00-6:30PM
{9, 0, 17, 0}, // Relay 4: Workshop fan 9AM-5PM
};
void setup() {
Wire.begin();
rtc.begin();
for (int i = 0; i < NUM_RELAYS; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // All OFF
}
}
void loop() {
DateTime now = rtc.now();
int currentMinutes = now.hour() * 60 + now.minute();
for (int i = 0; i = onMin && currentMinutes < offMin) {
digitalWrite(relayPins[i], LOW);
} else {
digitalWrite(relayPins[i], HIGH);
}
}
delay(10000);
}
Safety Considerations for Mains Voltage
- Use optocoupler-isolated relay modules: These provide electrical isolation between the Arduino and mains voltage. Most modules sold in India include this isolation — check for the optocoupler chip on the board.
- Use appropriate wire gauge: For 10A loads, use 1.5 sq mm wire minimum. For 16A loads, use 2.5 sq mm wire.
- Add a fuse: Place a 10A fuse in the live wire before the relay for overcurrent protection.
- Enclose the high-voltage side: Mount relays and mains wiring inside a proper junction box or electrical enclosure. Never leave exposed mains wires.
- Use a proper MCB: If the relay controls a high-power appliance (water heater, AC), ensure the circuit has an appropriate MCB (miniature circuit breaker) in the distribution board.
Advanced Features: Manual Override and LCD Display
Add a push button for manual override — pressing it toggles the relay regardless of the schedule. Store the override state in a variable and check it before applying the schedule. A 16×2 LCD displays the current time, relay states, and next scheduled event.
Store schedules in EEPROM so they persist across power cycles. Add a 4×4 keypad for setting schedules without a computer. This creates a fully standalone timer that can be installed in a switchboard and programmed on-site.
Frequently Asked Questions
Can I control a 230V AC appliance directly with Arduino?
Never connect 230V AC directly to any Arduino pin. Always use a relay module as an intermediary. The relay provides electrical isolation between the low-voltage Arduino circuit and the high-voltage mains circuit.
How many relays can one Arduino control?
The Uno has 14 digital pins, so theoretically 14 relays. In practice, 8-12 is a comfortable limit, leaving pins free for I2C, buttons, and display. The Mega 2560 can control 40+ relays. For very large systems, use I2C I/O expanders (MCP23017) to add 16 pins per chip.
Why does my relay click repeatedly?
This usually indicates insufficient power. Each relay coil draws 70-80 mA. A 4-channel module with all relays active draws 320 mA from the 5V pin. The Arduino’s USB port provides only 500 mA total. Use an external 5V power supply for the relay module’s VCC to avoid brownouts.
Conclusion
An Arduino relay timer is one of the most practical and useful projects you can build. Whether you are automating garden irrigation, scheduling lighting, or controlling industrial equipment, the combination of relay modules, an RTC, and straightforward scheduling code gives you a professional-quality timer system at a fraction of the cost of commercial alternatives.
Add comment