How to Control AC Appliance with Arduino: Simple Guide
To control an
AC appliance with Arduino, use a relay module to safely switch the high voltage on and off. Connect the relay to Arduino's digital pin and write code to turn the relay on or off, which controls the appliance power.Syntax
Use a relay module connected to an Arduino digital pin to control an AC appliance. The relay acts like a switch that the Arduino can turn on or off.
- pinMode(pin, OUTPUT); sets the relay control pin as output.
- digitalWrite(pin, HIGH); turns the relay off (usually).
- digitalWrite(pin, LOW); turns the relay on (closes the switch).
Note: Relay logic can be active LOW or HIGH depending on the module.
arduino
const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); // Set relay pin as output digitalWrite(relayPin, HIGH); // Start with relay off } void loop() { // Turn relay on (close switch) digitalWrite(relayPin, LOW); delay(1000); // Keep on for 1 second // Turn relay off (open switch) digitalWrite(relayPin, HIGH); delay(1000); // Keep off for 1 second }
Example
This example turns an AC appliance on and off every second using a relay connected to pin 7. The relay safely switches the appliance power.
arduino
const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); digitalWrite(relayPin, HIGH); // Relay off initially } void loop() { digitalWrite(relayPin, LOW); // Relay ON - appliance ON delay(1000); // Wait 1 second digitalWrite(relayPin, HIGH); // Relay OFF - appliance OFF delay(1000); // Wait 1 second }
Output
The AC appliance connected to the relay turns ON for 1 second, then OFF for 1 second, repeatedly.
Common Pitfalls
- Not using a relay module: Directly connecting AC to Arduino pins is dangerous and can damage the board or cause electric shock.
- Wrong relay logic: Some relays turn ON with LOW signal, others with HIGH. Check your relay module specs.
- Missing external power: Relay coils may need more current than Arduino pins can supply; use external power if needed.
- No flyback diode: Use relay modules with built-in protection or add a diode to protect Arduino from voltage spikes.
- Improper wiring: Always connect the AC appliance through the relay's normally open (NO) contacts and never touch live wires.
arduino
/* Wrong way: Connecting AC directly to Arduino pin - DANGEROUS! */ // int acPin = 7; // pinMode(acPin, OUTPUT); // digitalWrite(acPin, HIGH); // DO NOT DO THIS /* Right way: Use relay module to switch AC safely */ const int relayPin = 7; void setup() { pinMode(relayPin, OUTPUT); digitalWrite(relayPin, HIGH); // Relay off initially } void loop() { // Your code here }
Quick Reference
Tips for controlling AC appliances with Arduino:
- Always use a relay module rated for your appliance voltage and current.
- Check if your relay is active LOW or HIGH and write code accordingly.
- Use external power supply for relay coil if needed.
- Never touch live AC wires; ensure safe insulation and wiring.
- Test with a low-power device before connecting high-power appliances.
Key Takeaways
Use a relay module to safely control AC appliances with Arduino.
Set the relay control pin as output and switch it LOW or HIGH based on relay logic.
Never connect AC power directly to Arduino pins to avoid damage and hazards.
Check your relay module specs for correct wiring and logic level.
Always ensure safe wiring and insulation when working with AC voltage.