Introduction: Logging Sensor Data to a File
It is often useful to log data from sensors so that the data can later be reviewed and analyzed. For example, you could log the pH of a solution during a titration and analyze the graph later to find the equivalence point. Or, you could log the temperature of a server room to make sure the air conditioning system is working adequately, even when no one is there to verify.
Supplies
- Humidity Phidget
- VINT Hub
Step 1: Setup
Attach your Humidity Phidget to your VINT Hub, and your VINT Hub to your computer.
Step 2: Write Code - Python
#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)
#Record data points
count = 0
#Use your Phidgets
while (True):
#Update user
print("Logging data...")
#Write data to file in CSV format
with open ('phidgets_temperature.csv','a') as datafile:
datafile.write(str(temperatureSensor.getTemperature()) + "\n")
#Increment count
count += 1
#If 10 data points have been recorded, close file and exit program
if(count == 10):
print("Logging complete, exiting program")
exit()
time.sleep(0.5)
Step 3: Write Code - Java
package logdata;
import com.phidget22.*;
import java.io.FileWriter;
public class LogData {
public static void main(String[] args) throws Exception{
//Create
TemperatureSensor temperatureSensor = new TemperatureSensor();
//Open
temperatureSensor.open(1000);
//Create data file
FileWriter outfile = new FileWriter("phidgets_temperature.csv");
//Record data points
int count = 0;
//Use your Phidgets
while (true) {
//Update user
System.out.println("Logging data...");
//Write data to file in CSV format
outfile.write(Double.toString(temperatureSensor.getTemperature()) + "\n");
//Increment count
count += 1;
//If 10 data points have been recorded, close file and exit program
if(count == 10){
outfile.close();
System.out.println("Logging complete, exiting program");
System.exit(0);
}
Thread.sleep(500);
}
}
}
Step 4: Run Your Code
The program will log 10 data points and create a new file that you can open and view afterward. The file is generally located at the same location your script is (this will vary based on language/environment).
Step 5: What Is a CSV and Why Use It?
CSV stands for Comma-separated Values. As the name suggests, you create a CSV file by simply writing data to a file as a string, and using commas to separate values. The benefit of using CSV files is that they are easy to generate (as shown in the code above) and you can open them with any spreadsheet program, like Microsoft Excel, to view and manipulate your data.
In the images above, you can see a CSV opened in Excel, and the same file opened in Notepad. As you can see, the values are simply separated by commas. When writing these files from your code, you can move to a new line by adding the newline character “\n” which is shown in the code above.
Step 6: Learn More
Learn more at phidgets.com/education
Are you a teacher? Sign up for a FREE kit here: phidgets.com/education/free





