Power Plug Phidget
The Power Plug Phidget allows you to control anything that plugs into a standard power outlet.
You can turn the power on and off with the Power Plug Phidget. Combining this device and other Phidgets is useful if you want to turn a light on when it gets too dark, or maybe a fan on when it gets too hot!

Code (Java)
Create a file called PowerPlug and add the following code. Add a fan or light to your switch so you can see it running.
Not your programming language? Set my language and IDE.
package powerplug;
//Add Phidgets Library
import com.phidget22.*;
public class PowerPlug {
public static void main(String[] args) throws Exception{
//Create
DigitalOutput powerPlug = new DigitalOutput();
//Address
powerPlug.setHubPort(0);
powerPlug.setIsHubPortDevice(true);
//Open
powerPlug.open(1000);
//Use your Phidgets
powerPlug.setState(true);
Thread.sleep(3000);
powerPlug.setState(false);
}
}
//Add Phidgets Library
import com.phidget22.*;
public class PowerPlug {
public static void main(String[] args) throws Exception{
//Create
DigitalOutput powerPlug = new DigitalOutput();
//Address
powerPlug.setHubPort(0);
powerPlug.setIsHubPortDevice(true);
//Open
powerPlug.open(1000);
//Use your Phidgets
powerPlug.setState(true);
Thread.sleep(3000);
powerPlug.setState(false);
}
}
//Add Phidgets Library
import com.phidget22.*;
//Define
DigitalOutput powerPlug;
void setup(){
try{
//Create
powerPlug = new DigitalOutput();
//Address
powerPlug.setHubPort(0);
powerPlug.setIsHubPortDevice(true);
//Open
powerPlug.open(1000);
}catch(Exception e){
e.printStackTrace();
}
}
void draw(){
try{
//Use your Phidgets
powerPlug.setState(true);
delay(3000);
powerPlug.setState(false);
delay(3000);
}catch(Exception e){
e.printStackTrace();
}
}
Code (Python)
Create a file called PowerPlug and add the following code. Add a fan or light to your switch so you can see it running.
Not your programming language? Set my language and IDE.
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalOutput import *
#Required for sleep statement
import time
#Create
powerPlug = DigitalOutput()
#Address
powerPlug.setHubPort(0)
powerPlug.setIsHubPortDevice(True)
#Open
powerPlug.openWaitForAttachment(1000)
#Use your Phidgets
powerPlug.setState(True)
time.sleep(3)
powerPlug.setState(False)
Code (C#)
Create a file called PowerPlug and add the following code. Add a fan or light to your switch so you can see it running.
Not your programming language? Set my language and IDE.
//Add Phidgets Library
using Phidget22;
namespace PowerPlug{
class Program{
static void Main(string[] args){
//Create
DigitalOutput powerPlug = new DigitalOutput();
//Address
powerPlug.HubPort = 0;
powerPlug.IsHubPortDevice = true;
//Open
powerPlug.Open(1000);
//Use your Phidgets
powerPlug.State= true;
System.Threading.Thread.Sleep(3000);
powerPlug.State = false;
}
}
}
Code (Swift)
Create a file called PowerPlug and add the following code. Add a fan or light to your switch so you can see it running.
Not your programming language? Set my language and IDE.
Add two buttons.

import Cocoa
//Add Phidgets Library
import Phidget22Swift
class ViewController: NSViewController {
//Create
let powerPlug = DigitalOutput()
override func viewDidLoad() {
super.viewDidLoad()
do{
//Address
try powerPlug.setHubPort(0)
try powerPlug.setIsHubPortDevice(true)
//Open
try powerPlug.open()
}catch{
print(error)
}
}
@IBAction func turnPowerOn(_ sender: Any) {
do{
try powerPlug.setState(true)
}catch{
print(error)
}
}
@IBAction func turnPowerOff(_ sender: Any) {
do{
try powerPlug.setState(false)
}catch{
print(error)
}
}
}
Applications
Outlet Switches are useful in several applications. If you have ever gone on vacation and put your house lights on timers, you have used an outlet switch. The Clapper (a product popular in TV infomercials) uses the sound of your hands clapping to control a switch to turn off the lights in a room. Switches are useful to control everyday items.



Practice
Using your Getting Started Kit and Power Plug Phidget, you can create a simple program that turns power on or off based on user input.
- Connect your Power Plug to your Getting Started Kit.
- Write a program that turns power on when the green button is pressed and off when the red button is pressed.
- Try connecting a fan to the Power Plug Phidget and use the temperature reading from the Humidity Phidget to turn the fan on when the temperature gets too hot.
Check out the advanced lesson Using the Sensor API before you use the API for the first time.
API
This device doesn't have an API of its own. It is controlled by opening a DigitalOutput on the a VINT Hub (just like you do with LEDs).