How to Use Relay Module with Arduino: Simple Guide and Example
To use a
relay module with Arduino, connect the relay's input pin to an Arduino digital pin, and control it by setting that pin HIGH or LOW in your code. The relay acts like a switch to turn devices on or off safely using Arduino signals.Syntax
To control a relay module with Arduino, you use a digital pin to send a signal. The basic syntax involves setting the pin mode and writing HIGH or LOW to it.
- pinMode(pin, OUTPUT); - sets the Arduino pin connected to the relay as an output.
- digitalWrite(pin, HIGH); - turns the relay off (usually, depending on relay type).
- digitalWrite(pin, LOW); - turns the relay on (usually, depending on relay type).
arduino
const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); // Set relay pin as output } void loop() { digitalWrite(relayPin, LOW); // Turn relay ON delay(1000); // Wait 1 second digitalWrite(relayPin, HIGH); // Turn relay OFF delay(1000); // Wait 1 second }
Example
This example shows how to connect a relay module to Arduino pin 7 and toggle it on and off every second. It demonstrates controlling a relay to switch a device safely.
arduino
const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); // Initialize relay pin as output digitalWrite(relayPin, HIGH); // Start with relay OFF } void loop() { digitalWrite(relayPin, LOW); // Turn relay ON delay(1000); // Wait for 1 second digitalWrite(relayPin, HIGH); // Turn relay OFF delay(1000); // Wait for 1 second }
Output
The relay clicks ON and OFF every second, switching the connected device accordingly.
Common Pitfalls
Common mistakes when using relay modules with Arduino include:
- Not powering the relay module properly, causing it not to switch.
- Confusing HIGH and LOW signals; many relay modules are active LOW, so LOW turns them ON.
- Not using a separate power supply for high current relays, risking damage to Arduino.
- Failing to connect the relay module ground to Arduino ground, causing erratic behavior.
Always check your relay module's datasheet for correct wiring and signal logic.
arduino
/* Wrong way: Relay turns ON with HIGH signal (may not work if relay is active LOW) */ const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); } void loop() { digitalWrite(relayPin, HIGH); // This might turn relay OFF if active LOW delay(1000); digitalWrite(relayPin, LOW); // This might turn relay ON if active LOW delay(1000); } /* Right way: Use LOW to turn relay ON if module is active LOW */ void loop() { digitalWrite(relayPin, LOW); // Turn relay ON delay(1000); digitalWrite(relayPin, HIGH); // Turn relay OFF delay(1000); }
Quick Reference
| Action | Arduino Code | Description |
|---|---|---|
| Set relay pin as output | pinMode(relayPin, OUTPUT); | Prepare Arduino pin to control relay |
| Turn relay ON (active LOW) | digitalWrite(relayPin, LOW); | Activate relay switch |
| Turn relay OFF (active LOW) | digitalWrite(relayPin, HIGH); | Deactivate relay switch |
| Delay for timing | delay(1000); | Wait 1 second before next action |
Key Takeaways
Connect relay input pin to Arduino digital pin and ground common grounds.
Most relay modules are active LOW: LOW signal turns relay ON, HIGH turns it OFF.
Use pinMode to set relay pin as OUTPUT before controlling it.
Power relay modules with appropriate voltage and current separate from Arduino if needed.
Always test relay switching with safe loads before connecting high voltage devices.