Phidget Plant Kit

Use the Light Phidget

In this step, you will use your Light Phidget to determine the amount of light your plant is getting.

You can continue working from your PhidgetsPlant project in this step. Replace your previous code with the following code.

Write Code (Java)

Not your programming language? Set your preferences so we can display relevant code examples.

  
package phidgetsplant;

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

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

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

//Define
LightSensor light;

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

void draw(){
  try{
    
    //Use your Phidgets
    println("Light Level: " + light.getIlluminance());
    delay(250);    
  }catch(Exception e){
    //Handle Exceptions
    e.printStackTrace();
  }
}
  

Write Code (Python)

Not your programming language? Set your preferences so we can display relevant code examples.

  
#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)
  

Write Code (C#)

Not your programming language? Set your preferences so we can display relevant code examples.

  
//Add Phidgets Library
using Phidget22;

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

            //Create
            LightSensor light = new LightSensor();

            //Open
            light.Open(1000);

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

Write Code (Swift)

Not your programming language? Set your preferences so we can display relevant code examples.

the code below assumes you've created a single label and linked it to an IBAction named lightLabel.

  
import Cocoa
//Add Phidgets Library
import Phidget22Swift

class ViewController: NSViewController {

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

Run Your Program

You will see an illuminance value being printed to the screen.

How Much Light?

Every plant has different light requirements. Some require direct sunlight, others flourish in shade. Determine which type of plant you have, and then use the table below to find the illuminance range best suited to your plant.

Light Requirement Illuminance Range
Low Light < 2000lx
Medium Light 2000lx – 10000lx
Full Light > 10000lx

Make sure to write these values down. You will need them later when designing your system.

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 you are still having issues visit Advanced Troubleshooting

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

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