Power Switch Tail: Control any AC Electrical Device Remotely via Spacebrew


This tutorial walks you through how to connect a PowerSwitch Tail to Spacebrew.The PowerSwtich Tail is a device for controlling ac power with a microcontroller, including Arduino. It is very convenient to use when you want to control a standard wall outlet device without exposing to any high voltage wiring. In this example we will connect the PowerSwitch Tail to an Arduino to control a standard AC-powered lamp and control it with a JavaScript button App via Spacebrew. But the exciting thing is that you can basically plug any electrical device (15amps @ 120vac) and control it remotely via Spacebrew.

Hardware Needed

  • Arduino (we use the Uno)
  • PowerSwitch Tail II
  • Three-wire cable or regular hookup wires. Any size wire #16 AWG or smaller may be used.
  • USB Cable
  • AC-powered lamp
  • Small Screwdriver (for the PowerSwitch Tail screw terminals)

Software Needed


On the PowerSwitch Tail side you will need to connect the wires to the terminal block.

Connect the wires from the PowerSwitch Tail to the pins on the Arduino board.

STEP 1: Connecting the Power Switch Tail to Arduino

On the PowerSwitch Tail side you will need to connect the wires to the terminal block.  Use a small screwdriver to access the screws from the top of the PowerSwitch Tail. Turn the screws CCW to open the terminal contacts. Strip about ¼-inch of insulation from the wires you can use a three-wire cable or three separate hookup wires (preferably red, black, and another color) and insert into the terminal block contacts through the holes on the side of the PowerSwitch Tail.

  1. Connect POWER (red wire) to terminal 1: +in
  2. Connect signal (green wire) to terminal 2: -in
  3. Connect GROUND (black wire) to terminal 3: Ground
  4. Tighten the screws and verify the contacts firmly grip the wires.
  5. Next you will connect the wires from the PowerSwitch Tail to the pins on the Ardunio board. Connect POWER (wire from terminal 1: +in)  to 5V
  6. Connect signal (wire from terminal 2: -in) to Analog 0 (A0)
  7. Connect GROUND (wire from terminal 3: Ground) to GND
  8. Plug the PowerSwitch Tail into the AC power source
  9. Plug the AC-power lamp or any electrical device (15amps @ 120vac) that you would like to control into the PowerSwitch Tail receptacle.

Step 2: Uploading the Arduino Code

Assuming you have wired everything up correctly the PowerSwitch Tail and Arduino, you are ready to load the software onto the Arduino.

If you have never used Arduino before, it may be worthwhile to read through some of their getting started resources before proceeding with this tutorial.

If you already have Arduino installed and have successfully run one or two examples before, then this is nothing new - just copy and paste the code below into your Arduino window and press "upload".

// control powerSocket
const int powerPin = A0;
const int ledPin = 13;
boolean powerOn = false;

// control LEDs

int incomingByte; //a variable to read incoming serial data

void setup() {
//initiate serial communication:
Serial.println(F("starting up"));

pinMode(ledPin, OUTPUT);
pinMode(powerPin, OUTPUT);

digitalWrite(ledPin, LOW);
digitalWrite(powerPin, HIGH);

void loop() {

// read serial messages
if (Serial.available() > 0) {

powerOn = !powerOn;

if (powerOn) {
digitalWrite(ledPin, HIGH);
digitalWrite(powerPin, LOW);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(powerPin, HIGH);

//read all the bytes in the serial buffer (we don't care about the contents of the data)
while (Serial.available()) incomingByte = Serial.read();

Step 3: Download and Install the Spacebrew Library

First, download the Spacebrew Processing Libary.

To install the Spacebrew Processing Library, refer to Step 3 of our Connect an Arduino Project to Spacebrew via Processing tutorial.


Step 4: Create a Processing Sketch

Once you've successfully installed the Spacebrew Processing Library, it's time to create a Processing sketch to connect your Arduino to Spacebrew. Open a new sketch and paste the following code into the window. Run the sketch.

import spacebrew.*;
import processing.serial.*;

String server="sandbox.spacebrew.cc";
String name="PowerSwitch_Lamp";
String description ="Lamp that blinks on and off";

Spacebrew spacebrewConnection;
Serial myPort; // The serial port

void setup() {
size(600, 400);

spacebrewConnection = new Spacebrew( this );

// add each thing you publish to
spacebrewConnection.addSubscribe( "Blink", "boolean" );
spacebrewConnection.addPublish( "Blinked", true );

// connect!
spacebrewConnection.connect("ws://"+server+":9000", name, description );

int i = 0;
for(String s : Serial.list()){
println(Integer.toString(i) + ": " + s);
//You may need to change the serial port that Processing
//connects to. Look at the output in the Processing console
//to determine which index your Arduino is at.
myPort = new Serial(this, Serial.list()[0], 9600);

void draw() {

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = new String(myPort.readBytesUntil('\n'));
spacebrewConnection.send( "Blinked", true);

void onBooleanMessage( String name, boolean value ){

If everything worked correctly, your Processing sketch should look like this and you should see your PowerSwitch Tail as a client on the public Spacebrew admin page.


If everything worked correctly, you should see your PowerSwitch Tail as a client on the public Spacebrew admin page.

If everything worked correctly, you should see your PowerSwitch Tail as a client on the public Spacebrew admin page
(Note: if you do not see clients, publishers or subscribers on your admin page try it with another browser - we have mostly tested this with Chrome and have on occasion had issues with Safari and Firefox).

By default, the name of the client node will be "PowerSwitch_Lamp" - this comes from the line 4 of the Processing code. Try changing the name of your client in your code and rerun the sketch. You should see the name change reflected in the admin. It’s best to choose a descriptive name so others have an idea of what your client does and if they’d like to subscribe or publish to it.

Since the Power Switch Tail can receive boolean-type information, it has a BOOLEAN type node that appears under the SUBSCRIBERS column. Because this node controls the light on/off, it is labeled "blink" - this comes from line 12 of our Processing code), but you can change it to fit the AC electric device you have plugged to the Power Switch Tail receptacle.

Step 5: Creating a Publisher

Now that we have the Switch Tail appearing as a SUBSCRIBER client in Spacebrew, create a PUBLISHER client that you can connect to and control the Power Switch Tail and the plugged lamp.

Launched Javascript button example.

Since the Power Switch Tail subscribes to data in the form of BOOLEAN, we need to create a PUBLISHER that can publish BOOLEAN type. The easiest one to launch is our Javascript Button Example. This app is already set to connect to the the Public Spacebrew cloud server

Remember to give your client a unique name so you can easily identify your button on the Admin page.

Remember to give your client a unique name so you can easily identify your button on the Admin page. You can add your unique name after the "name=" in the query string - press enter to re-load the page with your unique name. (Here we have given it the unique name "PowerSwitch-button"). If you go back to the Spacebrew admin page the button should now appear and it should look the second image at left.


Step 6: Make the Clients Talk

Now that you have both the Power Switch Tail  and javascript button connected to Spacebrew, let’s make them talk. Click the BOOLEAN publisher node of the PowerSwitch-button (clicking either the blue rectangle or circular node works), and then click the PowerSwitch_Lamp BOOLEAN subscriber. You should now see a line connecting the two. It should look like the image at left.

Click the BOOLEAN publisher node of the PwerSwitch-button, and then click the PowerSwitch_Lamp BOOLEAN subscriber to connect the two.

That's it!

Try clicking the button to see the lamp (or the electrical device you have plugged to the Power Switch Tail) turn ON/OFF.

Clicking the javascript button to turn the lamp on and off.