Introduction
This experiment demonstrates how to control a relay module with an Arduino. The relay module can act as a switch to control high-power devices such as lights or fans.
Components Needed
- Arduino (e.g., Uno, Nano)
- Relay module (e.g., SRD-05VDC-SL-C)
- 1kΩ resistor
- LED (optional, for testing)
- External device (e.g., light bulb) with appropriate power supply
- Breadboard and jumper wires
Circuit Setup
- Connect the GND pin of the relay module to the GND of the Arduino.
- Connect the VCC pin of the relay module to the 5V pin of the Arduino.
- Connect the IN pin of the relay module to digital pin 7 on the Arduino through a 1kΩ resistor.
- Connect the common (COM) terminal of the relay to one terminal of the external device (e.g., light bulb or fan).
- Connect the Normally Open (NO) terminal of the relay to the positive side of the external power supply (e.g., 220V AC for a light bulb).
- Connect the other terminal of the external device to the negative side of the power supply.
Warning: Be cautious when working with high-voltage devices. Ensure power is disconnected during setup.
Code for Relay Control
Upload the following code to your Arduino to control the relay:
// Define relay control pin
const int relayPin = 7;
void setup() {
// Set the relay control pin as output
pinMode(relayPin, OUTPUT);
}
void loop() {
// Turn the relay on (activates the external device)
digitalWrite(relayPin, HIGH);
delay(3000); // Relay on for 3 seconds
// Turn the relay off (deactivates the external device)
digitalWrite(relayPin, LOW);
delay(3000); // Relay off for 3 seconds
}
Explanation
This code controls the relay as follows:
- When
relayPin
is set to HIGH, the relay is activated, closing the circuit and allowing current to flow to the connected device. - When
relayPin
is set to LOW, the relay is deactivated, opening the circuit and stopping current flow to the connected device. - The
delay
commands create a cycle where the relay alternates between on and off every 3 seconds.
Troubleshooting
- If the relay doesn't activate, check all connections and ensure the relay module is powered correctly.
- If the external device does not turn on, verify that the relay's COM and NO terminals are connected properly.
- If the relay clicks but the device does not operate, ensure that the device is connected to an appropriate external power source.
- Always ensure safety when dealing with high-voltage circuits. Disconnect power when making adjustments to the circuit.