Advanced Lesson

Two or More Smart Phidgets

In this lesson, you will learn how to use multiple smart Phidgets in your projects! You will partner with another student and use two Humidity Phidgets.

Setup

Click through the images to set up everything up!

Write Code (Java)

Copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.

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

  
package gettingstarted;

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

public class GettingStarted {
    public static void main(String[] args) throws Exception{

        //Create 
        TemperatureSensor temperatureSensor0 = new TemperatureSensor();
        TemperatureSensor temperatureSensor1 = new TemperatureSensor();

         //Address | Smart Phidgets must be addressed when you are using more than one of the same software objects in your program. Setting the HubPort allows you to keep track of which physical Phidget is paired with each software object. If you do not address the objects, your program will have unpredictable results.
         temperatureSensor0.setHubPort(3);
         temperatureSensor1.setHubPort(2);

        //Open 
        temperatureSensor0.open(1000);
        temperatureSensor1.open(1000);

        //Use your Phidgets 
        while (true) {
            System.out.println("Temperature 0: " + temperatureSensor0.getTemperature() + " °C" );
            System.out.println("Temperature 1: " + temperatureSensor1.getTemperature() + " °C" );
            Thread.sleep(150);
        }
    }
}
 
  
//Add Phidgets Library 
import com.phidget22.*;

public class GettingStarted {
    public static void main(String[] args) throws Exception{

        //Create 
        TemperatureSensor temperatureSensor0 = new TemperatureSensor();
        TemperatureSensor temperatureSensor1 = new TemperatureSensor();

         //Address | Smart Phidgets must be addressed when you are using more than one of the same software objects in your program. Setting the HubPort allows you to keep track of which physical Phidget is paired with each software object. If you do not address the objects, your program will have unpredictable results.
         temperatureSensor0.setHubPort(3);
         temperatureSensor1.setHubPort(2);

        //Open 
        temperatureSensor0.open(1000);
        temperatureSensor1.open(1000);

        //Use your Phidgets 
        while (true) {
            System.out.println("Temperature 0: " + temperatureSensor0.getTemperature() + " °C" );
            System.out.println("Temperature 1: " + temperatureSensor1.getTemperature() + " °C" );
            Thread.sleep(150);
        }
    }
}
 
  
//Add Phidgets Library
import com.phidget22.*;

//Define
TemperatureSensor temperatureSensor0;
TemperatureSensor temperatureSensor1;

void setup(){
  try{
    
    //Create 
    temperatureSensor0 = new TemperatureSensor();
    temperatureSensor1 = new TemperatureSensor();

    //Address | Smart Phidgets must be addressed when you are using more than one of the same software objects in your program. Setting the HubPort allows you to keep track of which physical Phidget is paired with each software object. If you do not address the objects, your program will have unpredictable results.
    temperatureSensor0.setHubPort(3);
    temperatureSensor1.setHubPort(2);
        
    //Open 
    temperatureSensor0.open(1000);
    temperatureSensor1.open(1000);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}

void draw(){
  try{
    //Use your Phidgets
    println("Temperature 0: " + temperatureSensor0.getTemperature() + " °C" );
    println("Temperature 1: " + temperatureSensor1.getTemperature() + " °C" );
    delay(150);
    
  }catch(Exception e){
   e.printStackTrace(); 
  }
}
 

Write Code (Python)

Copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.

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

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

#Create 
temperatureSensor0 = TemperatureSensor()
temperatureSensor1 = TemperatureSensor()

#Address | Smart Phidgets must be addressed when you are using more than one of the same software objects in your program. Setting the HubPort allows you to keep track of which physical Phidget is paired with each software object. If you do not address the objects, your program will have unpredictable results.
temperatureSensor0.setHubPort(3)
temperatureSensor1.setHubPort(2)

#Open 
temperatureSensor0.openWaitForAttachment(1000)
temperatureSensor1.openWaitForAttachment(1000)

#Use your Phidgets 
while(True):
    print(" Temperature 0 : " + str(temperatureSensor0.getTemperature()) + " °C" )
    print(" Temperature 1 : " + str(temperatureSensor1.getTemperature()) + " °C" )
    time.sleep(0.15)
  

Write Code (C#)

Copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.

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

  
//Add Phidgets Library 
using Phidget22;

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

            //Create 
            TemperatureSensor temperatureSensor0 = new TemperatureSensor();
            TemperatureSensor temperatureSensor1 = new TemperatureSensor();

            // Address | Smart Phidgets must be addressed when you are using more than one of the same software objects in your program. Setting the HubPort allows you to keep track of which physical Phidget is paired with each software object. If you do not address the objects, your program will have unpredictable results.
            temperatureSensor0.HubPort = 3;
            temperatureSensor0.IsHubPortDevice = false;
            temperatureSensor1.HubPort = 2;
            temperatureSensor1.IsHubPortDevice = false;

            //Open 
            temperatureSensor0.Open(1000);
            temperatureSensor1.Open(1000);

            //Use your Phidgets 
            while (true)
            {
                System.Console.WriteLine("Temperature 0: " + temperatureSensor0.Temperature + "°C");
                System.Console.WriteLine("Temperature 1: " + temperatureSensor1.Temperature + "°C");
                System.Threading.Thread.Sleep(150);
            }
        }
    }
}
  

Write Code (Swift)

Create two labels in your window and copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.

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

Create two labels

  

 import Cocoa
//Add Phidgets Library | You used cocoapods to install the Phidget library. The statement below give your program access to that code.
import Phidget22Swift

class ViewController: NSViewController {
    
    @IBOutlet weak var temperature0Label: NSTextField!
    @IBOutlet weak var temperature1Label: NSTextField!
    
    //Create | Here you have created a TemperatureSensor object. TemperatureSensor is a class in your Phidgets library that gathers temperature data from your Phidget.
    let temperatureSensor0 = TemperatureSensor()
    let temperatureSensor1 = TemperatureSensor()

    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Subscribe to events
            let _ = temperatureSensor0.temperatureChange.addHandler(onTemperatureChange0)
            let _ = temperatureSensor1.temperatureChange.addHandler(onTemperatureChange1)
            
            //Address | Specify which temperature sensors is on which hub port!
            try temperatureSensor0.setHubPort(2)
            try temperatureSensor1.setHubPort(3)

            //Open | Open establishes a connection between your object and your physical Phidget.
            try temperatureSensor0.open()
            try temperatureSensor1.open()
            
        }catch{
            print(error)
        }
    }
    
    //Data events
    func onTemperatureChange0(sender:TemperatureSensor, temperature: Double){
        DispatchQueue.main.async {
            //Use your Phidget | Update the label with the latest temperature data.
            self.temperature0Label.stringValue = String(temperature) + "°C"
        }
    }
    func onTemperatureChange1(sender:TemperatureSensor, temperature: Double){
        DispatchQueue.main.async {
            //Use your Phidget | Update the label with the latest temperature data.
            self.temperature1Label.stringValue = String(temperature) + "°C"
        }
    }
}
  

Run Your Program

You will see the temperature being displayed from both sensors in degrees Celsius.

When do I need to address Smart Phidgets?

You must address your Smart Phidgets in your program when you are using more than one of the same Phidget (i.e. two Humidity Phidgets) or more than one of the same object (i.e. two Temperature Sensor Objects). If you don't address them, you won't know which sensor is reporting to which object which can cause confusion!

Practice

  1. Modify your code so when you press the red button the temperature from temperatureSensor0 prints and when you press the green button the temperature from temperatureSensor1 prints.
Solutions available for educators. Email us for access.
Troubleshoot

I am getting a "Timed Out" exception.

  1. Make sure the USB cable from your VINT Hub to your computer is attached properly.
  2. Make the Phidget cable (the black, red and white one) is connected to your VINT Hub and to your Humidity Phidget properly).
  3. Make sure no other program is running that uses Phidgets. If a Phidget is already in use in another program, it will be busy and won't respond to this one.

Still having issues?

Visit the Advanced Troubleshooting Page or contact us (education@phidgets.com).

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