Light Phidget

The Light Phidget measures brightness. The sensors in the Light Phidget closely match the human eye’s sensitivity. The Light Phidget returns brightness (called illuminance) in lux (symbol: lx). Lux is a measure of how much light there is per square meter. A typical classroom will be around 250 lx.

See tutorial

Setup

Before you do any coding you will have to attach your Light Phidget to your VINT Hub as shown:

Code (Java)

Create a file called Light and add the following code. Run your code to see the illuminance of your classroom. Try covering the sensor or shining a light on the sensor to see the changes in output.

Not your programming language? Set my language and IDE.

  
package light;

//Add Phidgets Library
import com.phidget22.*;

public class Light{
    public static void main(String[] args) throws Exception{
        
        //Create
        LightSensor lightSensor = new LightSensor();
        
        //Open
        lightSensor.open(1000);
        
        //Use your Phidgets
        while(true){
            System.out.println("Illuminance: " + lightSensor.getIlluminance() + " lx");
            Thread.sleep(250);
        } 
    }
}
  
  
//Add Phidgets Library
import com.phidget22.*;

public class Light{
    public static void main(String[] args) throws Exception{
        
        //Create
        LightSensor lightSensor = new LightSensor();
        
        //Open
        lightSensor.open(1000);
        
        //Use your Phidgets
        while(true){
            System.out.println("Illuminance: " + lightSensor.getIlluminance() + " lx");
            Thread.sleep(250);
        } 
    }
}
  
  
//Add Phidgets Library
import com.phidget22.*;

//Define
LightSensor lightSensor;

void setup(){
  try{
    
    //Create 
    lightSensor = new LightSensor();
    
    //Open
    lightSensor.open(1000);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}

void draw(){
  try{
    
    //Use your Phidgets
    println("Illuminance: " + lightSensor.getIlluminance() + " lx");
    delay(250);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}
  

Code (Python)

Create a file called Light and add the following code. Run your code to see the illuminance of your classroom. Try covering the sensor or shining a light on the sensor to see the changes in output.

Not your programming language? Set my language and IDE.

  
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.LightSensor import *
#Required for sleep statement
import time

#Create
lightSensor = LightSensor()

#Open
lightSensor.openWaitForAttachment(1000)

#Use your Phidgets
while (True):
    print("Illuminance: " + str(lightSensor.getIlluminance()) + " lx")
    time.sleep(0.25)
  

Code (C#)

Create a file called Light and add the following code. Run your code to see the illuminance of your classroom. Try covering the sensor or shining a light on the sensor to see the changes in output.

Not your programming language? Set my language and IDE.

  
//Add Phidgets Library
using Phidget22;

namespace Light{
    class Program{
        static void Main(string[] args){

            //Create
            LightSensor lightSensor = new LightSensor();

            //Open
            lightSensor.Open(1000);

            //Use your Phidgets
            while (true){
                System.Console.WriteLine("Illuminance: " + lightSensor.Illuminance + " lx");
                System.Threading.Thread.Sleep(250);
            }
        }
    }
}
  

Code (Swift)

Create a file called Light and add the following code. Run your code to see the illuminance of your classroom. Try covering the sensor or shining a light on the sensor to see the changes in output.

Not your programming language? Set my language and IDE.

You will need to add a Label.

  
import Cocoa
//Add Phidgets Library
import Phidget22Swift

class ViewController: NSViewController {

    @IBOutlet weak var sensorLabel: NSTextField!
    
    //Create
    let lightSensor = LightSensor()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Subscribe to event
            let _ = lightSensor.illuminanceChange.addHandler(onIlluminanceChange)
            
            //Open
            try lightSensor.open()
        }catch{
            print(error)
        }
    }
    
    func onIlluminanceChange(sender:LightSensor, illuminance:Double){
        DispatchQueue.main.async {
	    //Use information from your Phidget to change label
            self.sensorLabel.stringValue = String(illuminance) + " lux"
        }
    }
}
  

Applications

If you have ever noticed your phone or tablet screen gets brighter or dimmer depending on your environment, you have witnessed a light sensor in action. Car and street lights use light sensors to automatically turn on and off lights - saving energy, time, and improving safety.

Light Sensors are used in some shipping container security systems to track when the container has been open or closed. Museums use light sensors to determine the placement of sensitive works of art. Placement and direction of solar panel installations also use light sensors.

Practice

Using your Getting Started Kit and Light Phidget, you can simulate a shipping container security system.

  1. Replace your Humidity Phidget with the Light Phidget.
  2. Place the Light Phidget inside the box.
  3. Write a program that uses the Light Phidget to let you know when the box is open or closed.
  4. Turn on the green LED when the box is closed and turn the Red LED on when the box is open.

Check out the advanced lesson Using the Sensor API before you use the API for the first time.

API
Troubleshoot
  1. Make sure everything is plugged in properly and your VINT Hub is connected to your computer
  2. Stop running other programs that are using your Phidgets before running a program.

If these do not solve your issue visit the Advanced Troubleshooting Page.

What are Phidgets?

Phidgets are programmable, modular USB devices, either sensors or controllers that you can connect together. Simply write code in your favorite language and solve real-world problems.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done