Controlling mains with Arduino

G'day gang,

I've got a feeling (and doing some searches kind of makes me agree) that this questions gets asked about once a week. How would I go about making something to turn on and off things that I can plug into the mains with the arduino? The reason I'm starting a new thread is because if there exists something a pre-made (but not as expensive and as fancy as X10) that could do it, it'd be great. I'm a pretty much electronics newb, so I don't want to electrocute myself or anything of that nature. The idea is that I would just like to be able to turn the heater in my room on and off (with say my (hopefully soon to have) iPhone) from around campus. Even something like a little motor doo dad that would physically switch the powerpoint on would be good enough for my purposes (then I wouldn't have to worry about the kind of electricity this heater would chew up)

Thanks,

Scotty

You can get one of those wireless / remote switching thingies. Then from the Arduino board you just control the remote control for the wireless switch. This way you do not have to deal with the mains power.

I am in the process of writing up an article for the playground on how to do this . In the mean time, perhaps this outline will be of some help.

A safe and inexpensive way to switch 110 to 240 volts AC from the Arduino is to use a low cost remote controlled power switch such as this one:

http://www.dealextreme.com/details.dx/sku.6026 or http://www.dealextreme.com/details.dx/sku.3072

The Arduino interfaces to a low voltage RF remote control, there is no physical connection between the arduino and the high voltage AC that is being controlled.

The interface just requires two optocouplers and a couple of resistors. I built mine using an old TLP521 that I pulled from an ancient circuit board in my junk box. Most popular optcouplers should work, such as the 4N28 or SFH618.

An optcoupler consists of an emitting diode (an LED) and a phototransistor Two are required, one for the 'on' button and one for 'off'

The anodes of the emitting diodes should be connected through resistors to Arduino pins. The resistor value depends on the optcoupler you use (1k as about right for a 4n28). The emitting diode cathodes are connected to the Arduino ground.

The phototransistor collector is connected to 9 to 12 volts DC. I removed the little 12volt battery that came with the remote and use the 9 volt DC supply that powers my RS232 Freeduino board. The phototransistor emitter is connected to the 'on' and 'off' switches on the remote control PCB.

Here is a sketch that switches on the AC voltage when the arduino receives the serial command “~1” and switches it off on the command “~0”

// SwitchAC example sketch
// this code pulses pins to trigger a remote control AC switch connected through an optocoupler

#define SWITCH_ON_PIN   2  // define pin number to turn remote switch on
#define SWITCH_OFF_PIN  3  // pin to turn remote switch off

void setup() {
  Serial.begin(19200);
  pinMode(SWITCH_ON_PIN, OUTPUT);   // declare Switch On and Off pins as outputs
  pinMode(SWITCH_OFF_PIN, OUTPUT);
}

void switchWrite( boolean state){
  byte pin; 
  if(state == HIGH)
    pin = SWITCH_ON_PIN;          
  else   
    pin = SWITCH_OFF_PIN;

  digitalWrite(pin,HIGH);
  delay(500);  // pulse the remote switch for half a second
  digitalWrite(pin,LOW);  // write to the switch pin
}

void loop() {
  while(Serial.available()>1){
    if(Serial.read() == '~') {
      char c = Serial.read(); 
      if( c == '0')
        switchWrite(LOW);
      else if (c == '1'){
        switchWrite(HIGH);               
      }       
    } 
  }
}

Pictures of the remote control AC switch interface

Prototype board connected to Arduino

Closeup of wiring to remote control PCB

That’s very cool, looking at the gizmo that I’m supposed to hook the heater up to though, I don’t see how this can plug into the power point. I don’t really want to have to re-wire the powerpoint live (as I don’t have access to switch the mains off) but this is exactly what I’m looking for thanks guys :slight_smile:

I don't see how this can plug into the power point. I don't really want to have to re-wire the powerpoint live (as I don't have access to switch the mains off)

I wired the relay module to a mains plug and mains socket:

That's very cool, looking at the gizmo that I'm supposed to hook the heater up to though, I don't see how this can plug into the power point. I don't really want to have to re-wire the powerpoint live (as I don't have access to switch the mains off) but this is exactly what I'm looking for thanks guys :)

You want a remote control mains socket.

--Phil.

If you don't want to wire up a plug and socket, you could try the interface technique mentioned earlier on something like this: http://www.maplin.co.uk/module.aspx?ModuleNo=98630&doy=7m6#overview

I don't see how this can plug into the power point. I don't really want to have to re-wire the powerpoint live (as I don't have access to switch the mains off)

I wired the relay module to a mains plug and mains socket:

Very cool, so I could follow your diagrams and hook that little component from DX to, say a small extension cord?

Here is a diagram of the Relay wiring. You can use a standard plug and the socket can be an extension cord, but don't exceed the maximum rating of the relay

This remote control switch from Maplins http://www.maplin.co.uk/Module.aspx?ModuleNo=98630&DOY=7m6 uses the same technology as the Dealextreme posted above but has the 13Amp plug and socket built into the relay module, so no wiring required. The downside is that the transmitter uses an SMT chip instead of DIL so its going to be more difficult to solder the interface wires. I will post pictures of the Maplin PCB when I can find some time to solder it up.

Here is a diagram of the Relay wiring. You can use a standard plug and the socket can be an extension cord, but don't exceed the maximum rating of the relay

Pretty much off topic, buy how'd you draw the diagram? What program? And what do people draw the nice colorful circuit diagrams? Not the ones from EagleCAD, but, I think you get what i mean.

how'd you draw the diagram? What program? And what do people draw the nice colorful circuit diagrams?

I use Visio

Hmm, powering a heater, not such a good idea, that first device posted apparently can do 7 amps at 240 Volts. That'd be 1,680 Watts, right? Well, my little heater is at 2,400 Watts. Can't they make a device that just physically switches on the circuit (like, sent a current to turn the switch on, another one to turn the switch off) so it'd be like turning on and off a main powerpoint without having to worry about how much load the relay can handle?

This device is a relay :slight_smile: When talking about electricity, you always need to check how much current the power lines and circuit can handle. People forgetting to do this and making heavy plugging cause fires every year.

Enough for the “moralist” part. It’s very easy on any electronics part supplier’s catalogue to find 1. The power a relay can handle, and 2. the power it needs to switch the circuit on. If you don’t know how to search, just go to a physical shop and ask a vendor, knowing your heater’s power consumption and just double-check what s/he will suggest you to buy. On some models you may need to use a transistor to power up the relay, but don’t worry, this is really easy to do.

This device is a relay :) When talking about electricity, you always need to check how much current the power lines and circuit can handle. People forgetting to do this and making heavy plugging cause fires every year.

Enough for the "moralist" part. It's very easy on any electronics part supplier's catalogue to find 1. The power a relay can handle, and 2. the power it needs to switch the circuit on. If you don't know how to search, just go to a physical shop and ask a vendor, knowing your heater's power consumption and just double-check what s/he will suggest you to buy. On some models you may need to use a transistor to power up the relay, but don't worry, this is really easy to do.

Has a powerpoint got a max-Watt rating it can handle? Why can't they just make relays that can do whatever the powerpoint can do? (just something to replace the physical on/off switch on the powerpoint)

Has a powerpoint got a max-Watt rating it can handle? Why can't they just make relays that can do whatever the powerpoint can do? (just something to replace the physical on/off switch on the powerpoint)

MMmh, I'm not sure what a "powerpoint" is, and google translator doesn't help me with this. (It translates powerpoint to PowerPoint, mmmh, helpfuuuuul !) If you're referring to the wall power plug or power switch, yes, it does. But it is usually quite high, since it's better that houses don't burn every morning, when people turn on light, an heater, a water boiler, a microwave oven, and some other stuff.

Such relays exists, they're big and expensive. Just stick to the power you need. But too much is better than not enough (On the RELAY side. It's better to have a 10AMP-capable relay to handle 7amps than a 5amps-capable one. NOT the reverse ^^ Or it will smell like toasted plastic, sooner or later. Or burned flesh.)

Seriously considerer you're dealing with relatively high powers, so make it safe. Especially make sure that your soldering is well done, and that the cable you'll be using is wide enough to handle that much power. Electric shocks is a minor danger, because it can easily be avoided, compared to fire.

Sorry to insist on safety issues, but I think you need to know what you're doing before making a device which operates at such levels of power.

yeah, by powerpoint I was referring to the "Wall outlet". I still don't see why those sort of relays are so big an expensive, I might end up just trying to build a little gizmo that can turn on the switch of the wall outlet instead of playing with electricity directly then :)

yeah, by powerpoint I was referring to the "Wall outlet". I still don't see why those sort of relays are so big an expensive, I might end up just trying to build a little gizmo that can turn on the switch of the wall outlet instead of playing with electricity directly then :)

Well in that case, you'd need a servo.

A suitable servo costs ~$12, plus the wall switch, 3,

So again, it;d be easier to do this :/