Sunday, December 20, 2020

Arduino Controlled 5V Relay for Christmas Tree Lights

Our christmas tree lights plug into the wall behind the tree and are always hard to reach without sprinkling needles on the floor when we want to turn the lights on or off. This year I made an Arduino controlled relay that can turn the lights on and off with a switch that extends out next to the tree. Here is a video of the switch in action:

Circuit

Here is the circuit I put together. I thought of using a short extension cord so I might be able to use the relay for other devices as well, such as a lamp or something that has a hard-to-reach switch.


Some points to make about using a relay, especially with 120V outlet voltage. INSERT TYPICAL DISCLAIMER HERE => You can die or be seriously injured by mishandling high voltage electricity and you should not undertake this project without a solid understanding of the dangers and taking proper safety precautions. You have to make sure you understand your circuit and are confident in your connections before connecting it to high voltage. I tested this circuit with low DC voltage before plugging it into the wall outlet to make sure everything was connected properly. I made sure I understood which wire on the extension cord is hot and which is neutral, and connected the hot wire to the relay terminals.

Code 

The code is super simple!

int relay = 2;
int sw = 3;

void setup() {
  pinMode(relay, OUTPUT);
  pinMode(sw, INPUT_PULLUP);
}

void loop() {
  bool lit = digitalRead(sw);
  if (lit) {
    digitalWrite(relay, LOW);
  }  else {
    digitalWrite(relay, HIGH);
  }
}

As a final touch, I put the switch in a Christmas lights box:



No comments :