Lesson 4

Read Temperature

In this lesson, you’ll learn how to write a program that gets data from a temperature sensor!

Connect Sensor

Connect your Humidity Phidget to port 3.

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 temperatureSensor = new TemperatureSensor();

        //Open 
        temperatureSensor.open(1000);

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

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

        //Create 
        TemperatureSensor temperatureSensor = new TemperatureSensor();

        //Open 
        temperatureSensor.open(1000);

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

//Define
TemperatureSensor temperatureSensor;

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

void draw(){
  try{
    //Use your Phidgets 
    println("Temperature: " + temperatureSensor.getTemperature() + " °C" );
    delay(150);
    
  }catch(Exception e){
    //Handle Exceptions 
    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 
temperatureSensor = TemperatureSensor()

#Open 
temperatureSensor.openWaitForAttachment(1000)

#Use your Phidgets 
while(True):
    print(" Temperature: " + str(temperatureSensor.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 temperatureSensor = new TemperatureSensor();

            //Open 
            temperatureSensor.Open(1000);

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

Write Code (Swift)

Create a label 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 a label

  
 import Cocoa
//Add Phidgets Library 
import Phidget22Swift

class ViewController: NSViewController {
    
    @IBOutlet weak var temperatureLabel: NSTextField!
    
    //Create 
    let temperatureSensor = TemperatureSensor()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Subscribe to event
            let _ = temperatureSensor.temperatureChange.addHandler(onTemperatureChange)

            //Open 
            try temperatureSensor .open()
            
        }catch{
            print(error)
        }
    }
    
    func onTemperatureChange(sender:TemperatureSensor, temperature: Double){
        DispatchQueue.main.async {
            //Use your Phidget 
            self.temperatureLabel.stringValue = String(temperature) + "°C"
        }
    }
}
  

Run Your Program

You will see the temperature displayed in degrees Celsius.

What about Addressing the Phidget?

The Humidity Phidget is a Smart Phidget. Smart Phidgets communicate to the VINT Hub and relay some basic information like what they are and where they are connected. In your previous lessons, you had to set the HubPort and the IsHubPortDevice property. With a Smart Phidget, you don't have to worry about setting those.

Practice

  1. Convert temperature data from degrees Celsius to degrees Fahrenheit. Hint: °F = (°C × 1.8) + 32
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