Saturday, April 28, 2012

Arduino Controlled Extension Cord

This is almost certainly the most dangerous project I have tried with electricity. I recently bought a board of relays. These are essentially just switches which can be electronically controlled and will turn on and off when given a signal from a device such as my Arduino.

I then took an extension cord and cut the hot wire of the cord and put the two ends into the COM and NC(Normally Closed) inputs on the relay. It is important to cut the hot wire because if you instead cut the neutral you might have full wall voltage at your device even when the relay is turned off.

Then with a simple program I turned on and off the relay while using the extension cord to power a lamp.





From what I can tell creating a light or other device that turns on at a particular time, temperature, reading from a motion sensor, microphone, or light level would be fairly trivial. The hardest part would simply be hardening the relay so that it will not be a fire or electrocution hazard.

The code running this project is:

// constants won't change. Used here to
// set pin numbers:
const int RelayPin =  2;      // the number of the relay pin


// Variables will change:
int RelaySetting = HIGH;             // ledState used to set the relay to off


long previousMillis = 0;        // will store last time relay was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long wavelength = 2000;           // interval at which to blink (milliseconds)
;

void setup() {
 
  digitalWrite(RelayPin, HIGH);  // set the digital pin as output:
  pinMode(RelayPin, OUTPUT);


}

void loop()
{
unsigned long currentMillis = millis();

if(currentMillis - previousMillis < wavelength/2 && currentMillis - previousMillis > 0) {
  RelaySetting=LOW;    // turns the relay on
  digitalWrite(RelayPin, RelaySetting);
}
else{
  RelaySetting=HIGH;      // turns the relay off
  digitalWrite(RelayPin, RelaySetting);

}
 

if(currentMillis - previousMillis > wavelength){
  previousMillis=currentMillis;
}

}

No comments: